[인프런 워밍업 스터디 클럽] 백엔드 과제4
9개월 전
문제 1
private final FruitService fruitService;
public FruitController(FruitService fruitService) {
this.fruitService = fruitService;
}
@PostMapping("/api/v1/fruit")
public void saveFruit(@RequestBody FruitCreateRequest request) {
fruitService.saveFruit(request);
}
public class FruitCreateRequest {
private String name;
private LocalDate warehousingDate;
private long price;
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
}
private final FruitRepository fruitRepository;
public FruitService(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
public void saveFruit(FruitCreateRequest request) {
fruitRepository.saveFruit(request.getName(), request.getWarehousingDate(), request.getPrice());
}
private final JdbcTemplate jdbcTemplate;
public FruitRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveFruit(String name, LocalDate warehousingDate, long price) {
String sql = "INSERT INTO fruit (name, stocked_date, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, name, warehousingDate, price);
}
문제 2
@PutMapping("/api/v1/fruit")
public void soldFruit(@RequestBody FruitUpdateRequest request) {
fruitService.updateFruit(request);
}
public class FruitUpdateRequest {
private long id;
private String name;
private LocalDate warehousingDate;
private long price;
private String status;
public long getId() {
return id;
}
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
public String getStatus() {
return status;
}
}
public void updateFruit(FruitUpdateRequest request) {
if (fruitRepository.isFruitNotExist(request.getId())) {
throw new IllegalArgumentException();
}
fruitRepository.updateFruit(request.getId());
}
public void updateFruit(long id) {
String sql = "UPDATE fruit SET status = 1 WHERE id = ?";
jdbcTemplate.update(sql, id);
}
public boolean isFruitNotExist(long id) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
return jdbcTemplate.query(readSql, (rs, rowNum) -> 0, id).isEmpty();
}
문제 3
@GetMapping("/api/v1/fruit/stat")
public ResponseEntity<FruitStatResponse> getFruitStat(@RequestParam String name) {
FruitStatResponse response = fruitService.getFruitStat(name);
return new ResponseEntity<>(response, HttpStatus.OK);
}
public class FruitStatResponse {
private long salesAmount;
private long notSalesAmount;
// Constructor
public FruitStatResponse(long salesAmount, long notSalesAmount) {
this.salesAmount = salesAmount;
this.notSalesAmount = notSalesAmount;
}
// Getters and setters
public long getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(long salesAmount) {
this.salesAmount = salesAmount;
}
public long getNotSalesAmount() {
return notSalesAmount;
}
public void setNotSalesAmount(long notSalesAmount) {
this.notSalesAmount = notSalesAmount;
}
}
public FruitStatResponse getFruitStat(String fruitName) {
long salesAmount = fruitRepository.sumPriceByFruitNameAndIsSoldTrue(fruitName);
long notSalesAmount = fruitRepository.sumPriceByFruitNameAndIsSoldFalse(fruitName);
return new FruitStatResponse(salesAmount, notSalesAmount);
}
public long sumPriceByFruitNameAndIsSoldTrue(String name) {
String sql = "SELECT SUM(price) FROM fruit WHERE name = ? AND status = 1";
return jdbcTemplate.queryForObject(sql, new Object[] {name}, Long.class);
}
public long sumPriceByFruitNameAndIsSoldFalse(String name) {
String sql = "SELECT SUM(price) FROM fruit WHERE name = ? AND status = 0";
return jdbcTemplate.queryForObject(sql, new Object[] {name}, Long.class);
}