From 1d895e94797ae47a498cf3a7d69bf4a0b297bbc1 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Tue, 3 Oct 2023 17:31:34 +0900 Subject: [PATCH 01/22] =?UTF-8?q?:see=5Fno=5Fevil:=20feat:=20gitignore?= =?UTF-8?q?=EC=97=90=20application-db.properties=20=EC=B6=94=EA=B0=80=20(#?= =?UTF-8?q?9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- src/main/resources/application-db.properties | 11 ----------- 2 files changed, 2 insertions(+), 12 deletions(-) delete mode 100644 src/main/resources/application-db.properties diff --git a/.gitignore b/.gitignore index e48b6be..88dbad9 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ out/ /.nb-gradle/ ### VS Code ### -.vscode/ \ No newline at end of file +.vscode/ +application-db.properties \ No newline at end of file diff --git a/src/main/resources/application-db.properties b/src/main/resources/application-db.properties deleted file mode 100644 index 1a2cded..0000000 --- a/src/main/resources/application-db.properties +++ /dev/null @@ -1,11 +0,0 @@ -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=create - -# Remote DB -spring.datasource.url=${DB_ENDPOINT} -spring.datasource.username=${DB_USERNAME} -spring.datasource.password=${DB_PASSWORD} - -spring.jpa.show-sql=true -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect -spring.jpa.properties.hibernate.format_sql=true From 8de56a812c3189d0ecc1061dd94fd17008b46908 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 15:29:12 +0900 Subject: [PATCH 02/22] =?UTF-8?q?:sparkles:=20feat:=20ResponseUserDto?= =?UTF-8?q?=EC=97=90=20=EB=88=84=EB=9D=BD=EB=90=9C=20id=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diareat/diareat/user/dto/ResponseUserDto.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java index 4f8b877..7d2e746 100644 --- a/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java @@ -9,6 +9,7 @@ @AllArgsConstructor public class ResponseUserDto { + private Long id; //누락된 id private String name; private int height; private int weight; @@ -16,11 +17,12 @@ public class ResponseUserDto { private int age; private BaseNutrition baseNutrition; - public static ResponseUserDto of(String userName, int userHeight, int userWeight, int userGender, int userAge, BaseNutrition userBaseNutrition) { - return new ResponseUserDto(userName, userHeight, userWeight, userGender, userAge, userBaseNutrition); + + public static ResponseUserDto of(Long id, String userName, int userHeight, int userWeight, int userGender, int userAge, BaseNutrition userBaseNutrition) { + return new ResponseUserDto(id, userName, userHeight, userWeight, userGender, userAge, userBaseNutrition); } public static ResponseUserDto from(User user) { - return new ResponseUserDto(user.getName(), user.getHeight(), user.getWeight(), user.getGender(), user.getAge(), user.getBaseNutrition()); + return new ResponseUserDto(user.getId(), user.getName(), user.getHeight(), user.getWeight(), user.getGender(), user.getAge(), user.getBaseNutrition()); } } From fffacf3a27715d1f5dc9ee17091cea31984e7f71 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 20:23:00 +0900 Subject: [PATCH 03/22] =?UTF-8?q?:sparkles:=20feat:=20=EC=9D=8C=EC=8B=9D?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=20=EB=B0=8F=20=EB=82=A0=EC=A7=9C=EB=B3=84?= =?UTF-8?q?=20=EC=9D=8C=EC=8B=9D=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../food/repository/FoodRepository.java | 4 +++ .../diareat/food/service/FoodService.java | 34 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java b/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java index ee9d2b3..4c4ef98 100644 --- a/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java +++ b/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java @@ -3,5 +3,9 @@ import com.diareat.diareat.food.domain.Food; import org.springframework.data.jpa.repository.JpaRepository; +import java.time.LocalDate; +import java.util.List; + public interface FoodRepository extends JpaRepository { + List findAllByUserIdAndDate(Long userId, LocalDate date); //유저가 특정 날짜에 먹은 음식 반환 } diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index a0702b2..29fa112 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -1,10 +1,24 @@ package com.diareat.diareat.food.service; +import com.diareat.diareat.food.domain.Food; +import com.diareat.diareat.food.dto.CreateFoodDto; +import com.diareat.diareat.food.dto.ResponseFoodDto; import com.diareat.diareat.food.repository.FavoriteFoodRepository; import com.diareat.diareat.food.repository.FoodRepository; +import com.diareat.diareat.user.domain.User; +import com.diareat.diareat.user.dto.ResponseResearchUserDto; +import com.diareat.diareat.user.repository.UserRepository; +import com.diareat.diareat.util.api.ResponseCode; +import com.diareat.diareat.util.exception.FoodException; +import com.diareat.diareat.util.exception.UserException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import javax.transaction.Transactional; +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Collectors; + @RequiredArgsConstructor @Service public class FoodService { @@ -12,11 +26,26 @@ public class FoodService { private final FoodRepository foodRepository; // 유저:음식은 1:다 private final FavoriteFoodRepository favoriteFoodRepository; // 유저:즐찾음식은 1:다 + private final UserRepository userRepository; + // 촬영 후, 음식 정보 저장 - public Long saveFood() { - return null; + @Transactional + public Long saveFood(CreateFoodDto createFoodDto) { + User user = userRepository.findById(createFoodDto.getUserId()) + .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + Food food = Food.createFood(createFoodDto.getName(), user, createFoodDto.getBaseNutrition()); + return foodRepository.save(food).getId(); } + // 회원이 특정 날짜에 먹은 음식 반환 + @Transactional + public List getFoodListByDate(Long userId, LocalDate date){ + List foodList = foodRepository.findAllByUserIdAndDate(userId, date); + return foodList.stream() + .map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getDate(), food.getTime(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList()); + } + + // 음식 정보 수정 public void updateFood() { @@ -42,6 +71,7 @@ public void deleteFavoriteFood() { } + /** * 메서드 구현 유의사항 * 1. 메서드명은 동사로 시작 From 061137a938cc073eb0b9a9f0ab9e8cb42b5f5a87 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 20:23:34 +0900 Subject: [PATCH 04/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20feat:=20?= =?UTF-8?q?=EC=9D=8C=EC=8B=9D=20=EC=A0=80=EC=9E=A5=20=EB=B0=8F=20=EB=82=A0?= =?UTF-8?q?=EC=A7=9C=EB=B3=84=20=EC=9D=8C=EC=8B=9D=20=EB=B0=98=ED=99=98=20?= =?UTF-8?q?TestCode=20=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/service/FoodServiceTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/test/java/com/diareat/diareat/service/FoodServiceTest.java diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java new file mode 100644 index 0000000..2dc8ec6 --- /dev/null +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -0,0 +1,75 @@ +package com.diareat.diareat.service; + +import com.diareat.diareat.food.domain.Food; +import com.diareat.diareat.food.dto.CreateFoodDto; +import com.diareat.diareat.food.dto.ResponseFoodDto; +import com.diareat.diareat.food.repository.FavoriteFoodRepository; +import com.diareat.diareat.food.repository.FoodRepository; +import com.diareat.diareat.food.service.FoodService; +import com.diareat.diareat.user.domain.BaseNutrition; +import com.diareat.diareat.user.domain.User; +import com.diareat.diareat.user.dto.CreateUserDto; +import com.diareat.diareat.user.dto.ResponseResearchUserDto; +import com.diareat.diareat.user.dto.ResponseUserDto; +import com.diareat.diareat.user.dto.UpdateUserDto; +import com.diareat.diareat.user.repository.UserRepository; +import com.diareat.diareat.user.service.UserService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import javax.swing.text.html.Option; +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + + +@SpringBootTest +@Transactional +class FoodServiceTest { + + @Autowired + FoodService foodService; + + @Autowired + UserService userService; + + @Autowired + FoodRepository foodRepository; + + @Autowired + FavoriteFoodRepository favoriteFoodRepository; + + @Autowired + UserRepository userRepository; + + @BeforeEach + public void setUp() { + userRepository.deleteAll(); + foodRepository.deleteAll(); + favoriteFoodRepository.deleteAll(); + } + + @Test + void testSaveAndGetFood() { // 음식 정보 저장 및 업데이트 + // given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1,180, 80,18)); + + //when + Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition)); + Food testFood = foodRepository.getReferenceById(foodId); + + List responseFoodDto = foodService.getFoodListByDate(userId, testFood.getDate()); + + assertNotNull(responseFoodDto); + assertEquals("testFood",responseFoodDto.get(0).getName()); + } + + +} From 564a7e95c7a66eef119c4ee3a813f8dfac37db9d Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 20:49:43 +0900 Subject: [PATCH 05/22] =?UTF-8?q?:sparkles:=20feat:=20=EC=9D=8C=EC=8B=9D?= =?UTF-8?q?=20Update=20=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diareat/diareat/food/service/FoodService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 29fa112..88562e0 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -3,6 +3,7 @@ import com.diareat.diareat.food.domain.Food; import com.diareat.diareat.food.dto.CreateFoodDto; import com.diareat.diareat.food.dto.ResponseFoodDto; +import com.diareat.diareat.food.dto.UpdateFoodDto; import com.diareat.diareat.food.repository.FavoriteFoodRepository; import com.diareat.diareat.food.repository.FoodRepository; import com.diareat.diareat.user.domain.User; @@ -47,8 +48,11 @@ public List getFoodListByDate(Long userId, LocalDate date){ // 음식 정보 수정 - public void updateFood() { - + @Transactional + public void updateFood(Long userId, UpdateFoodDto updateFoodDto) { + Food food = foodRepository.findById(updateFoodDto.getFoodId()) + .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + food.updateFood(updateFoodDto.getName(), updateFoodDto.getBaseNutrition()); } // 음식 삭제 From a6117b6f2352dd2966b0a21a13e0b6217c36c533 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 20:50:34 +0900 Subject: [PATCH 06/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20feat:=20updateFo?= =?UTF-8?q?od=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/food/service/FoodService.java | 2 +- .../diareat/service/FoodServiceTest.java | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 88562e0..feb9444 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -49,7 +49,7 @@ public List getFoodListByDate(Long userId, LocalDate date){ // 음식 정보 수정 @Transactional - public void updateFood(Long userId, UpdateFoodDto updateFoodDto) { + public void updateFood(UpdateFoodDto updateFoodDto) { Food food = foodRepository.findById(updateFoodDto.getFoodId()) .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); food.updateFood(updateFoodDto.getName(), updateFoodDto.getBaseNutrition()); diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 2dc8ec6..381dee2 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -3,6 +3,7 @@ import com.diareat.diareat.food.domain.Food; import com.diareat.diareat.food.dto.CreateFoodDto; import com.diareat.diareat.food.dto.ResponseFoodDto; +import com.diareat.diareat.food.dto.UpdateFoodDto; import com.diareat.diareat.food.repository.FavoriteFoodRepository; import com.diareat.diareat.food.repository.FoodRepository; import com.diareat.diareat.food.service.FoodService; @@ -56,7 +57,7 @@ public void setUp() { } @Test - void testSaveAndGetFood() { // 음식 정보 저장 및 업데이트 + void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리스트 불러오기 // given BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 1,180, 80,18)); @@ -71,5 +72,25 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 업데이트 assertEquals("testFood",responseFoodDto.get(0).getName()); } + @Test + void testUpdateFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); + + //when + BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); + foodService.updateFood(UpdateFoodDto.of(foodId, "testChangedFood", testChangedBaseNutrition)); + + Food changedFood = foodRepository.getReferenceById(foodId); + + assertNotNull(changedFood); + assertEquals("testChangedFood", changedFood.getName()); + assertEquals(2,changedFood.getBaseNutrition().getKcal()); + assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); + assertEquals(4,changedFood.getBaseNutrition().getProtein()); + assertEquals(5,changedFood.getBaseNutrition().getFat()); + } } From bce3547309d4f43729d98d6df25bed800b3a12b4 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 21:01:41 +0900 Subject: [PATCH 07/22] =?UTF-8?q?:sparkles:=20feat:=20deleteFood=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/diareat/diareat/food/service/FoodService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index feb9444..0d6290d 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -56,8 +56,9 @@ public void updateFood(UpdateFoodDto updateFoodDto) { } // 음식 삭제 - public void deleteFood() { - + @Transactional + public void deleteFood(Long foodId) { + foodRepository.deleteById(foodId); } // 즐겨찾기에 음식 저장 From 4bdd15a6a5d19d9d887f82131ed62156b6846591 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 21:02:05 +0900 Subject: [PATCH 08/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20feat:=20deleteFo?= =?UTF-8?q?od()=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diareat/diareat/service/FoodServiceTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 381dee2..8231670 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -93,4 +93,16 @@ void testUpdateFood() { assertEquals(5,changedFood.getBaseNutrition().getFat()); } + @Test + void testDeleteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); + + //when + foodService.deleteFood(foodId); + + assertNull(foodRepository.findById(foodId).orElse(null)); + } } From e256d43abde1ef53dc04579cd113c14c1b5cf14f Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:17:37 +0900 Subject: [PATCH 09/22] =?UTF-8?q?:recycle:=20refactor:=20ResponseFavoriteF?= =?UTF-8?q?oodDto=EC=97=90=EC=84=9C=20baseNutrition=20=EC=9E=90=EB=A3=8C?= =?UTF-8?q?=ED=98=95=20=EC=88=98=EC=A0=95=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/diareat/food/dto/ResponseFavoriteFoodDto.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/dto/ResponseFavoriteFoodDto.java b/src/main/java/com/diareat/diareat/food/dto/ResponseFavoriteFoodDto.java index 78678bb..3d33ca4 100644 --- a/src/main/java/com/diareat/diareat/food/dto/ResponseFavoriteFoodDto.java +++ b/src/main/java/com/diareat/diareat/food/dto/ResponseFavoriteFoodDto.java @@ -1,6 +1,7 @@ package com.diareat.diareat.food.dto; import com.diareat.diareat.food.domain.FavoriteFood; +import com.diareat.diareat.user.domain.BaseNutrition; import lombok.AllArgsConstructor; import lombok.Getter; @@ -10,10 +11,10 @@ public class ResponseFavoriteFoodDto { private Long favoriteFoodId; private String name; - private String baseNutrition; + private BaseNutrition baseNutrition; private int count; - public static ResponseFavoriteFoodDto of(Long favoriteFoodId, String name, String baseNutrition, int count) { + public static ResponseFavoriteFoodDto of(Long favoriteFoodId, String name, BaseNutrition baseNutrition, int count) { return new ResponseFavoriteFoodDto(favoriteFoodId, name, baseNutrition, count); } @@ -21,7 +22,7 @@ public static ResponseFavoriteFoodDto from(FavoriteFood favoriteFood) { return new ResponseFavoriteFoodDto( favoriteFood.getId(), favoriteFood.getName(), - favoriteFood.getBaseNutrition().toString(), + favoriteFood.getBaseNutrition(), favoriteFood.getCount() ); } From 89eaf285e90bc3daa1ebb5ef8f005bbb3ffe346c Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:18:55 +0900 Subject: [PATCH 10/22] =?UTF-8?q?:sparkles:=20feat:=20=EC=A6=90=EA=B2=A8?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0=20=EC=9D=8C=EC=8B=9D=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=B0=8F=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B0=98=ED=99=98=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/FavoriteFoodRepository.java | 3 +++ .../diareat/food/service/FoodService.java | 22 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/repository/FavoriteFoodRepository.java b/src/main/java/com/diareat/diareat/food/repository/FavoriteFoodRepository.java index d90278b..549a7cb 100644 --- a/src/main/java/com/diareat/diareat/food/repository/FavoriteFoodRepository.java +++ b/src/main/java/com/diareat/diareat/food/repository/FavoriteFoodRepository.java @@ -3,5 +3,8 @@ import com.diareat.diareat.food.domain.FavoriteFood; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface FavoriteFoodRepository extends JpaRepository { + List findAllByUserId(Long userId); } diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 0d6290d..e0dc36e 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -1,9 +1,8 @@ package com.diareat.diareat.food.service; +import com.diareat.diareat.food.domain.FavoriteFood; import com.diareat.diareat.food.domain.Food; -import com.diareat.diareat.food.dto.CreateFoodDto; -import com.diareat.diareat.food.dto.ResponseFoodDto; -import com.diareat.diareat.food.dto.UpdateFoodDto; +import com.diareat.diareat.food.dto.*; import com.diareat.diareat.food.repository.FavoriteFoodRepository; import com.diareat.diareat.food.repository.FoodRepository; import com.diareat.diareat.user.domain.User; @@ -62,8 +61,21 @@ public void deleteFood(Long foodId) { } // 즐겨찾기에 음식 저장 - public Long saveFavoriteFood() { - return null; + public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { + User user = userRepository.findById(createFavoriteFoodDto.getUserId()) + .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + + FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood(createFavoriteFoodDto.getName(), user, createFavoriteFoodDto.getBaseNutrition()); + return favoriteFoodRepository.save(favoriteFood).getId(); + } + + //즐겨찾기 음식 리스트 반환 + @Transactional + public List getFavoriteFoodByUserId(Long userId){ + List foodList = favoriteFoodRepository.findAllByUserId(userId); + return foodList.stream() + .map(favoriteFood -> ResponseFavoriteFoodDto.of(favoriteFood.getId(), favoriteFood.getName(), + favoriteFood.getBaseNutrition(), favoriteFood.getCount())).collect(Collectors.toList()); } // 즐겨찾기 음식 수정 From 52e2aa0b16c4d2a72d2c07cdc6b68630c30bbe30 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:19:18 +0900 Subject: [PATCH 11/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=EC=A6=90?= =?UTF-8?q?=EA=B2=A8=EC=B0=BE=EA=B8=B0=20=EC=9D=8C=EC=8B=9D=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EB=B0=8F=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/food/service/FoodService.java | 1 + .../diareat/service/FoodServiceTest.java | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index e0dc36e..6740136 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -61,6 +61,7 @@ public void deleteFood(Long foodId) { } // 즐겨찾기에 음식 저장 + @Transactional public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { User user = userRepository.findById(createFavoriteFoodDto.getUserId()) .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 8231670..48a5850 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -1,9 +1,8 @@ package com.diareat.diareat.service; +import com.diareat.diareat.food.domain.FavoriteFood; import com.diareat.diareat.food.domain.Food; -import com.diareat.diareat.food.dto.CreateFoodDto; -import com.diareat.diareat.food.dto.ResponseFoodDto; -import com.diareat.diareat.food.dto.UpdateFoodDto; +import com.diareat.diareat.food.dto.*; import com.diareat.diareat.food.repository.FavoriteFoodRepository; import com.diareat.diareat.food.repository.FoodRepository; import com.diareat.diareat.food.service.FoodService; @@ -66,10 +65,10 @@ void testSaveAndGetFood() { // 음식 정보 저장 및 해당 날짜 음식 리 Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood",testBaseNutrition)); Food testFood = foodRepository.getReferenceById(foodId); - List responseFoodDto = foodService.getFoodListByDate(userId, testFood.getDate()); + List responseFoodDtoList = foodService.getFoodListByDate(userId, testFood.getDate()); - assertNotNull(responseFoodDto); - assertEquals("testFood",responseFoodDto.get(0).getName()); + assertNotNull(responseFoodDtoList); + assertEquals("testFood",responseFoodDtoList.get(0).getName()); } @Test @@ -105,4 +104,20 @@ void testDeleteFood() { assertNull(foodRepository.findById(foodId).orElse(null)); } + + @Test + void testSaveAndGetFavoriteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); + + //when + Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); + + List responseFavoriteFoodDtoList = foodService.getFavoriteFoodByUserId(userId); + + assertNotNull(responseFavoriteFoodDtoList); + assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); + } } From 8d0abe6b1fa8db4b75e01ac42cad29f61cbe288e Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:25:31 +0900 Subject: [PATCH 12/22] =?UTF-8?q?:sparkles:=20feat:=20update,=20deleteFavo?= =?UTF-8?q?riteFood()=20=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/diareat/food/service/FoodService.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 6740136..027600c 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -80,13 +80,17 @@ public List getFavoriteFoodByUserId(Long userId){ } // 즐겨찾기 음식 수정 - public void updateFavoriteFood() { - + @Transactional + public void updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto) { + FavoriteFood food = favoriteFoodRepository.findById(updateFavoriteFoodDto.getFavoriteFoodId()) + .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + food.updateFavoriteFood(updateFavoriteFoodDto.getName(), updateFavoriteFoodDto.getBaseNutrition()); } // 즐겨찾기 해제 - public void deleteFavoriteFood() { - + @Transactional + public void deleteFavoriteFood(Long favoriteFoodId) { + favoriteFoodRepository.deleteById(favoriteFoodId); } From 60e1347236845bb17c121e3cfd19ba328f5463fd Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:25:55 +0900 Subject: [PATCH 13/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20update,=20delete?= =?UTF-8?q?FavoriteFood()=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/service/FoodServiceTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 48a5850..624cc46 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -120,4 +120,41 @@ void testSaveAndGetFavoriteFood() { assertNotNull(responseFavoriteFoodDtoList); assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); } + + @Test + void testUpdateFavoriteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); + Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); + + + //when + BaseNutrition testChangedBaseNutrition = BaseNutrition.createNutrition(2,3,4,5); + foodService.updateFavoriteFood(UpdateFavoriteFoodDto.of(favoriteFoodId, "testChangedFood", testChangedBaseNutrition)); + + FavoriteFood changedFood = favoriteFoodRepository.getReferenceById(favoriteFoodId); + + assertNotNull(changedFood); + assertEquals("testChangedFood", changedFood.getName()); + assertEquals(2,changedFood.getBaseNutrition().getKcal()); + assertEquals(3,changedFood.getBaseNutrition().getCarbohydrate()); + assertEquals(4,changedFood.getBaseNutrition().getProtein()); + assertEquals(5,changedFood.getBaseNutrition().getFat()); + } + + @Test + void testDeleteFavoriteFood() { + //given + BaseNutrition testBaseNutrition = BaseNutrition.createNutrition(1,1,1,1); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "tessPassword", 1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId, "testFood", testBaseNutrition)); + Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); + + //when + foodService.deleteFavoriteFood(favoriteFoodId); + + assertNull(favoriteFoodRepository.findById(favoriteFoodId).orElse(null)); + } } From e6b33d81ea644e12d71bcd72f06a509e159d1b7d Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Wed, 4 Oct 2023 23:28:16 +0900 Subject: [PATCH 14/22] :recycle: refactor: rename getFavoriteFoodByUserId (#9) to getFavoriteFoodList --- src/main/java/com/diareat/diareat/food/service/FoodService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 027600c..9b058b6 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -72,7 +72,7 @@ public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { //즐겨찾기 음식 리스트 반환 @Transactional - public List getFavoriteFoodByUserId(Long userId){ + public List getFavoriteFoodList(Long userId){ List foodList = favoriteFoodRepository.findAllByUserId(userId); return foodList.stream() .map(favoriteFood -> ResponseFavoriteFoodDto.of(favoriteFood.getId(), favoriteFood.getName(), From 75ce22b05541acfcaf7e04162ecc881ccf3c2451 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 00:00:45 +0900 Subject: [PATCH 15/22] =?UTF-8?q?:sparkles:=20feat:=20=EC=9E=84=EC=8B=9C?= =?UTF-8?q?=20=EB=82=A0=EC=A7=9C=EB=B3=84=20=EC=98=81=EC=96=91=EC=86=8C=20?= =?UTF-8?q?=ED=95=A9=20dto=20=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/ResponseNutritionSumByDateDto.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java diff --git a/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java b/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java new file mode 100644 index 0000000..0229a24 --- /dev/null +++ b/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java @@ -0,0 +1,22 @@ +package com.diareat.diareat.food.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseNutritionSumByDateDto { + int totalKcal; + int totalCarbohydrate; + int totalProtein; + int totalFat; + + double ratioKcal; + double ratioCarbohydrate; + double ratioProtein; + double ratioFat; + + public static ResponseNutritionSumByDateDto of (int totalKcal, int totalCarbohydrate, int totalProtein, int totalFat, double ratioKcal, double ratioCarbohydrate, double ratioProtein, double ratioFat){ + return new ResponseNutritionSumByDateDto(totalFat, totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat); + } +} From db38b0398e541d84d9a71584ce0c71de903817fc Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 00:01:11 +0900 Subject: [PATCH 16/22] =?UTF-8?q?:sparkles:=20=EB=82=A0=EC=A7=9C=EB=B3=84?= =?UTF-8?q?=20=EB=A8=B9=EC=9D=80=20=EC=9D=8C=EC=8B=9D=EB=93=A4=EC=9D=98=20?= =?UTF-8?q?=EC=98=81=EC=96=91=EC=84=B1=EB=B6=84=EB=B3=84=20=EC=B4=9D?= =?UTF-8?q?=ED=95=A9=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/food/service/FoodService.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 9b058b6..ee5a110 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -5,6 +5,7 @@ import com.diareat.diareat.food.dto.*; import com.diareat.diareat.food.repository.FavoriteFoodRepository; 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.ResponseResearchUserDto; import com.diareat.diareat.user.repository.UserRepository; @@ -17,6 +18,7 @@ import javax.transaction.Transactional; import java.time.LocalDate; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @RequiredArgsConstructor @@ -93,6 +95,57 @@ public void deleteFavoriteFood(Long favoriteFoodId) { favoriteFoodRepository.deleteById(favoriteFoodId); } + @Transactional + // 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) + public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) { + List foodList = foodRepository.findAllByUserIdAndDate(userId, date); + User targetUser = userRepository.findById(userId) + .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + int totalKcal = 0; + int totalCarbohydrate = 0; + int totalProtein = 0; + int totalFat = 0; + + for (Food food : foodList) { + BaseNutrition targetFoodNutrition = food.getBaseNutrition(); + totalKcal += targetFoodNutrition.getKcal(); + totalCarbohydrate += targetFoodNutrition.getCarbohydrate(); + totalProtein += targetFoodNutrition.getProtein(); + totalFat += targetFoodNutrition.getFat(); + } + + double ratioKcal = Math.round((totalKcal*1.0)/(targetUser.getBaseNutrition().getKcal()*1.0))*10.0; + double ratioCarbohydrate = Math.round((totalCarbohydrate*1.0)/(targetUser.getBaseNutrition().getCarbohydrate()*1.0))*10.0; + double ratioProtein = Math.round((totalProtein*1.0)/(targetUser.getBaseNutrition().getProtein()*1.0))*10.0; + double ratioFat = Math.round((totalFat*1.0)/(targetUser.getBaseNutrition().getFat()*1.0))*10.0; + + return ResponseNutritionSumByDateDto.of(totalKcal,totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat); + } + + @Transactional + // 유저의 최근 7일간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) + public void getNutritionSumByWeek(Long userId) { + + } + + @Transactional + // 유저의 최근 1개월간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) + public void getNutritionSumByMonth(Long userId) { + + } + + @Transactional + // 유저의 최근 7일간의 Best 3 음식 조회 (dto 구체적 협의 필요) + public void getBestFoodByWeek(Long userId) { + + } + + @Transactional + // 유저의 최근 7일간의 Worst 3 음식 조회 (dto 구체적 협의 필요) + public void getWorstFoodByWeek(Long userId) { + + } + /** * 메서드 구현 유의사항 From 81c437aabca331e39206afea7bfbc654b4f6599f Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 00:12:25 +0900 Subject: [PATCH 17/22] :pencil2: fix: totalFat -> totalKcal (#9) found on unit test --- .../diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java b/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java index 0229a24..5eb8e4c 100644 --- a/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java +++ b/src/main/java/com/diareat/diareat/food/dto/ResponseNutritionSumByDateDto.java @@ -17,6 +17,6 @@ public class ResponseNutritionSumByDateDto { double ratioFat; public static ResponseNutritionSumByDateDto of (int totalKcal, int totalCarbohydrate, int totalProtein, int totalFat, double ratioKcal, double ratioCarbohydrate, double ratioProtein, double ratioFat){ - return new ResponseNutritionSumByDateDto(totalFat, totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat); + return new ResponseNutritionSumByDateDto(totalKcal, totalCarbohydrate, totalProtein, totalFat, ratioKcal, ratioCarbohydrate, ratioProtein, ratioFat); } } From bd5c2b9998d22a24ab0fa9f5fa1ed155318399a8 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 00:15:14 +0900 Subject: [PATCH 18/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20feat:=20?= =?UTF-8?q?=EB=82=A0=EC=A7=9C=EB=B3=84=20=EC=98=81=EC=96=91=EC=86=8C=20?= =?UTF-8?q?=ED=95=A9=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/service/FoodServiceTest.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 624cc46..4121d4a 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -115,7 +115,7 @@ void testSaveAndGetFavoriteFood() { //when Long favoriteFoodId = foodService.saveFavoriteFood(CreateFavoriteFoodDto.of(foodId, userId, "testFood", testBaseNutrition)); - List responseFavoriteFoodDtoList = foodService.getFavoriteFoodByUserId(userId); + List responseFavoriteFoodDtoList = foodService.getFavoriteFoodList(userId); assertNotNull(responseFavoriteFoodDtoList); assertEquals("testFood",responseFavoriteFoodDtoList.get(0).getName()); @@ -157,4 +157,28 @@ void testDeleteFavoriteFood() { assertNull(favoriteFoodRepository.findById(favoriteFoodId).orElse(null)); } + + @Test + void testNutritionSumByDate(){ + //given + BaseNutrition testFoodNutrition = BaseNutrition.createNutrition(100,150,200,250); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword",1, 180, 80, 18)); + Long foodId = foodService.saveFood(CreateFoodDto.of(userId,"testFood", testFoodNutrition)); + Food food = foodRepository.getReferenceById(foodId); + + //when + ResponseNutritionSumByDateDto responseNutritionSumByDateDto = foodService.getNutritionSumByDate(userId,food.getDate()); + + assertNotNull(responseNutritionSumByDateDto); + assertEquals(100, responseNutritionSumByDateDto.getTotalKcal()); + assertEquals(150, responseNutritionSumByDateDto.getTotalCarbohydrate()); + assertEquals(200, responseNutritionSumByDateDto.getTotalProtein()); + assertEquals(250, responseNutritionSumByDateDto.getTotalFat()); + + assertEquals(Math.round((100*1.0)/(2000*1.0))*10.0, responseNutritionSumByDateDto.getRatioKcal()); + assertEquals(Math.round((150*1.0)/(300*1.0))*10.0, responseNutritionSumByDateDto.getRatioCarbohydrate()); + assertEquals(Math.round((200*1.0)/(80*1.0))*10.0, responseNutritionSumByDateDto.getRatioProtein()); + assertEquals(Math.round((250*1.0)/(80*1.0))*10.0, responseNutritionSumByDateDto.getRatioFat()); + + } } From 2d484559dce1fc561c2293e59a2351ddfbcdba35 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 00:50:47 +0900 Subject: [PATCH 19/22] =?UTF-8?q?:green=5Fheart:=20fix:=20application-db.p?= =?UTF-8?q?roperties=20=EB=B3=B5=EA=B5=AC=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +-- src/main/resources/application-db.properties | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/application-db.properties diff --git a/.gitignore b/.gitignore index 88dbad9..e48b6be 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,4 @@ out/ /.nb-gradle/ ### VS Code ### -.vscode/ -application-db.properties \ No newline at end of file +.vscode/ \ No newline at end of file diff --git a/src/main/resources/application-db.properties b/src/main/resources/application-db.properties new file mode 100644 index 0000000..ee08c3d --- /dev/null +++ b/src/main/resources/application-db.properties @@ -0,0 +1,11 @@ +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.hibernate.ddl-auto=create + +# Remote DB +spring.datasource.url=${DB_ENDPOINT} +spring.datasource.username=${DB_USERNAME} +spring.datasource.password=${DB_PASSWORD} + +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file From b9c60acccdb249efc7a8801365cb0e15b7074a1d Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 10:31:54 +0900 Subject: [PATCH 20/22] =?UTF-8?q?:white=5Fcheck=5Fmark:=20fix:=20UserServi?= =?UTF-8?q?ceTest=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/diareat/diareat/service/UserServiceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index 700c92b..6f5ba89 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -36,10 +36,9 @@ public void setUp() { @Test void testSaveAndGetUserInfo() { // 회원정보 저장 및 조회 // given - Long userId = 1L; // when - userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); ResponseUserDto responseUserDto = userService.getUserInfo(userId); // 검증: 올바른 결과를 반환하는지 확인 From 6646330a31a90babbd0bf46104401a7944bc73ba Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 10:47:20 +0900 Subject: [PATCH 21/22] =?UTF-8?q?:recycle:=20refactor:=20ResponseUserDto?= =?UTF-8?q?=EC=97=90=20id=20=EC=86=8D=EC=84=B1=20=EC=A0=9C=EA=B1=B0=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/diareat/diareat/user/dto/ResponseUserDto.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java index 7d2e746..ecdb851 100644 --- a/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java @@ -9,7 +9,6 @@ @AllArgsConstructor public class ResponseUserDto { - private Long id; //누락된 id private String name; private int height; private int weight; @@ -18,11 +17,11 @@ public class ResponseUserDto { private BaseNutrition baseNutrition; - public static ResponseUserDto of(Long id, String userName, int userHeight, int userWeight, int userGender, int userAge, BaseNutrition userBaseNutrition) { - return new ResponseUserDto(id, userName, userHeight, userWeight, userGender, userAge, userBaseNutrition); + public static ResponseUserDto of(String userName, int userHeight, int userWeight, int userGender, int userAge, BaseNutrition userBaseNutrition) { + return new ResponseUserDto(userName, userHeight, userWeight, userGender, userAge, userBaseNutrition); } public static ResponseUserDto from(User user) { - return new ResponseUserDto(user.getId(), user.getName(), user.getHeight(), user.getWeight(), user.getGender(), user.getAge(), user.getBaseNutrition()); + return new ResponseUserDto(user.getName(), user.getHeight(), user.getWeight(), user.getGender(), user.getAge(), user.getBaseNutrition()); } } From e48cf9683ef68de9d7ccb02dbf79343ffafd36d4 Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 5 Oct 2023 18:39:00 +0900 Subject: [PATCH 22/22] =?UTF-8?q?:recycle:=20refactor:=20Transactional=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=A4=91=EB=B3=B5=EB=90=9C=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=ED=95=A8=EC=88=98=ED=99=94=20(#9?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/food/service/FoodService.java | 41 +++++++++++-------- .../diareat/service/FoodServiceTest.java | 8 ---- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index ee5a110..f2db24e 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -7,18 +7,16 @@ 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.ResponseResearchUserDto; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.util.api.ResponseCode; import com.diareat.diareat.util.exception.FoodException; import com.diareat.diareat.util.exception.UserException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; -import javax.transaction.Transactional; import java.time.LocalDate; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; @RequiredArgsConstructor @@ -33,14 +31,13 @@ public class FoodService { // 촬영 후, 음식 정보 저장 @Transactional public Long saveFood(CreateFoodDto createFoodDto) { - User user = userRepository.findById(createFoodDto.getUserId()) - .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + User user = getUserById(createFoodDto.getUserId()); Food food = Food.createFood(createFoodDto.getName(), user, createFoodDto.getBaseNutrition()); return foodRepository.save(food).getId(); } // 회원이 특정 날짜에 먹은 음식 반환 - @Transactional + @Transactional(readOnly = true) public List getFoodListByDate(Long userId, LocalDate date){ List foodList = foodRepository.findAllByUserIdAndDate(userId, date); return foodList.stream() @@ -51,8 +48,7 @@ public List getFoodListByDate(Long userId, LocalDate date){ // 음식 정보 수정 @Transactional public void updateFood(UpdateFoodDto updateFoodDto) { - Food food = foodRepository.findById(updateFoodDto.getFoodId()) - .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + Food food = getFoodById(updateFoodDto.getFoodId()); food.updateFood(updateFoodDto.getName(), updateFoodDto.getBaseNutrition()); } @@ -65,15 +61,15 @@ public void deleteFood(Long foodId) { // 즐겨찾기에 음식 저장 @Transactional public Long saveFavoriteFood(CreateFavoriteFoodDto createFavoriteFoodDto) { - User user = userRepository.findById(createFavoriteFoodDto.getUserId()) - .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + + User user = getUserById(createFavoriteFoodDto.getUserId()); FavoriteFood favoriteFood = FavoriteFood.createFavoriteFood(createFavoriteFoodDto.getName(), user, createFavoriteFoodDto.getBaseNutrition()); return favoriteFoodRepository.save(favoriteFood).getId(); } //즐겨찾기 음식 리스트 반환 - @Transactional + @Transactional(readOnly = true) public List getFavoriteFoodList(Long userId){ List foodList = favoriteFoodRepository.findAllByUserId(userId); return foodList.stream() @@ -84,8 +80,7 @@ public List getFavoriteFoodList(Long userId){ // 즐겨찾기 음식 수정 @Transactional public void updateFavoriteFood(UpdateFavoriteFoodDto updateFavoriteFoodDto) { - FavoriteFood food = favoriteFoodRepository.findById(updateFavoriteFoodDto.getFavoriteFoodId()) - .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + FavoriteFood food = getFavoriteFoodById(updateFavoriteFoodDto.getFavoriteFoodId()); food.updateFavoriteFood(updateFavoriteFoodDto.getName(), updateFavoriteFoodDto.getBaseNutrition()); } @@ -95,12 +90,11 @@ public void deleteFavoriteFood(Long favoriteFoodId) { favoriteFoodRepository.deleteById(favoriteFoodId); } - @Transactional + @Transactional(readOnly = true) // 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) { List foodList = foodRepository.findAllByUserIdAndDate(userId, date); - User targetUser = userRepository.findById(userId) - .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + User targetUser = getUserById(userId); int totalKcal = 0; int totalCarbohydrate = 0; int totalProtein = 0; @@ -146,6 +140,21 @@ public void getWorstFoodByWeek(Long userId) { } + private User getUserById(Long userId){ + return userRepository.findById(userId) + .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); + } + + private Food getFoodById(Long foodId){ + return foodRepository.findById(foodId) + .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + } + + private FavoriteFood getFavoriteFoodById(Long foodId){ + return favoriteFoodRepository.findById(foodId) + .orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND)); + } + /** * 메서드 구현 유의사항 diff --git a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java index 4121d4a..b633575 100644 --- a/src/test/java/com/diareat/diareat/service/FoodServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/FoodServiceTest.java @@ -7,24 +7,16 @@ import com.diareat.diareat.food.repository.FoodRepository; import com.diareat.diareat.food.service.FoodService; import com.diareat.diareat.user.domain.BaseNutrition; -import com.diareat.diareat.user.domain.User; import com.diareat.diareat.user.dto.CreateUserDto; -import com.diareat.diareat.user.dto.ResponseResearchUserDto; -import com.diareat.diareat.user.dto.ResponseUserDto; -import com.diareat.diareat.user.dto.UpdateUserDto; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.user.service.UserService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.logging.log4j2.Log4J2LoggingSystem; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; -import javax.swing.text.html.Option; -import java.time.LocalDate; import java.util.List; -import java.util.Optional; import static org.junit.jupiter.api.Assertions.*;