Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 즐찾 음식으로부터 음식 생성 메소드 구현 (#96) #97

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ public ApiResponse<ResponseAnalysisDto> getAnalysisOfUser(@PathVariable Long use
public ApiResponse<List<ResponseRankUserDto>> getUserRankByWeek(@PathVariable Long userId){
return ApiResponse.success(foodService.getUserRankByWeek(userId),ResponseCode.FOOD_RANK_READ_SUCCESS.getMessage());
}

@Operation(summary = "[음식] 즐겨찾기 음식으로 음식 생성",description = "즐겨찾기 음식으로 음식을 생성합니다.")
@PostMapping("/favorite/createfrom")
public ApiResponse<Long> createFoodFromFavoriteFood(@RequestBody @Valid CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto){
return ApiResponse.success(foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto),ResponseCode.FOOD_CREATE_SUCCESS.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.diareat.diareat.food.dto;

import com.diareat.diareat.util.MessageUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CreateFoodFromFavoriteFoodDto {
@NotNull(message = MessageUtil.NOT_NULL)
private Long userId;

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

public static CreateFoodFromFavoriteFoodDto of(Long userId, Long favoriteFoodId) {
return new CreateFoodFromFavoriteFoodDto(userId, favoriteFoodId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,11 @@ public List<ResponseRankUserDto> getUserRankByWeek(Long userId) {
}

@Transactional()
public Long createFoodFromFavoriteFood(Long favoriteFoodId) {
FavoriteFood favoriteFood = getFavoriteFoodById(favoriteFoodId);
public Long createFoodFromFavoriteFood(CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto) {
validateFavoriteFood(createFoodFromFavoriteFoodDto.getFavoriteFoodId(), createFoodFromFavoriteFoodDto.getUserId());
FavoriteFood favoriteFood = getFavoriteFoodById(createFoodFromFavoriteFoodDto.getFavoriteFoodId());
Food food = FavoriteFood.createFoodFromFavoriteFood(favoriteFood);
Comment on lines +322 to 325
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

누락된 로직 반영해주셔서 감사합니다! 다만 새로 태어난 food가 자신이 즐찾음식으로부터 태어났다는 것을 표현하기 위해, food.setFavoriteFood 메서드만 325 라인 아래에 한 줄 추가해주시면 될 것 같습니다!

food.setFavoriteFood(favoriteFood);
return foodRepository.save(food).getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,29 @@ void testGetUserRankByWeek() throws Exception{
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].proteinScore").value(expectedResponse.getData().get(0).getProteinScore()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].fatScore").value(expectedResponse.getData().get(0).getFatScore()));
}

@DisplayName("즐겨찾기 음식으로 음식 생성")
@Test
@WithMockUser("test")
void testCreateFoodFromFavoriteFood() throws Exception{
//Given
Long testNewFoodId = 1L;
CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto = CreateFoodFromFavoriteFoodDto.of(testUserId, testFavoriteFoodId);
when(foodService.createFoodFromFavoriteFood(any(CreateFoodFromFavoriteFoodDto.class))).thenReturn(testNewFoodId);
ApiResponse<Long> expectedResponse = ApiResponse.success(foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto), ResponseCode.FOOD_CREATE_SUCCESS.getMessage());
String json = mapper.writeValueAsString(createFoodFromFavoriteFoodDto);


//When
mockMvc.perform(MockMvcRequestBuilders
.post("/api/food/favorite/createfrom")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.header.code").value(expectedResponse.getHeader().getCode()))
.andExpect(MockMvcResultMatchers.jsonPath("$.header.message").value(expectedResponse.getHeader().getMessage()))
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedResponse.getData()))
.andExpect(MockMvcResultMatchers.jsonPath("$.msg").value(expectedResponse.getMsg()));
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/diareat/diareat/service/FoodServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,31 @@ void testGetAnalysisOfUser(){
verify(foodRepository, times(1)).findAllByUserIdAndDateBetween(user.getId(), LocalDate.now().with(DayOfWeek.MONDAY), LocalDate.now(),sort);

}

@Test
void testCreateFoodFromFavoriteFood() {
// given
User user = User.createUser("testUser", "testImage","testPassword", 1, 180, 80, 18, BaseNutrition.createNutrition(2000,400,100,50));
Food food = Food.createFood( "Food", user, BaseNutrition.createNutrition(100, 100 ,10, 1), 2010,1,1);
FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood("FavoriteFood", user, food, BaseNutrition.createNutrition(100, 100 ,10, 1));
Food newFood = Food.createFood("FoodFromFavorite", user, BaseNutrition.createNutrition(100, 100 ,10, 1), 2010,1,1);
user.setId(1L);
food.setId(2L);
favoriteFood.setId(3L);
newFood.setId(4L);
CreateFoodFromFavoriteFoodDto createFoodFromFavoriteFoodDto = CreateFoodFromFavoriteFoodDto.of(user.getId(), favoriteFood.getId());


given(favoriteFoodRepository.existsById(favoriteFood.getId())).willReturn(true);
given(favoriteFoodRepository.existsByIdAndUserId(favoriteFood.getId(),user.getId())).willReturn(true);
given(favoriteFoodRepository.findById(favoriteFood.getId())).willReturn(Optional.of(favoriteFood));
given(foodRepository.save(any(Food.class))).willReturn(newFood);

// when
Long foodId = foodService.createFoodFromFavoriteFood(createFoodFromFavoriteFoodDto);

// then
assertEquals(4L, foodId);
verify(foodRepository, times(1)).save(any(Food.class));
}
}