From 2d24a5998add6b93cda3cc4191e507020735f0fb Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 11 Jan 2024 10:45:15 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20Note=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FoodController.java | 9 ++++++++ .../yourprotein/service/FoodService.java | 21 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kim3ho1/yourprotein/controller/FoodController.java b/src/main/java/com/kim3ho1/yourprotein/controller/FoodController.java index 09df068..a45b26c 100644 --- a/src/main/java/com/kim3ho1/yourprotein/controller/FoodController.java +++ b/src/main/java/com/kim3ho1/yourprotein/controller/FoodController.java @@ -2,8 +2,11 @@ import java.util.List; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -34,6 +37,12 @@ public ResponseEntity noteProtein(@RequestBody double protein) { return ResponseEntity.ok("ok"); } + @DeleteMapping("/note/{noteId}") + public ResponseEntity deleteNote(@PathVariable("noteId") Long noteId) { + // TODO Delete 테스트 + return ResponseEntity.ok(foodService.deleteNote(noteId)); + } + // 메인 화면 정보 - 오늘 섭취량/목표 섭취량 @GetMapping("") public ResponseEntity getNoteInfo() { diff --git a/src/main/java/com/kim3ho1/yourprotein/service/FoodService.java b/src/main/java/com/kim3ho1/yourprotein/service/FoodService.java index be91c73..ff30256 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/FoodService.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/FoodService.java @@ -2,12 +2,13 @@ import java.time.LocalDate; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; -import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpStatus; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.transaction.annotation.Transactional; import com.kim3ho1.yourprotein.domain.Food; import com.kim3ho1.yourprotein.domain.Note; @@ -55,8 +56,7 @@ public NoteResponseDto.NoteStatisticsResponseDto getNoteInfo() { List notesByUserAndToday = noteRepository.calculateToday(user.getId(), LocalDate.now().toString()); notesByUserAndToday.stream().map(data->{current+=data.getProtein(); return null; }).collect(Collectors.toList()); - // TODO goal 추가 - return new NoteResponseDto.NoteStatisticsResponseDto(1000.0, current); + return new NoteResponseDto.NoteStatisticsResponseDto(user.getGoalProtein(), current); } // 메인 화면 정보 - 일주일간 섭취량 @@ -73,4 +73,17 @@ public List getWeekly() { .build(); }).collect(Collectors.toList()); } + + @Transactional + public HttpStatus deleteNote(Long noteId) { + CustomUserDetails principal = (CustomUserDetails)SecurityContextHolder.getContext() + .getAuthentication() + .getPrincipal(); + User user = principal.getUser(); + Note note = noteRepository.findById(noteId).orElseThrow(() -> new RuntimeException()); + if (note.getUser() != user) + return HttpStatus.BAD_GATEWAY; + noteRepository.delete(note); + return HttpStatus.OK; + } } From 0d365ec2e5a854e6ce5e0c90c9f923e7cd509772 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 11 Jan 2024 11:07:15 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EB=A7=88=EC=9D=B4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 5 +++ .../com/kim3ho1/yourprotein/domain/User.java | 4 +++ .../yourprotein/dto/UserRegisterDto.java | 34 +++++++++++++++++++ .../yourprotein/service/UserService.java | 2 ++ .../yourprotein/service/UserServiceImpl.java | 22 ++++++++++++ 5 files changed, 67 insertions(+) diff --git a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java index baf6789..8150559 100644 --- a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java +++ b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java @@ -22,4 +22,9 @@ public ResponseEntity modifyUserDetails(@RequestBody UserRegisterDto.Register return ResponseEntity.ok(null); } + @PutMapping("/update") + public ResponseEntity updateUserDetails(@RequestBody UserRegisterDto.UpdateUserRequestDto updateUserRequestDto) { + return ResponseEntity.ok(userService.updateUserDetails(updateUserRequestDto)); + } + } diff --git a/src/main/java/com/kim3ho1/yourprotein/domain/User.java b/src/main/java/com/kim3ho1/yourprotein/domain/User.java index 31c5634..486ffc5 100644 --- a/src/main/java/com/kim3ho1/yourprotein/domain/User.java +++ b/src/main/java/com/kim3ho1/yourprotein/domain/User.java @@ -78,4 +78,8 @@ public void setGender(Gender gender) { public void setPurpose(PurposeType purpose) { this.purpose = purpose; } + + public void setGoalProtein(double goalProtein) { + this.goalProtein = goalProtein; + } } diff --git a/src/main/java/com/kim3ho1/yourprotein/dto/UserRegisterDto.java b/src/main/java/com/kim3ho1/yourprotein/dto/UserRegisterDto.java index 80e4735..aafac74 100644 --- a/src/main/java/com/kim3ho1/yourprotein/dto/UserRegisterDto.java +++ b/src/main/java/com/kim3ho1/yourprotein/dto/UserRegisterDto.java @@ -2,6 +2,13 @@ import com.kim3ho1.yourprotein.domain.Gender; import com.kim3ho1.yourprotein.domain.PurposeType; + +import jakarta.persistence.Column; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -46,4 +53,31 @@ public static class RegisterRequestDto { } + @Data + @AllArgsConstructor + @Builder + public static class UpdateUserRequestDto { + private double height; + private double weight; + + public PurposeType purpose; + public double goalProtein; + + } + @Data + @AllArgsConstructor + @Builder + public static class UserResponseDto { + + private Long id; + private String name; + private String email; + private Integer age; + private double height; + private double weight; + private Gender gender; + private PurposeType purpose; + private double goalProtein; + + } } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java index 6c45897..3026e7a 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java @@ -22,4 +22,6 @@ public interface UserService { void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequestDto); + UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto); + } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java index e3b0b3e..ba1d0f5 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Slf4j @Service @@ -77,4 +78,25 @@ public void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequest userRepository.save(user); } + + @Transactional // TODO 마이페이지 업데이트 Test + public UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto) { + CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + User user = userDetails.getUser(); + user.setHeight(updateUserRequestDto.getHeight()); + user.setWeight(updateUserRequestDto.getWeight()); + user.setPurpose(updateUserRequestDto.getPurpose()); + user.setGoalProtein(user.getGoalProtein()); + return UserRegisterDto.UserResponseDto.builder() + .id(user.getId()) + .name(user.getName()) + .email(user.getEmail()) + .age(user.getAge()) + .height(user.getHeight()) + .weight(user.getWeight()) + .gender(user.getGender()) + .purpose(user.getPurpose()) + .goalProtein(user.getGoalProtein()) + .build(); + } } From 46fc0fe7c21e9a3bf9cec348992dc67078b9ffec Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 11 Jan 2024 11:11:16 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=EB=A7=88=EC=9D=B4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourprotein/controller/UserController.java | 6 +++++- .../yourprotein/service/UserService.java | 1 + .../yourprotein/service/UserServiceImpl.java | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java index 8150559..247bc69 100644 --- a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java +++ b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java @@ -22,9 +22,13 @@ public ResponseEntity modifyUserDetails(@RequestBody UserRegisterDto.Register return ResponseEntity.ok(null); } - @PutMapping("/update") + @PutMapping("/update") // TODO 마이페이지 업데이트 Test public ResponseEntity updateUserDetails(@RequestBody UserRegisterDto.UpdateUserRequestDto updateUserRequestDto) { return ResponseEntity.ok(userService.updateUserDetails(updateUserRequestDto)); } + @GetMapping("") // TODO 마이페이지 조회 테스트 + public ResponseEntity getUserDetails() { + return ResponseEntity.ok(userService.getUserDetails()); + } } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java index 3026e7a..2b32378 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java @@ -23,5 +23,6 @@ public interface UserService { void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequestDto); UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto); + UserRegisterDto.UserResponseDto getUserDetails(); } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java index ba1d0f5..98f4dbe 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java @@ -99,4 +99,21 @@ public UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateU .goalProtein(user.getGoalProtein()) .build(); } + + @Override // TODO 마이페이지 테스트 + public UserRegisterDto.UserResponseDto getUserDetails() { + CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + User user = userDetails.getUser(); + return UserRegisterDto.UserResponseDto.builder() + .id(user.getId()) + .name(user.getName()) + .email(user.getEmail()) + .age(user.getAge()) + .height(user.getHeight()) + .weight(user.getWeight()) + .gender(user.getGender()) + .purpose(user.getPurpose()) + .goalProtein(user.getGoalProtein()) + .build(); + } } From 80909ff35b8afd4c5df2ee8c2ee98ab4ed01fff0 Mon Sep 17 00:00:00 2001 From: seheonnn Date: Thu, 11 Jan 2024 11:35:11 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=EB=AA=A9=ED=91=9C=20=EB=8B=A8?= =?UTF-8?q?=EB=B0=B1=EC=A7=88=20=EC=B4=88=EA=B8=B0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourprotein/controller/UserController.java | 5 +++++ .../yourprotein/domain/PurposeType.java | 4 ++-- .../yourprotein/service/UserService.java | 2 ++ .../yourprotein/service/UserServiceImpl.java | 18 +++++++++++++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java index 247bc69..836612b 100644 --- a/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java +++ b/src/main/java/com/kim3ho1/yourprotein/controller/UserController.java @@ -31,4 +31,9 @@ public ResponseEntity getUserDetails() { return ResponseEntity.ok(userService.getUserDetails()); } + @PutMapping("/reset") // TODO 목표 단백질량 초기화 + public ResponseEntity resetGoal() { + return ResponseEntity.ok(userService.resetGoal()); + } + } diff --git a/src/main/java/com/kim3ho1/yourprotein/domain/PurposeType.java b/src/main/java/com/kim3ho1/yourprotein/domain/PurposeType.java index 07d71b9..9ee502e 100644 --- a/src/main/java/com/kim3ho1/yourprotein/domain/PurposeType.java +++ b/src/main/java/com/kim3ho1/yourprotein/domain/PurposeType.java @@ -10,7 +10,7 @@ public enum PurposeType { TRAINING(2,1.2, "TRAINING"), // 운동 하는 사람 BULK(3,2.0, "BULK"); // 벌크업을 원하는 사람 private final int index; - private final Double value; + private final double value; private final String role; @@ -19,6 +19,6 @@ public int getIndex() { return index; } - public Double getActivity() { return value; } + public double getPurpose() { return value; } } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java index 2b32378..40ff6ea 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserService.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserService.java @@ -25,4 +25,6 @@ public interface UserService { UserRegisterDto.UserResponseDto updateUserDetails(UserRegisterDto.UpdateUserRequestDto updateUserRequestDto); UserRegisterDto.UserResponseDto getUserDetails(); + double calculateGoal(double weight, double val); + double resetGoal(); } diff --git a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java index 98f4dbe..addc7f3 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/UserServiceImpl.java @@ -61,7 +61,7 @@ public void setRefreshTokenByKakaoId(Long kakaoId, String refreshToken) { user.setRefreshToken(refreshToken); } - @Override + @Override // TODO 사용자 추가 정보 설정 public void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequestDto) { log.info("modify user details"); CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); @@ -74,6 +74,8 @@ public void modifyUserDetails(UserRegisterDto.RegisterRequestDto registerRequest user.setHeight(Double.parseDouble(registerRequestDto.getHeight())); user.setWeight(Double.parseDouble(registerRequestDto.getWeight())); user.setPurpose(registerRequestDto.getPurpose()); + user.setGoalProtein(calculateGoal( + Double.parseDouble(registerRequestDto.getWeight()), registerRequestDto.getPurpose().getPurpose())); userRepository.save(user); @@ -116,4 +118,18 @@ public UserRegisterDto.UserResponseDto getUserDetails() { .goalProtein(user.getGoalProtein()) .build(); } + + @Override + public double calculateGoal(double weight, double val) { + return weight * val; + } + + @Override + @Transactional + public double resetGoal() { + CustomUserDetails userDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + User user = userDetails.getUser(); + user.setGoalProtein(calculateGoal(user.getWeight(), user.getPurpose().getPurpose())); + return user.getGoalProtein(); + } }