[인프런 워밍업 클럽 0기] 4일차 과제
9개월 전
<과제>
문제 1. 우리는 작은 과일 가게를 운영하고 있습니다. 과일 가게에 입고된 "과일 정보"를 저장하는 API를 만들어 봅시다.
[조건]
HTTP method :
POST
HTTP path :
/api/v1/fruit
HTTP 요청 Body
{
"name": String,
"warehousingDate": LocalDate,
"price": long
}
[예시]
{
"name": "사과",
"warehousingDate": "2024-02-01",
"price": 5000
}
응답 : 성공 시 200 OK
[문제 풀이]
FruitController
package com.group.libraryapp.controller.assignment;
import com.group.libraryapp.dto.fruit.FruitCreateRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FruitController {
private final JdbcTemplate jdbcTemplate;
public FruitController(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@PostMapping("/api/v1/fruit")
public void saveFruit(@RequestBody FruitCreateRequest request) {
String sql = "INSERT INTO fruit (name, warehousingDate, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, request.getName(), request.getWarehousingDate(), request.getPrice());
}
}
FruitRequest
package com.group.libraryapp.dto.fruit;
import java.time.LocalDate;
public class FruitCreateRequest {
private String name;
private LocalDate warehousingDate;
private long price;
public FruitCreateRequest(String name, LocalDate warehousingDate, long price) {
this.name = name;
this.warehousingDate = warehousingDate;
this.price = price;
}
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
}
문제 2. 과일이 팔리게 되면, 우리 시스템에 팔린 과일 정보를 기록해야 합니다.
[조건]
HTTP method :
PUT
HTTP path :
/api/v1/fruit
HTTP 요청 Body
{
"id": long
}
[예시]
{
"id": 3
}
응답 : 성공 시 200 OK
[문제 풀이]
FruitController
@PutMapping("/api/v1/fruit")
public void updateFruit(@RequestBody FruitUpdateRequest request) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
boolean isFruitNotExist = jdbcTemplate.query(readSql, (rs, rowNum) -> 0, request.getId()).isEmpty();
if (isFruitNotExist) {
throw new IllegalArgumentException();
}
String sql = "UPDATE fruit SET name = ?, warehousingDate = ?, price = ? WHERE id = ?";
jdbcTemplate.update(sql, request.getName(), request.getWarehousingDate(), request.getPrice(), request.getId());
}
FruitUpdateRequest
package com.group.libraryapp.dto.fruit;
import java.time.LocalDate;
public class FruitUpdateRequest {
private long id;
private String name;
private LocalDate warehousingDate;
private long price;
public FruitUpdateRequest(long id, String name, LocalDate warehousingDate, long price) {
this.id = id;
this.name = name;
this.warehousingDate = warehousingDate;
this.price = price;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
}
문제 3. 우리는 특정 과일을 기준으로 팔린 금액, 팔리지 않은 금액을 조회하고 싶습니다.
[조건]
HTTP method :
GET
HTTP path :
/api/v1/fruit/stat
HTTP query
name : 과일 이름
HTTP 응답 body
{
"salesAmount": long,
"notSalesAmount": long
}
[예시]
GET /api/v1/fruit/stat?name=사과
{
"salesAmount": 6000,
"notSalesAmount": 4000
}
[문제 풀이]
FruitController
@GetMapping("api/v1/fruit/stat")
public FruitResponse getSalesTotalPrice(@RequestParam("name") String fruitName) {
String sql = "SELECT * FROM fruit WHERE name = ?";
List<Fruit> result = jdbcTemplate.query(sql, new Object[]{fruitName}, (RowMapper<Fruit>) (rs, rowNum) -> {
return new Fruit(rs.getString("name"), rs.getLong("price"), rs.getString("sell_status"));
});
long sellingPrice = 0L;
long havingPrice = 0L;
for (final Fruit fruit : result) {
if (fruit.getStatus().equals("HAVING")) {
havingPrice+= fruit.getPrice();
} else if (fruit.getStatus().equals("SELLING")) {
sellingPrice+= fruit.getPrice();
}
}
return new FruitResponse(sellingPrice, havingPrice);
}
Fruit
public static class Fruit {
private String name;
private long price;
private String status;
public Fruit(final String name, final long price, final String status) {
this.name = name;
this.price = price;
this.status = status;
}
public String getName() {
return name;
}
public long getPrice() {
return price;
}
public String getStatus() {
return status;
}
}
FruitResponse
public static class FruitResponse {
private long salesAmount;
private long notSalesAmount;
public FruitResponse(final long salesAmount, final long notSalesAmount) {
this.salesAmount = salesAmount;
this.notSalesAmount = notSalesAmount;
}
public long getSalesAmount() {
return salesAmount;
}
public long getNotSalesAmount() {
return notSalesAmount;
}
}
댓글을 작성해보세요.