From 5acb0d1879baee797324377fb8bf89105671c609 Mon Sep 17 00:00:00 2001 From: CHAE Date: Mon, 9 Oct 2023 19:38:46 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E2=9C=A8Feat:=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=95=84=20=EA=B8=B0=EB=8A=A5=20=EA=B4=80=EB=A0=A8=20dto=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/dto/UpdateUserDto.java | 7 +++---- .../user/dto/UpdateUserNutritionDto.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/diareat/diareat/user/dto/UpdateUserNutritionDto.java diff --git a/src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java b/src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java index a882577..1a643d4 100644 --- a/src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java @@ -1,6 +1,5 @@ package com.diareat.diareat.user.dto; -import com.diareat.diareat.user.domain.BaseNutrition; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -15,9 +14,9 @@ public class UpdateUserDto { private int height; private int weight; private int age; - private BaseNutrition baseNutrition; + private boolean isAutoUpdateNutrition; // 개인정보를 활용한 기준 영양소 자동계산 여부 - public static UpdateUserDto of(Long userId, String userName, int userHeight, int userWeight, int userAge, BaseNutrition userBaseNutrition) { - return new UpdateUserDto(userId, userName, userHeight, userWeight, userAge, userBaseNutrition); + public static UpdateUserDto of(Long userId, String userName, int userHeight, int userWeight, int userAge, boolean isAutoUpdateNutrition) { + return new UpdateUserDto(userId, userName, userHeight, userWeight, userAge, isAutoUpdateNutrition); } } diff --git a/src/main/java/com/diareat/diareat/user/dto/UpdateUserNutritionDto.java b/src/main/java/com/diareat/diareat/user/dto/UpdateUserNutritionDto.java new file mode 100644 index 0000000..a5e6aee --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/UpdateUserNutritionDto.java @@ -0,0 +1,21 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class UpdateUserNutritionDto { + + private Long userId; + private int calorie; + private int carbohydrate; + private int protein; + private int fat; + + public static UpdateUserNutritionDto of(Long userId, int calorie, int carbohydrate, int protein, int fat) { + return new UpdateUserNutritionDto(userId, calorie, carbohydrate, protein, fat); + } +} From 5b5fffcf9423d6b6a38f217c1597f3f389663957 Mon Sep 17 00:00:00 2001 From: CHAE Date: Mon, 9 Oct 2023 19:56:07 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=E2=9C=A8Feat:=20=EC=B9=9C=EA=B5=AC=20?= =?UTF-8?q?=EA=B4=80=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?dto=20=EC=B6=94=EA=B0=80=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/dto/FollowUserDto.java | 14 ++++++++++++++ .../user/dto/ResponseSearchUserDto.java | 17 +++++++++++++++++ .../diareat/user/dto/SearchUserDto.java | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java create mode 100644 src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java create mode 100644 src/main/java/com/diareat/diareat/user/dto/SearchUserDto.java diff --git a/src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java b/src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java new file mode 100644 index 0000000..336a337 --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java @@ -0,0 +1,14 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class FollowUserDto { + + private Long userId; + private Long followUserId; +} diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java new file mode 100644 index 0000000..bb2972c --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java @@ -0,0 +1,17 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseSearchUserDto { + + private Long userId; + private String name; + private String image; + + public static ResponseSearchUserDto of(Long userId, String name, String image) { + return new ResponseSearchUserDto(userId, name, image); + } +} diff --git a/src/main/java/com/diareat/diareat/user/dto/SearchUserDto.java b/src/main/java/com/diareat/diareat/user/dto/SearchUserDto.java new file mode 100644 index 0000000..d6aa5db --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/SearchUserDto.java @@ -0,0 +1,18 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class SearchUserDto { + + private Long userId; + private String inputName; + + public static SearchUserDto of(Long userId, String inputName) { + return new SearchUserDto(userId, inputName); + } +} From 5c3c8eadcdb8b73e77b6fd1b4a8a83508c1011a4 Mon Sep 17 00:00:00 2001 From: CHAE Date: Mon, 9 Oct 2023 20:44:29 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E2=9C=A8Feat:=20=ED=8C=94=EB=A1=9C?= =?UTF-8?q?=EC=9A=B0=20=EA=B8=B0=EB=8A=A5=20User=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=ED=95=98=EC=97=AC=20=EA=B5=AC=ED=98=84=20(#1?= =?UTF-8?q?9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/diareat/user/domain/Follow.java | 37 +++++++++++++++++++ .../com/diareat/diareat/user/domain/User.java | 26 ++++--------- .../user/repository/FollowRepository.java | 14 +++++++ 3 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/diareat/diareat/user/domain/Follow.java create mode 100644 src/main/java/com/diareat/diareat/user/repository/FollowRepository.java diff --git a/src/main/java/com/diareat/diareat/user/domain/Follow.java b/src/main/java/com/diareat/diareat/user/domain/Follow.java new file mode 100644 index 0000000..991834e --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/domain/Follow.java @@ -0,0 +1,37 @@ +package com.diareat.diareat.user.domain; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.io.Serializable; + +@NoArgsConstructor +@IdClass(Follow.PK.class) // 복합키를 위한 어노테이션 +@Getter +@Table(uniqueConstraints = { + @UniqueConstraint(columnNames = {"to_user", "from_user"}) +}) // 중복 팔로우 방지 +@Entity +public class Follow { + + @Id + @Column(name = "to_user", insertable = false, updatable = false) + private Long toUser; + + @Id + @Column(name = "from_user", insertable = false, updatable = false) + private Long fromUser; + + public static Follow makeFollow(Long toUser, Long fromUser) { + Follow follow = new Follow(); + follow.toUser = toUser; + follow.fromUser = fromUser; + return follow; + } + + public static class PK implements Serializable { // 복합키를 위한 클래스 + Long toUser; + Long fromUser; + } +} diff --git a/src/main/java/com/diareat/diareat/user/domain/User.java b/src/main/java/com/diareat/diareat/user/domain/User.java index f28c005..2f0d619 100644 --- a/src/main/java/com/diareat/diareat/user/domain/User.java +++ b/src/main/java/com/diareat/diareat/user/domain/User.java @@ -9,9 +9,7 @@ import javax.persistence.*; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @@ -27,6 +25,8 @@ public class User { @JsonIgnore private String keyCode; // 로그인 식별키 + private String image; // 프로필 사진 경로 + private int height; // 키 private int weight; // 몸무게 @@ -35,7 +35,7 @@ public class User { private int age; // 나이 - private BaseNutrition baseNutrition; // 영양소 + private BaseNutrition baseNutrition; // 기준영양소 @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 촬영한 음식도 삭제 private List foods = new ArrayList<>(); @@ -43,14 +43,6 @@ public class User { @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 즐겨찾기 음식도 삭제 private List favoriteFoods = new ArrayList<>(); - @ManyToMany // 팔로우 목록 - @JoinTable( - name = "user_follows", - joinColumns = @JoinColumn(name = "follower_id"), - inverseJoinColumns = @JoinColumn(name = "following_id") - ) - private Set followings = new HashSet<>(); - // 생성 메서드 public static User createUser(String name, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { User user = new User(); @@ -65,19 +57,15 @@ public static User createUser(String name, String keyCode, int height, int weigh } // 회원정보 수정 - public void updateUser(String name, int height, int weight, int age, BaseNutrition baseNutrition) { + public void updateUser(String name, int height, int weight, int age) { this.name = name; this.height = height; this.weight = weight; this.age = age; - this.baseNutrition = baseNutrition; } - public void followUser(User user) { - this.followings.add(user); - } - - public void unfollowUser(User user) { - this.followings.remove(user); + // 회원 기준영양소 직접 수정 + public void updateBaseNutrition(BaseNutrition baseNutrition) { + this.baseNutrition = baseNutrition; } } diff --git a/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java b/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java new file mode 100644 index 0000000..e5ff152 --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java @@ -0,0 +1,14 @@ +package com.diareat.diareat.user.repository; + +import com.diareat.diareat.user.domain.Follow; +import com.diareat.diareat.user.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface FollowRepository extends JpaRepository { + @Query(value = "select u from Follow f INNER JOIN User u ON f.toUser = u.id where f.fromUser = :userId") // 팔로우 목록 조회 + List findAllByFromUser(@Param("userId") Long userId); +} From 6fe33cd95a4eba6b80d2b7f7958224b575fbd257 Mon Sep 17 00:00:00 2001 From: CHAE Date: Mon, 9 Oct 2023 20:51:40 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=E2=99=BB=EF=B8=8FRefactor:=20=EA=B2=80?= =?UTF-8?q?=EC=83=89/=ED=94=84=EB=A1=9C=ED=95=84=20=EA=B8=B0=EB=8A=A5=20dt?= =?UTF-8?q?o=20=EC=88=98=EC=A0=95=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/dto/ResponseRankUserDto.java | 22 ++++++++++++++++++ .../user/dto/ResponseResearchUserDto.java | 16 ------------- .../user/dto/ResponseSearchUserDto.java | 5 ++-- .../user/dto/ResponseSimpleUserDto.java | 17 ++++++++++++++ .../diareat/user/dto/ResponseUserDto.java | 12 ++++------ .../user/dto/ResponseUserNutritionDto.java | 23 +++++++++++++++++++ .../user/repository/FollowRepository.java | 2 ++ 7 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/diareat/diareat/user/dto/ResponseRankUserDto.java delete mode 100644 src/main/java/com/diareat/diareat/user/dto/ResponseResearchUserDto.java create mode 100644 src/main/java/com/diareat/diareat/user/dto/ResponseSimpleUserDto.java create mode 100644 src/main/java/com/diareat/diareat/user/dto/ResponseUserNutritionDto.java diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseRankUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseRankUserDto.java new file mode 100644 index 0000000..c4c0700 --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseRankUserDto.java @@ -0,0 +1,22 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseRankUserDto { + + private Long userId; + private String name; + private String image; + private double calorieScore; + private double carbohydrateScore; + private double proteinScore; + private double fatScore; + private double totalScore; + + public static ResponseRankUserDto of(Long userId, String name, String image, double calorieScore, double carbohydrateScore, double proteinScore, double fatScore, double totalScore) { + return new ResponseRankUserDto(userId, name, image, calorieScore, carbohydrateScore, proteinScore, fatScore, totalScore); + } +} diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseResearchUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseResearchUserDto.java deleted file mode 100644 index 24aac2f..0000000 --- a/src/main/java/com/diareat/diareat/user/dto/ResponseResearchUserDto.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.diareat.diareat.user.dto; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class ResponseResearchUserDto { - - private Long userId; - private String name; - - public static ResponseResearchUserDto of(Long userId, String name) { - return new ResponseResearchUserDto(userId, name); - } -} diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java index bb2972c..ef4a23a 100644 --- a/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseSearchUserDto.java @@ -10,8 +10,9 @@ public class ResponseSearchUserDto { private Long userId; private String name; private String image; + private boolean isFollow; // 유저가 이미 팔로우한 유저인지 확인 - public static ResponseSearchUserDto of(Long userId, String name, String image) { - return new ResponseSearchUserDto(userId, name, image); + public static ResponseSearchUserDto of(Long userId, String name, String image, boolean isFollow) { + return new ResponseSearchUserDto(userId, name, image, isFollow); } } diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseSimpleUserDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseSimpleUserDto.java new file mode 100644 index 0000000..b56e7f6 --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseSimpleUserDto.java @@ -0,0 +1,17 @@ +package com.diareat.diareat.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseSimpleUserDto { + + private String name; + private String image; + private double nutritionScore; + + public static ResponseSimpleUserDto of(String name, String image, double nutritionScore) { + return new ResponseSimpleUserDto(name, image, nutritionScore); + } +} 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 ecdb851..dcda49f 100644 --- a/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java @@ -1,6 +1,5 @@ package com.diareat.diareat.user.dto; -import com.diareat.diareat.user.domain.BaseNutrition; import com.diareat.diareat.user.domain.User; import lombok.AllArgsConstructor; import lombok.Getter; @@ -12,16 +11,13 @@ public class ResponseUserDto { private String name; private int height; private int weight; - private int gender; 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 from(User user) { + return new ResponseUserDto(user.getName(), user.getHeight(), user.getWeight(), user.getAge()); } - public static ResponseUserDto from(User user) { - return new ResponseUserDto(user.getName(), user.getHeight(), user.getWeight(), user.getGender(), user.getAge(), user.getBaseNutrition()); + public static ResponseUserDto of(String name, int height, int weight, int age) { + return new ResponseUserDto(name, height, weight, age); } } diff --git a/src/main/java/com/diareat/diareat/user/dto/ResponseUserNutritionDto.java b/src/main/java/com/diareat/diareat/user/dto/ResponseUserNutritionDto.java new file mode 100644 index 0000000..a2db114 --- /dev/null +++ b/src/main/java/com/diareat/diareat/user/dto/ResponseUserNutritionDto.java @@ -0,0 +1,23 @@ +package com.diareat.diareat.user.dto; + +import com.diareat.diareat.user.domain.User; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ResponseUserNutritionDto { + + private int calorie; + private int carbohydrate; + private int protein; + private int fat; + + public static ResponseUserNutritionDto of(int calorie, int carbohydrate, int protein, int fat) { + return new ResponseUserNutritionDto(calorie, carbohydrate, protein, fat); + } + + public static ResponseUserNutritionDto from(User user) { + return new ResponseUserNutritionDto(user.getBaseNutrition().getKcal(), user.getBaseNutrition().getCarbohydrate(), user.getBaseNutrition().getProtein(), user.getBaseNutrition().getFat()); + } +} diff --git a/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java b/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java index e5ff152..cfd4843 100644 --- a/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java +++ b/src/main/java/com/diareat/diareat/user/repository/FollowRepository.java @@ -11,4 +11,6 @@ public interface FollowRepository extends JpaRepository { @Query(value = "select u from Follow f INNER JOIN User u ON f.toUser = u.id where f.fromUser = :userId") // 팔로우 목록 조회 List findAllByFromUser(@Param("userId") Long userId); + + boolean existsByFromUserAndToUser(Long fromUser, Long toUser); // 팔로우 여부 확인 } From 9908210f0f9a90ac233371a3d2190ccbcb452f4c Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 10 Oct 2023 11:48:11 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=E2=9C=A8=EF=B8=8FFeat:=20BaseNutrition=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=EC=98=81=EC=96=91=EC=86=8C=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EB=A1=9C=EC=A7=81=20=EC=9E=84=EC=8B=9C=20=EB=8F=84?= =?UTF-8?q?=EC=9E=85=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/domain/BaseNutrition.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/domain/BaseNutrition.java b/src/main/java/com/diareat/diareat/user/domain/BaseNutrition.java index ed23ef2..86719f9 100644 --- a/src/main/java/com/diareat/diareat/user/domain/BaseNutrition.java +++ b/src/main/java/com/diareat/diareat/user/domain/BaseNutrition.java @@ -26,11 +26,22 @@ public static BaseNutrition createNutrition(int kcal, int carbohydrate, int prot return baseNutrition; } - // 영양소 수정 - public void updateNutrition(int kcal, int carbohydrate, int protein, int fat) { - this.kcal = kcal; - this.carbohydrate = carbohydrate; - this.protein = protein; - this.fat = fat; + // 생성 메서드 by 개인정보 (성별, 나이, 키, 몸무게로 기준영양소 자동 계산 기능) + public static BaseNutrition calculateNutrition(int gender, int age, int height, int weight) { + BaseNutrition baseNutrition = new BaseNutrition(); + + // 임의의 식으로 기초대사량 계산하였으며, 추후 식약처 및 관련 기관에서 제공하는 공식으로 변경할 예정 + baseNutrition.kcal = (int) (66.47 + (13.75 * weight) + (5 * height) - (6.76 * age)); // 기초대사량 계산식 + baseNutrition.carbohydrate = (int) (baseNutrition.kcal * 0.65 / 4); // 탄수화물 65% + baseNutrition.protein = (int) (baseNutrition.kcal * 0.1 / 4); // 단백질 10% + baseNutrition.fat = (int) (baseNutrition.kcal * 0.25 / 9); + + if(gender == 1) { // 여성일 경우 + baseNutrition.kcal -= 161; + baseNutrition.carbohydrate -= 40; + baseNutrition.protein -= 5; + baseNutrition.fat -= 10; + } + return baseNutrition; } } From 352897dcaad2132532cd57f930a0c1da2291b3b1 Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 10 Oct 2023 12:00:53 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=E2=9C=A8=EF=B8=8FFeat:=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=ED=95=84/=EC=B9=9C=EA=B5=AC=EA=B4=80=EB=A6=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B4=80=EB=A0=A8=20UserService=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/user/service/UserService.java | 115 ++++++++---------- 1 file changed, 49 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index 1ebc485..ed90037 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -1,11 +1,10 @@ package com.diareat.diareat.user.service; import com.diareat.diareat.user.domain.BaseNutrition; +import com.diareat.diareat.user.domain.Follow; 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.dto.*; +import com.diareat.diareat.user.repository.FollowRepository; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.util.api.ResponseCode; import com.diareat.diareat.util.exception.UserException; @@ -21,109 +20,93 @@ public class UserService { private final UserRepository userRepository; + private final FollowRepository followRepository; // 회원정보 저장 @Transactional public Long saveUser(CreateUserDto createUserDto) { - // 나이, 성별, 키, 몸무게로 기준 영양소 계산 (일단 임의의 값으로 설정했고 추후 로직 구성) - BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); + BaseNutrition baseNutrition = BaseNutrition.createNutrition(0, 0, 0, 0); // 로직 확정 전에는 임시 코드로 대체 + // BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight()); User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getAge(), createUserDto.getGender(), baseNutrition); return userRepository.save(user).getId(); } + // 회원 기본정보 조회 + @Transactional(readOnly = true) + public ResponseSimpleUserDto getSimpleUserInfo(Long userId) { + User user = getUserById(userId); + double nutritionScore = 100; // 로직 확정 전에는 임시 값으로 대체 + return ResponseSimpleUserDto.of(user.getName(), user.getImage(), nutritionScore); + } + // 회원정보 조회 @Transactional(readOnly = true) public ResponseUserDto getUserInfo(Long userId) { - return ResponseUserDto.from(getUserById(userId)); + User user = getUserById(userId); + return ResponseUserDto.from(user); } // 회원정보 수정 @Transactional public void updateUserInfo(UpdateUserDto updateUserDto) { User user = getUserById(updateUserDto.getUserId()); - BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); - user.updateUser(updateUserDto.getName(), updateUserDto.getHeight(), updateUserDto.getWeight(), updateUserDto.getAge(), baseNutrition); + user.updateUser(updateUserDto.getName(), updateUserDto.getHeight(), updateUserDto.getWeight(), updateUserDto.getAge()); + } + + // 회원 기준영양소 조회 + @Transactional(readOnly = true) + public ResponseUserNutritionDto getUserNutrition(Long userId) { + User user = getUserById(userId); + return ResponseUserNutritionDto.from(user); + } + + // 회원 기준영양소 직접 수정 + @Transactional + public void updateBaseNutrition(UpdateUserNutritionDto updateUserNutritionDto) { + User user = getUserById(updateUserNutritionDto.getUserId()); + BaseNutrition baseNutrition = BaseNutrition.createNutrition(updateUserNutritionDto.getCalorie(), updateUserNutritionDto.getCarbohydrate(), updateUserNutritionDto.getProtein(), updateUserNutritionDto.getFat()); + user.updateBaseNutrition(baseNutrition); } // 회원 탈퇴 @Transactional public void deleteUser(Long userId) { - User user = getUserById(userId); - userRepository.delete(user); + validateUser(userId); + userRepository.deleteById(userId); } - // 회원이 팔로우를 위해 검색한 유저 목록 조회 + // 회원의 친구 검색 결과 조회 @Transactional(readOnly = true) - public List searchUser(String name) { + public List searchUser(Long hostId, String name) { + validateUser(hostId); List users = userRepository.findAllByNameContaining(name); return users.stream() - .map(user -> ResponseResearchUserDto.of(user.getId(), user.getName())).collect(Collectors.toList()); + .map(user -> ResponseSearchUserDto.of(user.getId(), user.getName(), user.getImage(), followRepository.existsByFromUserAndToUser(hostId, user.getId()))).collect(Collectors.toList()); } // 회원이 특정 회원 팔로우 @Transactional public void followUser(Long userId, Long followId) { - User user = getUserById(userId); - User followUser = getUserById(followId); - user.followUser(followUser); + validateUser(userId); + validateUser(followId); + followRepository.save(Follow.makeFollow(userId, followId)); } // 회원이 특정 회원 팔로우 취소 @Transactional public void unfollowUser(Long userId, Long unfollowId) { - User user = getUserById(userId); - User followUser = getUserById(unfollowId); - user.unfollowUser(followUser); + validateUser(userId); + validateUser(unfollowId); + followRepository.delete(Follow.makeFollow(userId, unfollowId)); + } + + private void validateUser(Long userId) { + if(!userRepository.existsById(userId)) + throw new UserException(ResponseCode.USER_NOT_FOUND); } private User getUserById(Long userId) { return userRepository.findById(userId) .orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND)); } - - /* - 아래 메서드들은 원래 두 패키지 모두에 존재할 수 있는 혼합적인 의미를 가지고 있으며, 일단 FoodService에서 구현을 담당할 예정이다. - 현재 UserService가 음식 관련 Repository에 의존하지 않고 있기에, FoodService에서 구현하는 것이 더 적절하다고 판단했다. - - // 유저의 즐겨찾기 음식 목록 조회 - public void getFavoriteFoodList(Long userId) { - - } - - // 유저의 특정 날짜에 먹은 음식 목록 조회 - public void getFoodListByDate(Long userId, LocalDate date) { - - } - - // 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) - public void getNutritionSumByDate(Long userId, LocalDate date) { - - } - - // 유저의 최근 7일간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) - public void getNutritionSumByWeek(Long userId) { - - } - - // 유저의 최근 1개월간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요) - public void getNutritionSumByMonth(Long userId) { - - } - - // 유저의 최근 7일간의 Best 3 음식 조회 (dto 구체적 협의 필요) - public void getBestFoodByWeek(Long userId) { - - } - - // 유저의 최근 7일간의 Worst 3 음식 조회 (dto 구체적 협의 필요) - public void getWorstFoodByWeek(Long userId) { - - } - */ - - /* - * 위 메서드 외 누락된 메서드가 존재할 수 있으며, UserService에는 아래와 같은 추가적인 부가 기능을 구현할 가능성이 있다. - * 1. 팔로우 목록에 나를 포함하여 칼탄단지 섭취량 기준으로 건강한 섭취 현황을 분석(?)하여 점수별로 정렬하는 랭킹 기능 - * 2. 주간 과제 기능 (예: 주간에 목표한 섭취량을 달성하면 보상이 주어지는 등) - */ } From aef5da6ae66c0e28ae8e6a50c1cab84d323ee443 Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 10 Oct 2023 23:04:21 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=EF=B8=8FChore:=20UserSer?= =?UTF-8?q?vice=20=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/diareat/diareat/user/service/UserService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index ed90037..866aa3d 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -27,7 +27,7 @@ public class UserService { public Long saveUser(CreateUserDto createUserDto) { BaseNutrition baseNutrition = BaseNutrition.createNutrition(0, 0, 0, 0); // 로직 확정 전에는 임시 코드로 대체 // BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight()); - User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getAge(), createUserDto.getGender(), baseNutrition); + User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); return userRepository.save(user).getId(); } From c30ae9a6a8f6c78350ac6531a08ae73c96ab4d29 Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 10 Oct 2023 23:07:21 +0900 Subject: [PATCH 8/9] =?UTF-8?q?:white=5Fcheck=5Fmark:=20Test:=20UserServic?= =?UTF-8?q?eTest=20=EC=B6=94=EA=B0=80=20=EC=9E=91=EC=84=B1=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/service/UserServiceTest.java | 129 ++++++++++++------ 1 file changed, 88 insertions(+), 41 deletions(-) diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index 6f5ba89..0fdce53 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -1,23 +1,18 @@ package com.diareat.diareat.service; -import com.diareat.diareat.user.domain.BaseNutrition; -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.dto.*; +import com.diareat.diareat.user.repository.FollowRepository; import com.diareat.diareat.user.repository.UserRepository; import com.diareat.diareat.user.service.UserService; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; -import java.util.List; - import static org.junit.jupiter.api.Assertions.*; - @SpringBootTest @Transactional class UserServiceTest { @@ -28,93 +23,145 @@ class UserServiceTest { @Autowired UserRepository userRepository; + @Autowired + FollowRepository followRepository; + @BeforeEach - public void setUp() { + void setUp() { userRepository.deleteAll(); + followRepository.deleteAll(); + } + + @AfterEach + void tearDown() { + userRepository.deleteAll(); + followRepository.deleteAll(); } @Test - void testSaveAndGetUserInfo() { // 회원정보 저장 및 조회 + void saveUser() { // given + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); // when - Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); ResponseUserDto responseUserDto = userService.getUserInfo(userId); - // 검증: 올바른 결과를 반환하는지 확인 + // then assertNotNull(responseUserDto); assertEquals("testUser", responseUserDto.getName()); + assertEquals(25, responseUserDto.getAge()); } @Test - void testUpdateUserInfo() { // 회원정보 수정 + void getSimpleUserInfo() { + // given + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + + // when + ResponseSimpleUserDto responseSimpleUserDto = userService.getSimpleUserInfo(userId); + // then + assertEquals("testUser", responseSimpleUserDto.getName()); + } + + @Test + void getUserInfo() { // given - Long id = userService.saveUser(CreateUserDto.of("1", "testPassword", 180, 75, 1, 25)); - BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + + // when + ResponseUserDto responseUserDto = userService.getUserInfo(userId); + + // then + assertEquals("testUser", responseUserDto.getName()); + } + + @Test + void updateUserInfo() { + // given + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); + UpdateUserDto updateUserDto = UpdateUserDto.of(userId, "updateUser", 180, 75, 25, false); // when - UpdateUserDto updateUserDto = UpdateUserDto.of(id, "2", 185, 70, 75, baseNutrition); userService.updateUserInfo(updateUserDto); - ResponseUserDto responseUserDto = userService.getUserInfo(id); // then - assertNotNull(responseUserDto); - assertEquals("2", responseUserDto.getName()); - assertEquals(185, responseUserDto.getHeight()); - assertEquals(70, responseUserDto.getWeight()); + assertEquals("updateUser", userService.getUserInfo(userId).getName()); + assertEquals(180, userService.getUserInfo(userId).getHeight()); } @Test - void testDeleteUser() { // 회원 탈퇴 + void getUserNutrition() { // 임시 코드 사용, 추후 로직 개편 시 테스트코드 수정 // given - Long id = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); // when - userService.deleteUser(id); + ResponseUserNutritionDto responseUserNutritionDto = userService.getUserNutrition(userId); // then - assertNull(userRepository.findById(id).orElse(null)); + assertEquals(0, responseUserNutritionDto.getCalorie()); } @Test - void testSearchUserName() { + void updateBaseNutrition() { // given - userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); - userService.saveUser(CreateUserDto.of("hello", "testPassword", 180, 75, 1, 25)); + Long userId = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 160, 50, 25)); // when - String name = "testUser"; - List users = userService.searchUser(name); + UpdateUserNutritionDto updateUserNutritionDto = UpdateUserNutritionDto.of(userId, 2000, 300, 80, 80); + userService.updateBaseNutrition(updateUserNutritionDto); // then - assertEquals(1, users.size()); + assertEquals(2000, userService.getUserNutrition(userId).getCalorie()); } @Test - void testFollowUser() { // 회원이 특정 회원 팔로우 + void deleteUser() { // given - Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); - Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25)); + Long id = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); // when - userService.followUser(id1, id2); + userService.deleteUser(id); + + // then + assertFalse(userRepository.existsById(id)); + } + + @Test + void searchUser() { + // given + userService.saveUser(CreateUserDto.of("user1", "testPassword", 0, 175, 80, 25)); + Long id = userService.saveUser(CreateUserDto.of("user2", "testPassword", 0, 175, 80, 25)); + String name = "user"; + + // then + assertEquals(2, userService.searchUser(id, name).size()); + } + + @Test + void followUser() { + // given + Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 75, 1, 25)); + Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 0, 75, 1, 25)); + + // when + userService.followUser(id2, id1); // then - assertEquals(1, userRepository.findById(id1).get().getFollowings().size()); + assertEquals(1, followRepository.findAllByFromUser(id1).size()); } @Test - void testUnfollowUser() { // 회원이 특정 회원 팔로우 취소 + void unfollowUser() { // given - Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 180, 75, 1, 25)); - Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 180, 75, 1, 25)); + Long id1 = userService.saveUser(CreateUserDto.of("testUser", "testPassword", 0, 175, 1, 25)); + Long id2 = userService.saveUser(CreateUserDto.of("followUser", "testPassword", 0, 175, 1, 25)); // when userService.followUser(id1, id2); userService.unfollowUser(id1, id2); // then - assertEquals(0, userRepository.findById(id1).get().getFollowings().size()); + assertEquals(0, followRepository.findAllByFromUser(id1).size()); } -} +} \ No newline at end of file From 05399865eebf26de90942b9f0242de7a9cd399f7 Mon Sep 17 00:00:00 2001 From: CHAE Date: Tue, 10 Oct 2023 23:25:00 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=EF=B8=8FChore:=20UserSer?= =?UTF-8?q?viceTest=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/diareat/diareat/user/service/UserService.java | 2 +- src/test/java/com/diareat/diareat/service/UserServiceTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/diareat/diareat/user/service/UserService.java b/src/main/java/com/diareat/diareat/user/service/UserService.java index 866aa3d..2ce3bee 100644 --- a/src/main/java/com/diareat/diareat/user/service/UserService.java +++ b/src/main/java/com/diareat/diareat/user/service/UserService.java @@ -25,7 +25,7 @@ public class UserService { // 회원정보 저장 @Transactional public Long saveUser(CreateUserDto createUserDto) { - BaseNutrition baseNutrition = BaseNutrition.createNutrition(0, 0, 0, 0); // 로직 확정 전에는 임시 코드로 대체 + BaseNutrition baseNutrition = BaseNutrition.createNutrition(2000, 300, 80, 80); // BaseNutrition baseNutrition = BaseNutrition.createNutrition(createUserDto.getGender(), createUserDto.getAge(), createUserDto.getHeight(), createUserDto.getWeight()); User user = User.createUser(createUserDto.getName(), createUserDto.getKeyCode(), createUserDto.getHeight(), createUserDto.getWeight(), createUserDto.getGender(), createUserDto.getAge(), baseNutrition); return userRepository.save(user).getId(); diff --git a/src/test/java/com/diareat/diareat/service/UserServiceTest.java b/src/test/java/com/diareat/diareat/service/UserServiceTest.java index 0fdce53..7d2ec45 100644 --- a/src/test/java/com/diareat/diareat/service/UserServiceTest.java +++ b/src/test/java/com/diareat/diareat/service/UserServiceTest.java @@ -99,7 +99,7 @@ void getUserNutrition() { // 임시 코드 사용, 추후 로직 개편 시 테 ResponseUserNutritionDto responseUserNutritionDto = userService.getUserNutrition(userId); // then - assertEquals(0, responseUserNutritionDto.getCalorie()); + assertEquals(2000, responseUserNutritionDto.getCalorie()); } @Test