본문 바로가기
[패스트캠퍼스] Spring

패스트캠퍼스 챌린지 31일차

by 엑츄얼리 2022. 2. 23.

Part 1. Spring Framework

 Ch 04. 예외처리

  01. 예외처리를 활용한 서비스 로직 리팩토링_01

  - 문제점

    Controller 의 역할 : Presentation layer 

     응답을 잘받고 요청에 대한 반응을 전달

     클래스에 역할에 따라 메소드를 잘 분류하지 않으면, 클래스의 용도가 불분명해져서 유지보수성이 낮아진다.

 

  * 좋지 않은 코드 (DeveloperValidationDto Class와 validateCreateDeveloperRequest Method에 집중)

.
.
.
public DeveloperValidationDto validateCreateDeveloperRequest(CreateDeveloper.Request request) {
    DeveloperValidationDto developerValidationDto = null;
    //business validation
    developerValidationDto = validateDeveloperLevel(
            request.getDeveloperLevel(),
            request.getExperienceYears()
    );
    try{
        if(developerRepository.findByMemberId(request.getMemberId()).isPresent())
        developerValidationDto = new DeveloperValidationDto(
                DUPLICATED_MEMBER_ID,
                DUPLICATED_MEMBER_ID.getMessage()
        );
    }catch(Exception e){
        log.error(e.getMessage(), e);
        developerValidationDto = new DeveloperValidationDto(
                DUPLICATED_MEMBER_ID,
                DUPLICATED_MEMBER_ID.getMessage()
        );
    }


    return null;
}
.
.
.
//아래로 수정
    private void validateCreateDeveloperRequest(CreateDeveloper.Request request) {
        //business validation
        validateDeveloperLevel(
                request.getDeveloperLevel(),
                request.getExperienceYears()
        );

        developerRepository.findByMemberId(request.getMemberId())
                .ifPresent((developer -> {
                    throw new DMakerException(DUPLICATED_MEMBER_ID);
                }));
    }

<dMakerService.java>

 

package com.fastcampus.programmin.dmaker.dto;

import com.fastcampus.programmin.dmaker.exception.DMakerErrorCode;
import lombok.*;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class DeveloperValidationDto {
    private DMakerErrorCode errorCode;
    private String errorMessage;
}

<DeveloperValidationDto.java>

 

.
.
.
@PostMapping("/create-developer")
public CreateDeveloper.Response createDevelopers(
        @Valid @RequestBody CreateDeveloper.Request request
) {
    log.info("request : {}", request);
    //dMakerService.validateCreateDeveloperRequest(request);

    DeveloperValidationDto developerValidationDto =
            dMakerService.validateCreateDeveloperRequest(request);

    if(developerValidationDto != null){
        return CreateDeveloper.Response.builder()
                .errorCode(developerValidationDto.getErrorCode())
                .errorMessage(developerValidationDto.getErrorMessage())
                .build();
    }

    return dMakerService.createDeveloper(request);

    //return Collections.singletonList("Olaf");
}
.
.
.
//아래로 수정
    @PostMapping("/create-developer")
    public CreateDeveloper.Response createDevelopers(
            @Valid @RequestBody CreateDeveloper.Request request
    ) {
        log.info("request : {}", request);
        dMakerService.validateCreateDeveloperRequest(request);

        return dMakerService.createDeveloper(request);
    }

<DMakerController.java>

 


https://bit.ly/37BpXic

댓글