From 293b702b79201c19f8e04a4614ff9e2f2a6ca1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=9E=AC=ED=98=81?= Date: Sun, 29 Sep 2024 03:55:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20:recycle:=20validation=20?= =?UTF-8?q?=EA=B3=A0=EB=8F=84=ED=99=94=20(#72)=20(#73)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++ .../calendar/application/CalendarService.java | 7 ++-- .../CalendarQueryDslRepositoryImpl.java | 12 ++++-- .../response/CheckGoalProgressResponse.java | 6 +-- .../domain/repository/UserRepository.java | 2 + .../validation/annotation/ExistUser.java | 18 +++++++++ .../validator/UserExistValidator.java | 37 +++++++++++++++++++ 7 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/goormdari/domain/validation/annotation/ExistUser.java create mode 100644 src/main/java/com/goormdari/domain/validation/validator/UserExistValidator.java diff --git a/build.gradle b/build.gradle index 689c57a..cd7ce2a 100644 --- a/build.gradle +++ b/build.gradle @@ -58,6 +58,9 @@ dependencies { //mail implementation 'org.springframework.boot:spring-boot-starter-mail' + //validation + implementation 'org.springframework.boot:spring-boot-starter-validation' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' diff --git a/src/main/java/com/goormdari/domain/calendar/application/CalendarService.java b/src/main/java/com/goormdari/domain/calendar/application/CalendarService.java index 85d5b61..9955f06 100644 --- a/src/main/java/com/goormdari/domain/calendar/application/CalendarService.java +++ b/src/main/java/com/goormdari/domain/calendar/application/CalendarService.java @@ -4,6 +4,7 @@ import com.goormdari.domain.calendar.dto.response.CheckGoalProgressResponse; import com.goormdari.domain.user.domain.User; import com.goormdari.domain.user.domain.repository.UserRepository; +import com.goormdari.domain.validation.annotation.ExistUser; import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -20,10 +21,8 @@ public class CalendarService { private final UserRepository userRepository; - public CheckGoalProgressResponse searchCheckGoalProgress(String username, YearMonth date) { - System.out.println("username = " + username); - User user = userRepository.findByUsername(username) - .orElseThrow(() -> new EntityNotFoundException("User not found")); + public CheckGoalProgressResponse searchCheckGoalProgress(@ExistUser final String username, final YearMonth date) { + User user = userRepository.findByUsername(username).get(); CheckGoalProgressResponse checkGoalProgressResponse = calendarRepository.findByIdAndDate(user.getId(), date); diff --git a/src/main/java/com/goormdari/domain/calendar/domain/repository/CalendarQueryDslRepositoryImpl.java b/src/main/java/com/goormdari/domain/calendar/domain/repository/CalendarQueryDslRepositoryImpl.java index e4d2c21..da36afd 100644 --- a/src/main/java/com/goormdari/domain/calendar/domain/repository/CalendarQueryDslRepositoryImpl.java +++ b/src/main/java/com/goormdari/domain/calendar/domain/repository/CalendarQueryDslRepositoryImpl.java @@ -42,6 +42,7 @@ public CheckGoalProgressResponse findByIdAndDate(Long userId, YearMonth date) { throw new EntityNotFoundException("User not found"); } + List calendarList = queryFactory .selectFrom(calendar) .where( @@ -49,13 +50,15 @@ public CheckGoalProgressResponse findByIdAndDate(Long userId, YearMonth date) { calendar.date.between(startDate, endDate) ) .fetch(); - + String goal = userInfo.getGoal(); + int dDay = calculateDDay(userInfo.getDeadLine()); + List dayAchiveList = calendarList.stream() .map(entry -> new CheckGoalProgressResponse.DayAchive( entry.getDate(), @@ -63,15 +66,16 @@ public CheckGoalProgressResponse findByIdAndDate(Long userId, YearMonth date) { )) .collect(Collectors.toList()); - Double routineCompletionRate = calculateRoutineCompletionRate(userId, date); + +// Double routineCompletionRate = calculateRoutineCompletionRate(userId, date); + return new CheckGoalProgressResponse( userInfo.getNickname(), userInfo.getProfileUrl(), goal, dDay, - dayAchiveList, - routineCompletionRate + dayAchiveList ); } diff --git a/src/main/java/com/goormdari/domain/calendar/dto/response/CheckGoalProgressResponse.java b/src/main/java/com/goormdari/domain/calendar/dto/response/CheckGoalProgressResponse.java index 3531a5e..638373d 100644 --- a/src/main/java/com/goormdari/domain/calendar/dto/response/CheckGoalProgressResponse.java +++ b/src/main/java/com/goormdari/domain/calendar/dto/response/CheckGoalProgressResponse.java @@ -12,8 +12,7 @@ public record CheckGoalProgressResponse( String profileUrl, String goal, int dDay, - List dayAchiveList, - Double routineCompletionRate + List dayAchiveList ) { public record DayAchive( LocalDate date, @@ -22,12 +21,11 @@ public record DayAchive( } @QueryProjection - public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List dayAchiveList, Double routineCompletionRate) { + public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List dayAchiveList) { this.nickname = nickname; this.profileUrl = profileUrl; this.goal = goal; this.dDay = dDay; this.dayAchiveList = dayAchiveList; - this.routineCompletionRate = routineCompletionRate; } } diff --git a/src/main/java/com/goormdari/domain/user/domain/repository/UserRepository.java b/src/main/java/com/goormdari/domain/user/domain/repository/UserRepository.java index d3652d7..0f8ba68 100644 --- a/src/main/java/com/goormdari/domain/user/domain/repository/UserRepository.java +++ b/src/main/java/com/goormdari/domain/user/domain/repository/UserRepository.java @@ -17,4 +17,6 @@ public interface UserRepository extends JpaRepository { @Query("SELECT u FROM User u WHERE u.team.id = :teamId") List findByTeamId(Long teamId); + + boolean existsByUsername(String username); } diff --git a/src/main/java/com/goormdari/domain/validation/annotation/ExistUser.java b/src/main/java/com/goormdari/domain/validation/annotation/ExistUser.java new file mode 100644 index 0000000..1b4e677 --- /dev/null +++ b/src/main/java/com/goormdari/domain/validation/annotation/ExistUser.java @@ -0,0 +1,18 @@ +package com.goormdari.domain.validation.annotation; + +import com.goormdari.domain.validation.validator.UserExistValidator; +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +import java.lang.annotation.*; + +@Documented +@Constraint(validatedBy = UserExistValidator.class) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ExistUser { + + String message() default "해당하는 유저가 존재하지 않습니다."; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/src/main/java/com/goormdari/domain/validation/validator/UserExistValidator.java b/src/main/java/com/goormdari/domain/validation/validator/UserExistValidator.java new file mode 100644 index 0000000..e33ee35 --- /dev/null +++ b/src/main/java/com/goormdari/domain/validation/validator/UserExistValidator.java @@ -0,0 +1,37 @@ +package com.goormdari.domain.validation.validator; + +import com.goormdari.domain.user.domain.User; +import com.goormdari.domain.user.domain.repository.UserRepository; +import com.goormdari.domain.validation.annotation.ExistUser; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class UserExistValidator implements ConstraintValidator { + + private final UserRepository userRepository; + + + @Override + public void initialize(ExistUser constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + } + + @Override + public boolean isValid(String username, ConstraintValidatorContext context) { + + boolean isValid = userRepository.existsByUsername(username); + + if (!isValid) { + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate("User not found").addConstraintViolation(); + } + + return isValid; + } +} From 2f6ad8da89a38bebf3f279ba51df9994fb4ed813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=9E=AC=ED=98=81?= Date: Sun, 29 Sep 2024 04:01:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EC=9C=A0=EC=A0=80=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20(#74)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: :recycle: validation 고도화 (#72) * fix: 유저 정보 업데이트 삭제 --- .../user/presentation/UserController.java | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/main/java/com/goormdari/domain/user/presentation/UserController.java b/src/main/java/com/goormdari/domain/user/presentation/UserController.java index 438d526..63317f8 100644 --- a/src/main/java/com/goormdari/domain/user/presentation/UserController.java +++ b/src/main/java/com/goormdari/domain/user/presentation/UserController.java @@ -64,21 +64,4 @@ public UserInfoResponse getCurrentUserInfo(@Parameter(description = "Accesstoken return userService.getUserInfo(userId); } - @Operation(summary = "현재 유저 프로필 업데이트", description = "유저의 nickname, username, password(지난 비밀번호 검증 과정 존재) email, profileImageUrl 업데이트 기능(null 값으로 전송 시, 업데이트 X)") - @PostMapping("/profile") - public UserInfoResponse updateCurrentUserInfo(@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @RequestHeader("Authorization") String token, - @Valid @RequestBody UpdateUserRequest updateUserRequest) { - if (token == null) { - throw new InvalidTokenException(); - } - - String jwt = token.startsWith("Bearer ") ? token.substring(7) : token; - if (!jwtUtil.validateToken(jwt)) { - throw new IllegalArgumentException("Invalid token"); - } - Long userId = jwtUtil.extractId(jwt); - - return userService.updateUserProfile(userId, updateUserRequest); - } - -} +} \ No newline at end of file