인프런 워밍업 클럽 스터디 0기
BE 4일차
@PostMapping("/api/v1/fruit")
public ResponseEntity saveFruit(@RequestBody Fruit fruit){
FruitResponse fruitResponse = new FruitResponse();
return new ResponseEntity(fruitResponse.save(fruit),HttpStatus.OK);
}
import lombok.Getter;
import org.springframework.jdbc.core.JdbcTemplate;
@Getter
public class FruitResponse {
private JdbcTemplate jdbcTemplate;
public FruitResponse() {}
public Fruit save(Fruit fruit){
String sql = "INSERT INTO fruit (name,warehousingDate,price) VALUES (?,?,?)";
jdbcTemplate.update(sql,fruit.getName(),fruit.getWarehousingDate(),fruit.getPrice());
return fruit;
}
}
import lombok.Getter;
import java.time.LocalDate;
@Getter
public class Fruit {
private String name;
private LocalDate warehousingDate;
private long price;
public Fruit(String name, LocalDate warehousingDate, long price) {
this.name = name;
this.warehousingDate = warehousingDate;
this.price = price;
}
}
@PutMapping("/api/v1/fruit")
public void buyFruit(@RequestBody Long id){
fruitResponse.isBuy(id);
}
alter table fruit add column sold boolean default false;
public void isBuy(long id){
String sql = "update fruit set sold = true where id=?";
jdbcTemplate.update(sql,id);
}
@GetMapping("/api/v1/fruit/stat")
public Response getFruitStatus(@RequestParam String name){
long salesAmount = fruitResponse.calcSalesAmount(name);
long notSalesAmount = fruitResponse.notCalcAmount(name);
return response;
}