Part 1. Spring Framework
Ch 03. 예제를 만들며 이해하는 스프링 웹 어플리케이션 핵심 기술
10-1. 간단한 http 테스트 만들기(Postman을 활용한 http test 방법)
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 CreateDeveloper.Response createDeveloper(CreateDeveloper.Request request) {
validateCreateDeveloperRequest(request);
//business logic start
// Developer.DeveloperBuilder builder = Developer.builder();
// builder.developerLevel(request.getDeveloperLevel());
// builder.developerSkillType(request.getDeveloperSkillType());
// builder.experienceYears(request.getExperienceYears());
// builder.memberId(request.getMemberId());
// builder.name(request.getName());
// builder.age(request.getAge());
// Developer developer = builder
// .build();
Developer developer = Developer.builder()
.developerLevel(request.getDeveloperLevel())
.developerSkillType(request.getDeveloperSkillType())
.experienceYears(request.getExperienceYears())
.memberId(request.getMemberId())
.name(request.getName())
.age(request.getAge())
.build();
/* A -> B 1만원 송금 */
// A 계좌에서 1만원 줄임
developerRepository.save(developer);
// B 계좌에서 1만원 늘림림
//developerRepository.delete(developer1);
return CreateDeveloper.Response.fromEntity(developer);
}
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>
---------------------------------------------------------------------------------------------------------------------------------
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 27일차 (0) | 2022.02.19 |
---|---|
패스트캠퍼스 챌린지 26일차 (0) | 2022.02.18 |
패스트캠퍼스 챌린지 24일차 (0) | 2022.02.16 |
패스트캠퍼스 챌린지 23일차 (0) | 2022.02.15 |
패스트캠퍼스 챌린지 22일차 (0) | 2022.02.14 |
댓글