[인프런 워밍업 클럽 0기] BE 6일차 과제
10개월 전
문제 1을 해결하기 전에 과제4에서 했던 코드를 조금 수정을 한뒤 과제를 진행 하였다.
문제1
DTO
@Data
@AllArgsConstructor
@RequiredArgsConstructor
public class FruitDTO {
private long id;
private String name;
private LocalDate warehousingDate;
private long price;
private boolean is_sold;
@Data
@AllArgsConstructor
public static class FruitResponse {
private long salesAmount;
private long notSalesAmount;
}
}
controller
@RequestMapping("/api/v1")
@RestController
public class FruitController {
private final FruitService fruitService;
public FruitController(FruitService fruitService) {
this.fruitService = fruitService;
}
@PostMapping("/fruit")
public void saveFruit(@RequestBody FruitDTO request) {
fruitService.save(request);
}
@PutMapping("/fruit")
public void sellFruit(@RequestBody FruitDTO request) {
fruitService.sell(request);
}
@GetMapping("/fruit/stat")
public FruitResponse getStat(@RequestParam String name) {
return fruitService.getStat(name);
}
}
Service
@Service
public class FruitService {
private final FruitMysqlRepository fruitRepository;
public FruitService(FruitMysqlRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
public void save(FruitDTO request) {
fruitRepository.save(request);
}
public void sell(FruitDTO request) {
if (fruitRepository.isFruitNotExist(request)) {
throw new IllegalArgumentException("존재하지 않는 과일 입니다.");
}
fruitRepository.sell(request);
}
public FruitResponse getStat(String name) {
return fruitRepository.getStat(name);
}
}
Repository
@Repository
public class FruitMysqlRepository implements FruitRepository {
private final JdbcTemplate jdbcTemplate;
public FruitMysqlRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void save(FruitDTO request) {
String sql = "INSERT INTO fruit (name, warehousingDate, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, request.getName(), request.getWarehousingDate(), request.getPrice());
}
public void sell(FruitDTO request) {
String sql = "UPDATE fruit SET is_sold = 1 WHERE id = ?";
jdbcTemplate.update(sql, request.getId());
}
public boolean isFruitNotExist(FruitDTO request) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
return jdbcTemplate.query(readSql, (rs, rowNum) -> 0, request.getId()).isEmpty();
}
public FruitResponse getStat(String name) {
String salesSql = "select sum(price) from fruit where is_sold = 1 group by name having name = ?";
String notSalesSql = "select sum(price) from fruit where is_sold = 0 group by name having name = ?";
long salesAmount = jdbcTemplate.queryForObject(salesSql, Long.class, name);
long notSalesAmount = jdbcTemplate.queryForObject(notSalesSql, Long.class, name);
return new FruitResponse(salesAmount, notSalesAmount);
}
}
문제2
FruitRepository interface
public interface FruitRepository {
void save(FruitDTO request);
void sell(FruitDTO request);
FruitResponse getStat(String name);
boolean isFruitNotExist(FruitDTO request);
}
수정 된 Service
@Service
public class FruitService {
private final FruitRepository fruitRepository;
public FruitService(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
public void save(FruitDTO request) {
fruitRepository.save(request);
}
public void sell(FruitDTO request) {
if (fruitRepository.isFruitNotExist(request)) {
throw new IllegalArgumentException("존재하지 않는 과일 입니다.");
}
fruitRepository.sell(request);
}
public FruitResponse getStat(String name) {
return fruitRepository.getStat(name);
}
}
FruitMemoryRepository
@Primary
@Repository
public class FruitMemoryRepository implements FruitRepository {
List<FruitDTO> fruitList = new ArrayList<>();
private long seq = 0L;
@Override
public void save(FruitDTO request) {
fruitList.add(new FruitDTO(
++seq,
request.getName(),
request.getWarehousingDate(),
request.getPrice(),
false));
}
@Override
public void sell(FruitDTO request) {
fruitList.get((int)request.getId()).set_sold(true);
}
public boolean isFruitNotExist(FruitDTO request) {
return fruitList.size() <= (int) request.getId();
}
@Override
public FruitResponse getStat(String name) {
long salesAmount = 0L;
long notSalesAmount = 0L;
for (FruitDTO fruit : fruitList) {
if (fruit.getName().equals(name)) {
if (fruit.is_sold()) {
salesAmount += fruit.getPrice();
} else {
notSalesAmount += fruit.getPrice();
}
}
}
return new FruitResponse(salesAmount, notSalesAmount);
}
}
댓글을 작성해보세요.