Skip to content

Commit

Permalink
mission: add week8 mission Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
InSooBeen committed Nov 23, 2024
1 parent 6183326 commit 33db35e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

public interface MemberMissionCommandService {
MemberMission AddMemberMission(MemberMissionRequestDTO.AddMemberMissionDto request);
boolean isChallenging(Long memberId, Long missionId);
boolean isExist(Long memberId, Long missionId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -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<IsMemberMissionNotStarted, MemberMissionRequestDTO.AddMemberMissionDto> {
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;
}

}
Original file line number Diff line number Diff line change
@@ -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는 필수 항목입니다.")
Expand Down

0 comments on commit 33db35e

Please sign in to comment.