Skip to content

Commit

Permalink
refactor: ♻️ validation 고도화 (#72) (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 authored Sep 28, 2024
1 parent 2dff696 commit 293b702
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,40 @@ public CheckGoalProgressResponse findByIdAndDate(Long userId, YearMonth date) {
throw new EntityNotFoundException("User not found");
}


List<Calendar> calendarList = queryFactory
.selectFrom(calendar)
.where(
calendar.userId.eq(userId),
calendar.date.between(startDate, endDate)
)
.fetch();



String goal = userInfo.getGoal();


int dDay = calculateDDay(userInfo.getDeadLine());


List<CheckGoalProgressResponse.DayAchive> dayAchiveList = calendarList.stream()
.map(entry -> new CheckGoalProgressResponse.DayAchive(
entry.getDate(),
entry.getSuccess_count()
))
.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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public record CheckGoalProgressResponse(
String profileUrl,
String goal,
int dDay,
List<DayAchive> dayAchiveList,
Double routineCompletionRate
List<DayAchive> dayAchiveList
) {
public record DayAchive(
LocalDate date,
Expand All @@ -22,12 +21,11 @@ public record DayAchive(
}

@QueryProjection
public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List<DayAchive> dayAchiveList, Double routineCompletionRate) {
public CheckGoalProgressResponse(String nickname, String profileUrl, String goal, int dDay, List<DayAchive> dayAchiveList) {
this.nickname = nickname;
this.profileUrl = profileUrl;
this.goal = goal;
this.dDay = dDay;
this.dayAchiveList = dayAchiveList;
this.routineCompletionRate = routineCompletionRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface UserRepository extends JpaRepository<User, Long> {

@Query("SELECT u FROM User u WHERE u.team.id = :teamId")
List<User> findByTeamId(Long teamId);

boolean existsByUsername(String username);
}
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -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<ExistUser, String> {

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;
}
}

0 comments on commit 293b702

Please sign in to comment.