From 33db35e6db742cc6c5ffe74de48e4bdf19aef9fa Mon Sep 17 00:00:00 2001 From: InSooBeen Date: Sat, 23 Nov 2024 17:55:01 +0900 Subject: [PATCH] mission: add week8 mission Changes --- .../MemberMissionCommandService.java | 2 +- .../MemberMissionCommandServiceImpl.java | 14 +++----- .../annotation/IsMemberMissionNotStarted.java | 17 ++++++++++ .../IsMemberMissionNotStartedValidator.java | 33 +++++++++++++++++++ .../MemberMissionRequestDTO.java | 6 ++++ 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 src/main/java/javalab/umc7th_mission/study/validation/annotation/IsMemberMissionNotStarted.java create mode 100644 src/main/java/javalab/umc7th_mission/study/validation/validatior/IsMemberMissionNotStartedValidator.java diff --git a/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandService.java b/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandService.java index c679f9a..a9963c0 100644 --- a/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandService.java +++ b/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandService.java @@ -5,6 +5,6 @@ public interface MemberMissionCommandService { MemberMission AddMemberMission(MemberMissionRequestDTO.AddMemberMissionDto request); - boolean isChallenging(Long memberId, Long missionId); + boolean isExist(Long memberId, Long missionId); } diff --git a/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandServiceImpl.java b/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandServiceImpl.java index 9103ab4..66b7762 100644 --- a/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandServiceImpl.java +++ b/src/main/java/javalab/umc7th_mission/study/service/MemberMissionService/MemberMissionCommandServiceImpl.java @@ -5,7 +5,6 @@ import javalab.umc7th_mission.study.converter.MemberMissionConverter; import javalab.umc7th_mission.study.domain.Member; import javalab.umc7th_mission.study.domain.Mission; -import javalab.umc7th_mission.study.domain.enums.MissionStatus; import javalab.umc7th_mission.study.domain.mapping.MemberMission; import javalab.umc7th_mission.study.repository.MemberMissionRepository.MemberMissionRepository; import javalab.umc7th_mission.study.repository.MemberRepository.MemberRepository; @@ -29,23 +28,20 @@ public MemberMission AddMemberMission(MemberMissionRequestDTO.AddMemberMissionDt Mission mission = missionRepository.findById(Math.toIntExact(request.getMissionId())) .orElseThrow(() -> new GeneralException(ErrorStatus.MISSION_NOT_FOUND)); - - if(isChallenging(request.getMemberId(), request.getMissionId())){ - throw new GeneralException(ErrorStatus.MEMBER_MISSION_ALREADY_IN_CHALLENGING); - } MemberMission membermission = MemberMissionConverter.toMemberMission(member, mission); + + return memberMissionRepository.save(membermission); } @Override - public boolean isChallenging(Long memberId, Long missionId){ - MemberMission memberMission = memberMissionRepository.findMemberMissionByMemberIdAndMissionId(memberId, missionId); + public boolean isExist(Long memberId, Long missionId){ + MemberMission memberMission = memberMissionRepository.findMemberMissionByMemberIdAndMissionId(memberId, missionId); - if(memberMission != null && memberMission.getStatus().equals(MissionStatus.CHALLENGING)){ + if(memberMission != null){ return true; } - return false; } } diff --git a/src/main/java/javalab/umc7th_mission/study/validation/annotation/IsMemberMissionNotStarted.java b/src/main/java/javalab/umc7th_mission/study/validation/annotation/IsMemberMissionNotStarted.java new file mode 100644 index 0000000..896781d --- /dev/null +++ b/src/main/java/javalab/umc7th_mission/study/validation/annotation/IsMemberMissionNotStarted.java @@ -0,0 +1,17 @@ +package javalab.umc7th_mission.study.validation.annotation; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import javalab.umc7th_mission.study.validation.validatior.IsMemberMissionNotStartedValidator; + +import java.lang.annotation.*; + +@Documented +@Constraint(validatedBy = IsMemberMissionNotStartedValidator.class) +@Target( { ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IsMemberMissionNotStarted { + String message() default "해당 미션은 이미 진행중입니다."; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/src/main/java/javalab/umc7th_mission/study/validation/validatior/IsMemberMissionNotStartedValidator.java b/src/main/java/javalab/umc7th_mission/study/validation/validatior/IsMemberMissionNotStartedValidator.java new file mode 100644 index 0000000..7249903 --- /dev/null +++ b/src/main/java/javalab/umc7th_mission/study/validation/validatior/IsMemberMissionNotStartedValidator.java @@ -0,0 +1,33 @@ +package javalab.umc7th_mission.study.validation.validatior; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import javalab.umc7th_mission.study.apiPayload.code.status.ErrorStatus; +import javalab.umc7th_mission.study.service.MemberMissionService.MemberMissionCommandService; +import javalab.umc7th_mission.study.validation.annotation.IsMemberMissionNotStarted; +import javalab.umc7th_mission.study.web.dto.member_mission.MemberMissionRequestDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class IsMemberMissionNotStartedValidator implements ConstraintValidator { + private final MemberMissionCommandService memberMissionCommandService; + @Override + public void initialize(IsMemberMissionNotStarted constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + } + + @Override + public boolean isValid(MemberMissionRequestDTO.AddMemberMissionDto request, ConstraintValidatorContext context) { + boolean isValid = memberMissionCommandService.isExist(request.getMemberId(), request.getMissionId()); + + if(isValid){ + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate(ErrorStatus.MEMBER_MISSION_ALREADY_IN_CHALLENGING.toString()).addConstraintViolation(); + return false; + } + return true; + } + +} diff --git a/src/main/java/javalab/umc7th_mission/study/web/dto/member_mission/MemberMissionRequestDTO.java b/src/main/java/javalab/umc7th_mission/study/web/dto/member_mission/MemberMissionRequestDTO.java index f5a239a..7cfc0a9 100644 --- a/src/main/java/javalab/umc7th_mission/study/web/dto/member_mission/MemberMissionRequestDTO.java +++ b/src/main/java/javalab/umc7th_mission/study/web/dto/member_mission/MemberMissionRequestDTO.java @@ -1,11 +1,17 @@ package javalab.umc7th_mission.study.web.dto.member_mission; import jakarta.validation.constraints.NotNull; +import javalab.umc7th_mission.study.validation.annotation.IsMemberMissionNotStarted; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import java.time.LocalDate; + public class MemberMissionRequestDTO { + @IsMemberMissionNotStarted @Getter public static class AddMemberMissionDto{ @NotNull(message = "회원 id는 필수 항목입니다.")