목차
1. 이미 존재하는 정보인지 = verifyExistObject // Create 메서드에 활용
2. 이미 등록된 정보인지 = findVerifiedObject // Update, Find, Delete 메서드에 활용
3. 주문된 커피 정보 조회
4. Create 메서드
5. Update 메서드
6. find 메서드
7. find's 메서드
8. delete 메서드
1. 이미 존재하는건지 확인 // verifyExistObject // Create 메서드에 활용
VerifyExistCoffee (coffeeCode) 메서드
private void verifyExistCoffee(String coffeeCode) {
Optional<Coffee> coffee = coffeeRepository.findByCoffeeCode(coffeeCode);
if(coffee.isPresent())
throw new BusinessLogicException(ExceptionCode.COFFEE_CODE_EXISTS);
}
BusinessLogicException 클래스
package com.codestates.exception;
import lombok.Getter;
public class BusinessLogicException extends RuntimeException {
@Getter
private ExceptionCode exceptionCode;
public BusinessLogicException(ExceptionCode exceptionCode) {
super(exceptionCode.getMessage());
this.exceptionCode = exceptionCode;
}
}
ExceptionCode 클래스
package com.codestates.exception;
import lombok.Getter;
// TODO 수정됨
public enum ExceptionCode {
MEMBER_NOT_FOUND(404, "Member not found"),
MEMBER_EXISTS(409, "Member exists"),
COFFEE_NOT_FOUND(404, "Coffee not found"),
COFFEE_CODE_EXISTS(409, "Coffee Code exists"),
ORDER_NOT_FOUND(404, "Order not found"),
CANNOT_CHANGE_ORDER(403, "Order can not change"),
NOT_IMPLEMENTATION(501, "Not Implementation");
@Getter
private int status;
@Getter
private String message;
ExceptionCode(int code, String message) {
this.status = code;
this.message = message;
}
}
2. 이미 등록된 정보인지 확인 // findVerifiedObject // Update, Find, Delete 메서드에 활용
public Coffee findVerifiedCoffee(long coffeeId) {
Optional<Coffee> optionalCoffee = coffeeRepository.findById(coffeeId);
Coffee findCoffee =
optionalCoffee.orElseThrow(() ->
new BusinessLogicException(ExceptionCode.COFFEE_NOT_FOUND));
return findCoffee;
}
A. BusinessLogicException 코드
package com.codestates.exception;
import lombok.Getter;
public class BusinessLogicException extends RuntimeException {
@Getter
private ExceptionCode exceptionCode;
public BusinessLogicException(ExceptionCode exceptionCode) {
super(exceptionCode.getMessage());
this.exceptionCode = exceptionCode;
}
}
B. ExceptionCode 클래스
package com.codestates.exception;
import lombok.Getter;
// TODO 수정됨
public enum ExceptionCode {
MEMBER_NOT_FOUND(404, "Member not found"),
MEMBER_EXISTS(409, "Member exists"),
COFFEE_NOT_FOUND(404, "Coffee not found"),
COFFEE_CODE_EXISTS(409, "Coffee Code exists"),
ORDER_NOT_FOUND(404, "Order not found"),
CANNOT_CHANGE_ORDER(403, "Order can not change"),
NOT_IMPLEMENTATION(501, "Not Implementation");
@Getter
private int status;
@Getter
private String message;
ExceptionCode(int code, String message) {
this.status = code;
this.message = message;
}
}
3. 주문된 커피 정보 조회
// (2) 주문에 해당하는 커피 정보 조회
public List<Coffee> findOrderedCoffees(Order order) {
return order.getOrderCoffees()
.stream()
.map(coffeeRef -> findCoffee(coffeeRef.getCoffeeId()))
.collect(Collectors.toList());
}
4. 데이터 생성 Create // @PostMapping 핸들러메서드
public Coffee createCoffee(Coffee coffee) {
// (1) 커피 코드를 대문자로 변경
String coffeeCode = coffee.getCoffeeCode().toUpperCase();
// 이미 등록된 커피 코드인지 확인
verifyExistCoffee(coffeeCode);
coffee.setCoffeeCode(coffeeCode);
return coffeeRepository.save(coffee);
}
5. 데이터 수정 Update // @PatchMapping 핸들러메서드
public Coffee updateCoffee(Coffee coffee) {
// 조회하려는 커피가 검증된 커피인지 확인(존재하는 커피인지 확인 등)
Coffee findCoffee = findVerifiedCoffee(coffee.getCoffeeId());
Optional.ofNullable(coffee.getKorName())
.ifPresent(korName -> findCoffee.setKorName(korName));
Optional.ofNullable(coffee.getEngName())
.ifPresent(engName -> findCoffee.setEngName(engName));
Optional.ofNullable(coffee.getPrice())
.ifPresent(price -> findCoffee.setPrice(price));
return coffeeRepository.save(findCoffee);
}
6. 특정 데이터 조회 find 메서드 // @GetMapping {"/URI"} 핸들러메서드
public Coffee findCoffee(long coffeeId) {
return findVerifiedCoffeeByQuery(coffeeId);
}
7. 전체 데이터 조회 find's 메서드 // @GetMapping 핸들러메서드
public List<Coffee> findCoffees() {
return (List<Coffee>) coffeeRepository.findAll();
}
8. 데이터 삭제 delete 메서드 // @DeleteMapping 핸들러메서드
public void deleteCoffee(long coffeeId) {
Coffee coffee = findVerifiedCoffee(coffeeId);
coffeeRepository.delete(coffee);
}
'백엔드 학습 과정 > Section 3 [Spring MVC, JDBC, JPA, RestDo' 카테고리의 다른 글
JPA에서 em을 활용한 회원과 주문 정보 저장 및 호출 흐름 (0) | 2023.01.01 |
---|---|
Spring JPA 기능 코드 (0) | 2023.01.01 |
#4. Spring Data JDBC (0) | 2022.12.23 |
#3. Spring 예외 처리 (0) | 2022.12.23 |
#2. Spring MVC [Service 계층] - 역할, 생성, 적용, 필요 애너테이션 (0) | 2022.12.20 |