순수한 Java 코드만 이용하여 JPA 익히기
클래스 아래쪽으로 Entity, Repository, Service, Controller 전부 생성
제일 큰 test class 안쪽으로 Beverage로 돌려보기
public class BeverageController {
private final BeverageService beverageService;
public void deleteDrink(String name) {
Beverage beverage = beverageService.deletebeverage(name);
}
}
Controller 단에 name을 기준으로 delete 하는 메서드 생성
그러면 Service단에 deletebeverage라는 메서드가 있어야 함
public class BeverageService {
private final BeverageRepository beverageRepository;
public BeverageService(BeverageRepository beverageRepository) {
this.beverageRepository = beverageRepository;
}
public Beverage createDrink(String name, Long price, Date date, Long ea) {
Beverage beverage = new Beverage(name, price, date, ea);
beverage.setName(name);
beverage.setPrice(price);
beverage.setEa(ea);
beverage.setDate(date);
return beverageRepository.save(beverage);
}
public Beverage getBeverage(String name) {
return beverageRepository.findDrinkByName(name)
.orElseThrow(() -> new RuntimeException("해당 제품 없음"));
}
public Beverage deletebeverage(String name) {
Beverage beverage = getBeverage(name);
beverageRepository.delete(beverage);
return beverage;
}
}
이번엔 Repository단에 delete라는 메서드가 있어야 함
public class BeverageRepository {
public final Map<String, Beverage> assistance = new HashMap<>();
public final Map<Long, Beverage> assistance2 = new HashMap<>();
public final Map<Date, Beverage> assistance3 = new HashMap<>();
public List<Beverage> getAllDrinkByName() {
return new ArrayList<>(assistance2.values());
}
public Beverage save(Beverage beverage) {
assistance2.put(beverage.getId(), beverage);
return beverage;
}
public void delete(Beverage beverage) {
assistance.remove(beverage.getName());
}
}