Part 1. Spring Framework
Ch 03. 예제를 만들며 이해하는 스프링 웹 어플리케이션 핵심 기술
10. 간단한 http 테스트 만들기
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>
package com.fastcampus.programmin.dmaker.controller;
import com.fastcampus.programmin.dmaker.dto.CreateDeveloper;
import com.fastcampus.programmin.dmaker.service.dMakerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Slf4j
@RestController //사용자의 요청을 받아 json 형태로 응답을 반환 //ctrl + 클릭
@RequiredArgsConstructor
// DMakerController(Bean) DMakerService(Bean) DeveloperRepository(Bean)
//===========================Spring Application =============================
//
//
public class DMakerController {
private final dMakerService dMakerService;
@GetMapping("/developers") //사용자 요청이 /developers를 통해 들어온다면 (ex] GET /developers HTTP/1.1)
//
public List<String> getAllDevelopers() {
// GET /Developers HTTP/1.1
log.info("GET /developers HTTP/1.1");
return Arrays.asList("snow", "elsa", "Olaf");
}
@PostMapping("/create-developer")
public CreateDeveloper.Response createDevelopers(
@Valid @RequestBody CreateDeveloper.Request request
)
{
log.info("request : {}", request);
return dMakerService.createDeveloper(request);
//return Collections.singletonList("Olaf");
}
}
<DMakerController.java>
### Send POST request with json body
POST http://localhost:8080/create-developer
Content-Type: application/json
{
"developerLevel": "JUNIOR",
"developerSkillType": "FRONT_END",
"experienceYears": 2,
"memberId": "Micky",
"name": "mouse",
"age": 20
}
<dev.create.http>
---------------------------------------------------------------------------------------------------------------------------------
https://bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 26일차 (0) | 2022.02.18 |
---|---|
패스트캠퍼스 챌린지 25일차 (0) | 2022.02.17 |
패스트캠퍼스 챌린지 23일차 (0) | 2022.02.15 |
패스트캠퍼스 챌린지 22일차 (0) | 2022.02.14 |
패스트캠퍼스 챌린지 21일차 (0) | 2022.02.13 |
댓글