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/user/presentation/UserController.java b/src/main/java/com/goormdari/domain/user/presentation/UserController.java index 2c31b1b..9056edb 100644 --- a/src/main/java/com/goormdari/domain/user/presentation/UserController.java +++ b/src/main/java/com/goormdari/domain/user/presentation/UserController.java @@ -1,10 +1,9 @@ package com.goormdari.domain.user.presentation; import com.goormdari.domain.calendar.exception.InvalidTokenException; -import com.goormdari.domain.user.dto.response.FindCurrentStepResponse; import com.goormdari.domain.user.dto.response.UserInfoResponse; -import com.goormdari.domain.user.dto.request.UpdateUserRequest; import com.goormdari.domain.user.service.UserService; +import com.goormdari.domain.user.dto.response.FindCurrentStepResponse; import com.goormdari.global.config.security.jwt.JWTUtil; import com.goormdari.global.payload.ErrorResponse; import com.goormdari.global.payload.ResponseCustom; @@ -15,7 +14,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; 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; + } +}