Skip to content

Commit

Permalink
Merge pull request #20 from CAUSOLDOUTMEN/feature/19-feat-userservice
Browse files Browse the repository at this point in the history
✨️feat: User 객체 속성 개편 및 UserService 추가 구현 (#19)
  • Loading branch information
synoti21 authored Oct 11, 2023
2 parents 60e12ed + 0539986 commit 5c94104
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 160 deletions.
23 changes: 17 additions & 6 deletions src/main/java/com/diareat/diareat/user/domain/BaseNutrition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/diareat/diareat/user/domain/Follow.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 7 additions & 19 deletions src/main/java/com/diareat/diareat/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,6 +25,8 @@ public class User {
@JsonIgnore
private String keyCode; // 로그인 식별키

private String image; // 프로필 사진 경로

private int height; // 키

private int weight; // 몸무게
Expand All @@ -35,22 +35,14 @@ public class User {

private int age; // 나이

private BaseNutrition baseNutrition; // 영양소
private BaseNutrition baseNutrition; // 기준영양소

@OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 촬영한 음식도 삭제
private List<Food> foods = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 즐겨찾기 음식도 삭제
private List<FavoriteFood> favoriteFoods = new ArrayList<>();

@ManyToMany // 팔로우 목록
@JoinTable(
name = "user_follows",
joinColumns = @JoinColumn(name = "follower_id"),
inverseJoinColumns = @JoinColumn(name = "following_id")
)
private Set<User> 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();
Expand All @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/diareat/diareat/user/dto/FollowUserDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
private boolean isFollow; // 유저가 이미 팔로우한 유저인지 확인

public static ResponseSearchUserDto of(Long userId, String name, String image, boolean isFollow) {
return new ResponseSearchUserDto(userId, name, image, isFollow);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 4 additions & 8 deletions src/main/java/com/diareat/diareat/user/dto/ResponseUserDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/diareat/diareat/user/dto/SearchUserDto.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/diareat/diareat/user/dto/UpdateUserDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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<Follow, Follow.PK> {
@Query(value = "select u from Follow f INNER JOIN User u ON f.toUser = u.id where f.fromUser = :userId") // 팔로우 목록 조회
List<User> findAllByFromUser(@Param("userId") Long userId);

boolean existsByFromUserAndToUser(Long fromUser, Long toUser); // 팔로우 여부 확인
}
Loading

0 comments on commit 5c94104

Please sign in to comment.