Part 1. Spring Framework
Ch 03. 예제를 만들며 이해하는 스프링 웹 어플리케이션 핵심 기술
09. 비즈니스 벨리데이션 적용
package com.fastcampus.programmin.dmaker.service;
import com.fastcampus.programmin.dmaker.Repository.DeveloperRepository;
import com.fastcampus.programmin.dmaker.dto.CreateDeveloper;
import com.fastcampus.programmin.dmaker.entity.Developer;
import com.fastcampus.programmin.dmaker.type.DeveloperLevel;
import com.fastcampus.programmin.dmaker.type.DeveloperSkillType;
import exception.DMakerErrorCode;
import exception.DMakerException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Optional;
import static exception.DMakerErrorCode.*;
@Service
@RequiredArgsConstructor
public class dMakerService {
private final DeveloperRepository developerRepository;
private final EntityManager em;
@Transactional
public void createDeveloper(CreateDeveloper.Request request) {
validateCreateDeveloperRequest(request);
//business logic start
Developer developer = Developer.builder()
.developerLevel(DeveloperLevel.JUNIOR)
.developerSkillType(DeveloperSkillType.FRONT_END)
.experienceYears(2)
.name("Olaf")
.age(5)
.build();
/* A -> B 1만원 송금 */
// A 계좌에서 1만원 줄임
developerRepository.save(developer);
// B 계좌에서 1만원 늘림림
//developerRepository.delete(developer1);
}
private void validateCreateDeveloperRequest(CreateDeveloper.Request request) {
DeveloperLevel developerLevel = request.getDeveloperLevel();
Integer experienceYears = request.getExperienceYears();
if (developerLevel == DeveloperLevel.SENIOR
&& experienceYears < 10) {
throw new DMakerException(LEVEL_EXPERIENCE_YEARS_NOT_MATCHED);
}
if (developerLevel == DeveloperLevel.JUNGNior
&& (experienceYears < 4 || experienceYears > 10))
throw new DMakerException(LEVEL_EXPERIENCE_YEARS_NOT_MATCHED);
if (developerLevel == DeveloperLevel.JUNIOR && experienceYears > 4)
throw new DMakerException(LEVEL_EXPERIENCE_YEARS_NOT_MATCHED);
developerRepository.findByMemberId(request.getMemberId())
.ifPresent((developer -> {
throw new DMakerException(DUPLICATED_MEMBER_ID);
}));
}
}
<DMakerService.java>
package com.fastcampus.programmin.dmaker.Repository;
import com.fastcampus.programmin.dmaker.entity.Developer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface DeveloperRepository
extends JpaRepository<Developer, Long> {
Optional<Developer> findByMemberId(String memberId);
}
<DeveloperRepository.java>
package exception;
import lombok.Getter;
@Getter
public class DMakerException extends RuntimeException {
private DMakerErrorCode dMakerErrorCode;
private String detailMessage;
public DMakerException(DMakerErrorCode errorCode) {
super(errorCode.getMessage());
this.dMakerErrorCode = errorCode;
this.detailMessage = errorCode.getMessage();
}
public DMakerException(DMakerErrorCode errorCode, String detailMessage) {
super(detailMessage);
this.dMakerErrorCode = errorCode;
this.detailMessage = detailMessage;
}
}
<DMakerException.java>
package exception;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum DMakerErrorCode {
NO_DEVELOPER("해당되는 개발자가 없습니다."),
DUPLICATED_MEMBER_ID("MemberId가 중복되는 개발자가 있습니다."),
LEVEL_EXPERIENCE_YEARS_NOT_MATCHED("개발자 레벨과 연차가 맞지 않습니다."),
INTERNAL_SERVER_ERROR("서버에 오류가 발상했습니다."),
INVALID_REQUEST("잘못된 요청입니다.");
private final String message;
}
<DMakerErrorCode.java>
---------------------------------------------------------------------------------------------------------------------------------
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 25일차 (0) | 2022.02.17 |
---|---|
패스트캠퍼스 챌린지 24일차 (0) | 2022.02.16 |
패스트캠퍼스 챌린지 22일차 (0) | 2022.02.14 |
패스트캠퍼스 챌린지 21일차 (0) | 2022.02.13 |
패스트캠퍼스 챌린지 20일차 (0) | 2022.02.12 |
댓글