Skip to content

Commit

Permalink
✨ feat: 음식 이름 중복 여부 확인 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Nov 6, 2023
1 parent 139b0ac commit 6740bc6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public interface FoodRepository extends JpaRepository<Food, Long> {
boolean existsByIdAndUserId(Long id, Long userId); // 유저가 먹은 음식인지 확인
boolean existsByName(String name);
List<Food> findAllByUserIdAndDate(Long userId, LocalDate date); //유저가 특정 날짜에 먹은 음식 반환
List<Food> findAllByUserIdAndDateBetween(Long userId, LocalDate startDate, LocalDate endDate); // 유저가 특정 기간 내에 먹은 음식 반환
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class FoodService {
@CacheEvict(value = "ResponseFoodDto", key = "#createFoodDto.getUserId()+#createFoodDto.getDate()", cacheManager = "diareatCacheManager")
@Transactional
public Long saveFood(CreateFoodDto createFoodDto) {
if (foodRepository.existsByName(createFoodDto.getName())){
throw new FoodException(ResponseCode.FOOD_NAME_ALREADY_EXIST);
}
User user = getUserById(createFoodDto.getUserId());
Food food = Food.createFood(createFoodDto.getName(), user, createFoodDto.getBaseNutrition());
return foodRepository.save(food).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum ResponseCode {
FOLLOWED_ALREADY(HttpStatus.CONFLICT, false, "이미 팔로우한 사용자입니다."),
UNFOLLOWED_ALREADY(HttpStatus.CONFLICT, false, "이미 언팔로우한 사용자입니다."),
FAVORITE_ALREADY_EXIST(HttpStatus.CONFLICT, false, "이미 즐겨찾기에 존재하는 음식입니다."),
FOOD_NAME_ALREADY_EXIST(HttpStatus.CONFLICT, false, "이미 존재하는 음식 이름입니다."),

// 500 Internal Server Error
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, false, "서버에 오류가 발생하였습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ void testGetNutritionSumByDate() throws Exception{
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto,ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByDate(any(Long.class),any(LocalDate.class))).thenReturn(responseNutritionSumByDateDto);

String json = mapper.writeValueAsString(responseNutritionSumByDateDto);

//When
mockMvc.perform(MockMvcRequestBuilders
Expand Down Expand Up @@ -295,7 +294,6 @@ void testGetNutritionSumByWeek() throws Exception {
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByWeek(any(Long.class))).thenReturn(responseNutritionSumByDateDto);

String json = mapper.writeValueAsString(responseNutritionSumByDateDto);

//When
mockMvc.perform(MockMvcRequestBuilders
Expand Down Expand Up @@ -329,7 +327,6 @@ void testGetNutritionSumByMonth() throws Exception {
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByMonth(any(Long.class))).thenReturn(responseNutritionSumByDateDto);

String json = mapper.writeValueAsString(responseNutritionSumByDateDto);

//When
mockMvc.perform(MockMvcRequestBuilders
Expand Down Expand Up @@ -369,7 +366,6 @@ void testGetScoreOfUserWithBestAndWorstFoods() throws Exception{
ApiResponse<ResponseScoreBestWorstDto> expectedResponse = ApiResponse.success(responseScoreBestWorstDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getScoreOfUserWithBestAndWorstFoods(any(Long.class))).thenReturn(responseScoreBestWorstDto);

String json = mapper.writeValueAsString(responseScoreBestWorstDto);

//When
mockMvc.perform(MockMvcRequestBuilders
Expand Down Expand Up @@ -399,7 +395,6 @@ void testGetAnalysisOfUser() throws Exception {

ApiResponse<ResponseAnalysisDto> expectedResponse = ApiResponse.success(responseAnalysisDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getAnalysisOfUser(any(Long.class))).thenReturn(responseAnalysisDto);
String json = mapper.writeValueAsString(responseAnalysisDto);

//When
mockMvc.perform(MockMvcRequestBuilders
Expand Down

0 comments on commit 6740bc6

Please sign in to comment.