Skip to content

Commit

Permalink
Merge pull request #49 from CAUSOLDOUTMEN/feature/47-refactor-dtovalid
Browse files Browse the repository at this point in the history
Refactor: 요청 Dto 검증 과정 추가 (#47)
  • Loading branch information
synoti21 authored Nov 2, 2023
2 parents 5895213 + a94e464 commit 630344f
Show file tree
Hide file tree
Showing 32 changed files with 354 additions and 117 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ dependencies {

// Redis dependency
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Validation dependency
implementation 'org.springframework.boot:spring-boot-starter-validation'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String createToken(String userPk) {
return Jwts.builder()
.setClaims(claims) // 정보 저장
.setIssuedAt(now) // 토큰 발행 시간 정보
.setExpiration(new Date(now.getTime() + (30 * 60 * 1000L))) // 토큰 유효시각 설정 (30분)
.setExpiration(new Date(now.getTime() + (360 * 60 * 1000L))) // 토큰 유효시각 설정 (360분)
.signWith(SignatureAlgorithm.HS256, secretKey) // 암호화 알고리즘과, secret 값
.compact();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.diareat.diareat.auth.component.JwtTokenProvider;
import com.diareat.diareat.auth.service.KakaoAuthService;
import com.diareat.diareat.user.dto.JoinUserDto;
import com.diareat.diareat.user.dto.request.JoinUserDto;
import com.diareat.diareat.user.service.UserService;
import com.diareat.diareat.util.api.ApiResponse;
import com.diareat.diareat.util.api.ResponseCode;
Expand All @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@Api(tags = "2. Auth")
Expand All @@ -36,7 +37,7 @@ public ApiResponse<HashMap<Long, String>> authCheck(@RequestHeader String access
// 회원가입 (성공 시 Jwt 토큰 발급)
@Operation(summary = "[회원가입] 회원가입 및 토큰 발급", description = "신규 회원가입을 처리하고, 회원가입 성공 시 Jwt 토큰을 발급합니다.")
@PostMapping("/join")
public ApiResponse<HashMap<Long, String>> saveUser(JoinUserDto joinUserDto) {
public ApiResponse<HashMap<Long, String>> saveUser(@Valid @RequestBody JoinUserDto joinUserDto) {
Long userId = userService.saveUser(kakaoAuthService.createUserDto(joinUserDto));
HashMap<Long, String> map = new HashMap<>();
map.put(userId, jwtTokenProvider.createToken(userId.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.diareat.diareat.auth.component.KakaoUserInfo;
import com.diareat.diareat.auth.dto.KakaoUserInfoResponse;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.dto.CreateUserDto;
import com.diareat.diareat.user.dto.JoinUserDto;
import com.diareat.diareat.user.dto.request.CreateUserDto;
import com.diareat.diareat.user.dto.request.JoinUserDto;
import com.diareat.diareat.user.repository.UserRepository;
import com.diareat.diareat.util.api.ResponseCode;
import com.diareat.diareat.util.exception.UserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

Expand All @@ -23,7 +24,7 @@ public class FoodController {
//음식 정보 저장
@Operation(summary = "[음식] 음식 정보 저장", description = "촬영한 음식 정보를 저장합니다.")
@PostMapping("/save")
public ApiResponse<Long> saveFood(CreateFoodDto createFoodDto){
public ApiResponse<Long> saveFood(@RequestBody @Valid CreateFoodDto createFoodDto){
return ApiResponse.success(foodService.saveFood(createFoodDto),ResponseCode.FOOD_CREATE_SUCCESS.getMessage());
}

Expand All @@ -42,7 +43,7 @@ public ApiResponse<List<ResponseFoodDto>> getFoodListByDate(@PathVariable Long u
//음식 정보 수정
@Operation(summary = "[음식] 음식 정보 수정",description = "음식에 대한 정보를 수정합니다.")
@PostMapping("/update")
public ApiResponse<Void> updateFood(UpdateFoodDto updateFoodDto){
public ApiResponse<Void> updateFood(@RequestBody @Valid UpdateFoodDto updateFoodDto){
foodService.updateFood(updateFoodDto);
return ApiResponse.success(null,ResponseCode.FOOD_UPDATE_SUCCESS.getMessage());
}
Expand All @@ -57,7 +58,7 @@ public ApiResponse<Void> deleteFood(@PathVariable Long foodId, @RequestHeader Lo
//즐겨찾기에 음식 저장
@Operation(summary = "[즐겨찾기] 즐겨찾기에 저장",description = "유저의 즐겨찾기에 음식을 저장합니다.")
@PostMapping("/favorite")
public ApiResponse<Long> saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto){
public ApiResponse<Long> saveFavoriteFood(@RequestBody @Valid CreateFavoriteFoodDto createFavoriteFoodDto){
return ApiResponse.success(foodService.saveFavoriteFood(createFavoriteFoodDto),ResponseCode.FOOD_FAVORITE_CREATE_SUCCESS.getMessage());
}

Expand All @@ -71,7 +72,7 @@ public ApiResponse<List<ResponseFavoriteFoodDto>> getFavoriteFoodList(@PathVaria
//즐겨찾기 음식 수정
@Operation(summary = "[즐겨찾기] 즐겨찾기 수정",description = "유저의 즐겨찾기에 등록된 음식의 정보를 수정합니다.")
@PostMapping("/favorite/update")
public ApiResponse<Void> updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto){
public ApiResponse<Void> updateFavoriteFood(@RequestBody @Valid UpdateFavoriteFoodDto updateFavoriteFoodDto){
foodService.updateFavoriteFood(updateFavoriteFoodDto);
return ApiResponse.success(null, ResponseCode.FOOD_FAVORITE_UPDATE_SUCCESS.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.util.MessageUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CreateFavoriteFoodDto {

@NotNull(message = MessageUtil.NOT_NULL)
private Long foodId;

@NotNull(message = MessageUtil.NOT_NULL)
private Long userId;

@NotBlank(message = MessageUtil.NOT_BLANK)
private String name;

@NotNull(message = MessageUtil.NOT_NULL)
private BaseNutrition baseNutrition;

public static CreateFavoriteFoodDto of(Long foodId, Long userId, String name, BaseNutrition baseNutrition) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/diareat/diareat/food/dto/CreateFoodDto.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.util.MessageUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PastOrPresent;
import java.time.LocalDate;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CreateFoodDto {

@NotNull(message = MessageUtil.NOT_NULL)
private Long userId;

@NotBlank(message = MessageUtil.NOT_BLANK)
private String name;

@NotNull(message = MessageUtil.NOT_NULL)
private BaseNutrition baseNutrition;

@PastOrPresent(message = MessageUtil.PAST_OR_PRESENT)
private LocalDate date;

public static CreateFoodDto of(Long userId, String name, BaseNutrition baseNutrition, LocalDate date) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.util.MessageUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UpdateFavoriteFoodDto {

@NotNull(message = MessageUtil.NOT_NULL)
private Long favoriteFoodId;

@NotNull(message = MessageUtil.NOT_NULL)
private Long userId;

@NotBlank(message = MessageUtil.NOT_BLANK)
private String name;

@NotNull(message = MessageUtil.NOT_NULL)
private BaseNutrition baseNutrition;

public static UpdateFavoriteFoodDto of(Long favoriteFoodId, Long userId, String name, BaseNutrition baseNutrition) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/diareat/diareat/food/dto/UpdateFoodDto.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.util.MessageUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UpdateFoodDto {

@NotNull(message = MessageUtil.NOT_NULL)
private Long foodId;

@NotNull(message = MessageUtil.NOT_NULL)
private Long userId;

@NotBlank(message = MessageUtil.NOT_BLANK)
private String name;

@NotNull(message = MessageUtil.NOT_NULL)
private BaseNutrition baseNutrition;

public static UpdateFoodDto of(Long foodId, Long userId, String name, BaseNutrition baseNutrition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.diareat.diareat.food.repository.FoodRepository;
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.dto.ResponseRankUserDto;
import com.diareat.diareat.user.dto.response.ResponseRankUserDto;
import com.diareat.diareat.user.repository.FollowRepository;
import com.diareat.diareat.user.repository.UserRepository;
import com.diareat.diareat.util.api.ResponseCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.diareat.diareat.user.controller;

import com.diareat.diareat.user.dto.*;
import com.diareat.diareat.user.dto.request.SearchUserDto;
import com.diareat.diareat.user.dto.request.UpdateUserDto;
import com.diareat.diareat.user.dto.request.UpdateUserNutritionDto;
import com.diareat.diareat.user.dto.response.ResponseSearchUserDto;
import com.diareat.diareat.user.dto.response.ResponseSimpleUserDto;
import com.diareat.diareat.user.dto.response.ResponseUserDto;
import com.diareat.diareat.user.dto.response.ResponseUserNutritionDto;
import com.diareat.diareat.user.service.UserService;
import com.diareat.diareat.util.api.ApiResponse;
import com.diareat.diareat.util.api.ResponseCode;
Expand All @@ -9,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "1. User")
Expand Down Expand Up @@ -36,7 +43,7 @@ public ApiResponse<ResponseUserDto> getUserInfo(@PathVariable Long userId) {
// 회원정보 수정
@Operation(summary = "[프로필] 회원 정보 수정", description = "회원 정보를 수정합니다.")
@PutMapping("/update")
public ApiResponse<Void> updateUserInfo(UpdateUserDto updateUserDto) {
public ApiResponse<Void> updateUserInfo(@RequestBody @Valid UpdateUserDto updateUserDto) {
userService.updateUserInfo(updateUserDto);
return ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
}
Expand All @@ -51,33 +58,31 @@ public ApiResponse<ResponseUserNutritionDto> getUserNutrition(@PathVariable Long
// 회원 기준섭취량 직접 수정
@Operation(summary = "[프로필] 회원 기준섭취량 직접 수정", description = "회원 기준섭취량을 직접 수정합니다.")
@PutMapping("{userId}/nutrition")
public ApiResponse<Void> updateUserNutrition(UpdateUserNutritionDto updateUserNutritionDto) {
public ApiResponse<Void> updateUserNutrition(@RequestBody @Valid UpdateUserNutritionDto updateUserNutritionDto) {
userService.updateBaseNutrition(updateUserNutritionDto);
return ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
}

// 회원의 친구 검색 결과 조회
@Operation(summary = "[주간 랭킹] 회원의 친구 검색 결과 조회", description = "회원의 친구 검색 결과를 조회합니다.")
@GetMapping("{userId}/search/{name}")
public ApiResponse<List<ResponseSearchUserDto>> searchUser(@PathVariable Long userId, @RequestParam String name) {
return ApiResponse.success(userService.searchUser(userId, name), ResponseCode.USER_SEARCH_SUCCESS.getMessage());
@GetMapping("/search")
public ApiResponse<List<ResponseSearchUserDto>> searchUser(@RequestBody @Valid SearchUserDto searchUserDto) {
return ApiResponse.success(userService.searchUser(searchUserDto.getUserId(), searchUserDto.getInputName()), ResponseCode.USER_SEARCH_SUCCESS.getMessage());
}

// 실제 팔로잉 유저들의 점수 계산하여 랭킹 형태로 반환하는 API: FoodService에서 계산할지 UserService에서 FoodRepository를 콜해서 처리할지 협의 필요

// 회원이 특정 회원 팔로우
@Operation(summary = "[주간 랭킹] 회원이 특정 회원 팔로우", description = "회원이 특정 회원을 팔로우합니다.")
@PostMapping("{userId}/follow/{followId}")
public ApiResponse<Void> followUser(@PathVariable Long userId, @PathVariable Long followId) {
userService.followUser(userId, followId);
return ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
return ApiResponse.success(null, ResponseCode.USER_FOLLOW_SUCCESS.getMessage());
}

// 회원이 특정 회원 팔로우 취소
@Operation(summary = "[주간 랭킹] 회원이 특정 회원 팔로우 취소", description = "회원이 특정 회원을 팔로우 취소합니다.")
@DeleteMapping("{userId}/follow/{followId}")
public ApiResponse<Void> unfollowUser(@PathVariable Long userId, @PathVariable Long followId) {
userService.unfollowUser(userId, followId);
return ApiResponse.success(null, ResponseCode.USER_UPDATE_SUCCESS.getMessage());
return ApiResponse.success(null, ResponseCode.USER_UNFOLLOW_SUCCESS.getMessage());
}
}
14 changes: 0 additions & 14 deletions src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/com/diareat/diareat/user/dto/JoinUserDto.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.diareat.diareat.user.dto;
package com.diareat.diareat.user.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -7,7 +7,7 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CreateUserDto {
public class CreateUserDto { // JoinUserDto에서 검증 절차를 대행하기에 검증절차 생략

private String name;
private String image;
Expand Down
Loading

0 comments on commit 630344f

Please sign in to comment.