
๋ ์๋ฌด๊ฒ๋ ๋ชจ๋ฅด๋ ๊ฐ์๋ค.
์ง์ง๋ค. ๊น์ํ์ค ์๋ฐ ์
๋ฌธํธ ํ๋๋ฃ๊ณ ์ด ์คํฐ๋๋ฅผ ์ ์ฒญํ๋ค.
๊ทธ๋์ ๋๋ก ๋์๊ฐ๋ค๋ฉด ๋ฉฑ์ด์ ์ก๊ณ ์ถ๋ค.
์ด์๋ถ์ ์ด์งธ์ ์งธ ๊น์ํ์ค ์๋ฐ ๊ธฐ๋ณธํธ์ด๋ ๋ณํํ๋ฉด์ ๊ณผ์ ๋ฅผ ํ๊ณ ์๋ค.
๊นํ๋ธ๋ก ์ฌ๋ฆฌ๊ณ ์ถ์๋ฐ ๊นํ๋ธ๋ฅผ ์ ๋ชป์จ์ ๊ทธ๋ฅ ๋ธ๋ก๊ทธ๋ก ๊ณ์ ์ฌ๋ฆฌ๊ณ ์๋ค.
์ํด๋ฅผ ๋ถํ๋๋ ค์..๐
๊ณผ์ผ์ ๋ณด๋ฅผ ์ ์ฅํ๋ api๋ฅผ ๋ง๋ค์ด๋ณด์.
name,warehousingDate,price๋ฅผ body๋ก ๋์ ธ์ฃผ๋ฉด ์ ์ฅํ๋ api์ด๋ค.
1๋ฒ ๋ฌธ์ ํ์ด
1) 1๋ฒ์์๋ PostMapping์ด๋ ธํ ์ด์ ์ ์ฌ์ฉ.
2) sql์ด๋ผ๋ String ๋ณ์์ db์ ๋ฐ์ดํฐ๋ฅผ insertํ ์ ์๋ ๊ตฌ๋ฌธ์ ์์ฑ.
3) body๋ก ์์ฒญ๋ ์ ์ฅํด์ผ ํ ๋ฐ์ดํฐ๋ SaveFruitRequest๋ผ๋ ํด๋์ค๋ฅผ ๊ฑฐ์ณ ๊ฐ์ฒด๋ก ๊ฐ์ผ ํ, getter๋ก ์ ๊ทผํด values ์ ์ธ์๋ก ์ ๋ฌ.
ํ๋ฆฐ ๊ณผ์ผ์ ์ ๋ณด๋ฅผ ๊ธฐ๋กํ๋ api๋ฅผ ๋ง๋ค์ด๋ณด์.
ํ๋ฆฐ ๊ณผ์ผ์ body๋ฅผ ํตํด id๊ฐ์ผ๋ก ์์ฒญ์ ์ค๋ค.
2๋ฒ ๋ฌธ์ ํ์ด
1) 2๋ฒ์์๋ PutMapping ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉ.
2) fruit db ํ ์ด๋ธ์๋ ํน์ id์ ๊ณผ์ผ์ด ํ๋ ธ๋์ง ํ์ธํ ์ ์๋ boolean ํ์ ์ "isSell" ์นผ๋ผ์ ๋ง๋ฌ.
3) ๋ณธ๊ฒฉ์ ์ผ๋ก ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ธฐ์ ์์,์๋ id๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฒ๊ทธ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด isFruitNotExist๋ผ๋ ๋ณ์๋ฅผ ๋ง๋ค์ด body๋ก ์ ๋ฌ๋ id์ ํด๋นํ๋ ๊ฐ์ด ์๋์ง๋ฅผ ํ์ธํ๊ณ , ์๋ค๋ฉด ์์ธ์ฒ๋ฆฌํจ.
3) sql์ด๋ผ๋ String ๋ณ์์ db์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฐ์ดํธ ํ ์ ์๋ ๊ตฌ๋ฌธ์ ์์ฑ.
4) body๋ก ์์ฒญ๋ id๋ SaveFruitRequest๋ผ๋ ํด๋์ค๋ฅผ ๊ฑฐ์ณ ๊ฐ์ฒด๋ก ๊ฐ์ผ ํ, getter๋ก ์ ๊ทผํด jdbcTemplate.update()๊ตฌ๋ฌธ์ ์ธ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ. (*๋๋ฒ์งธ ์ธ์์๋ true๋ฅผ ๋ฃ๋๋ค.)
ํน์ ๊ณผ์ผ์ ๊ธฐ์ค์ผ๋ก ํ๋ฆฐ๊ธ์ก, ํ๋ฆฌ์ง ์์ ๊ธ์ก์ ์ดํฉ์ ์กฐํํ๋ api๋ฅผ ๋ง๋ค์ด๋ณด์.
3๋ฒ ๋ฌธ์ ํ์ด
1) 3๋ฒ์์๋ GetMapping ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉ.
2) ์์ 2๋ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก, if๋ฌธ์ผ๋ก name์ด ์กด์ฌํ์ง ์๋ ๋ฒ๊ทธ๋ฅผ ์ฒ๋ฆฌํจ.
3) isSellsql์ด๋ผ๋ String๋ณ์์ ์์ฒญ๋ฐ๋ name๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ select ๊ตฌ๋ฌธ์ ์์ฑ.
4) fruitList ๋ณ์์ price, isSell์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๋ฆฌ์คํธ ๊ฐ์ฒด๋ฅผ ์ ์ฅ.
5) salesTotal,notSalesTotal๋ณ์๋ฅผ ๋ง๋ค๊ณ , ์์ fruitList๋ฅผ for๋ฌธ์ผ๋ก ๋๋ฉฐ isSell() getter์ ๊ฐ์ด true์ธ์ง๋ฅผ ํ์ธํ๊ณ , true์ด๋ฉด salesTotal์ false๋ผ๋ฉด notSalesTotal์ price๋ฅผ ๋์ ํด ์ ์ฅ.
6) ๋ง์ง๋ง์ผ๋ก ์ดํฉ์ด ๊ณ์ฐ๋ ๋ ๋ณ์๋ฅผ FruitFinalResponseํด๋์ค๋ฅผ ๊ฑฐ์ณ ๊ฐ์ฒด๋ก ๊ฐ์ธ ๋ฆฌํด.
โ ์ ์ฒด ์ฝ๋
package com.group.libraryapp.controller.fourthAssignment;
import com.group.libraryapp.dto.fourth.request.SaveFruitRequest;
import com.group.libraryapp.dto.fourth.response.FruitFinalResponse;
import com.group.libraryapp.dto.fourth.response.FruitResponse;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class FruitController {
private final JdbcTemplate jdbcTemplate;
public FruitController(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
/////////////// 1๋ฒ
@PostMapping("/api/v1/fruit")
public void saveFruitInfo(@RequestBody SaveFruitRequest request) {
String sql = "INSERT INTO fruit(name,warehousingDate,price) VALUES(?,?,?)";
jdbcTemplate.update(sql,request.getName(),request.getWarehousingDate(),request.getPrice());
}
/////////////// 2๋ฒ
@PutMapping("/api/v1/fruit")
public void updateFruit(@RequestBody SaveFruitRequest request){
String readSql ="SELECT * from fruit WHERE id = ?";
//query() ํตํด ๋์ค๋๊ฒ ๋ฆฌ์คํธ์ธ๋ฐ, ๋ญ๋ผ๋ ์์ผ๋ฉด 0์ ๋ฃ์ด์ค๊ฑฐ๊ณ ์กฐํ๊ฐ ์๋๋ฉด ๋น์ด์์๊ฑฐ์.
boolean isFruitNotExist = jdbcTemplate.query(readSql, (rs, rowNum) -> 0,request.getId()).isEmpty();
if(isFruitNotExist){
throw new IllegalArgumentException();
}
String sql = "UPDATE fruit SET isSell = ? WHERE id = ?";
jdbcTemplate.update(sql,true,request.getId());
}
/////////////// 3๋ฒ
@GetMapping("/api/v1/fruit/stat")
public FruitFinalResponse getFruits(@RequestParam String name) {
String sql = "SELECT * FROM fruit WHERE name = ?";
//๊ฐ์ ธ์จ ๋ฐ์ดํฐ ๊ฒฐ๊ณผ rs ๋ฅผ ์ฐ๋ฆฌ๊ฐ ์ํ๋ userResponseํํ๋ก ๋ฐ๊ฟ์ค.์ด๊ฑฐ๋ ๊ฐ์ฒด ์ฐธ์กฐ๊ฐ์ธ๋ฐ, ์ ์ฒด์์ ๋ฐํํ๋๊ฑด ๊ฒฐ๊ตญ query๋ก ๊ฐ์ธ์ ๋์ค๋ฏ๋ก ๋ฆฌ์คํธ ํ์
.
boolean isFruitNotExist = jdbcTemplate.query(sql, (rs, rowNum) -> 0,name).isEmpty();
if(isFruitNotExist){
throw new IllegalArgumentException();
}else{
String isSellsql = "SELECT * FROM fruit WHERE name = ?";
List<FruitResponse> fruitList = jdbcTemplate.query(isSellsql,(rs, rowNum) -> {
long price = rs.getLong("price");
boolean isSell = rs.getBoolean("isSell");
return new FruitResponse(price,isSell);
},name);
long salesTotal=0;
long notSalesTotal=0;
for(int i=0;i<fruitList.size();i++){
if(fruitList.get(i).isSell()==true){
salesTotal = salesTotal + fruitList.get(i).getSalesAmount();
}else{
notSalesTotal = notSalesTotal + fruitList.get(i).getSalesAmount();
}
}
return new FruitFinalResponse(salesTotal,notSalesTotal);
}
}
}
package com.group.libraryapp.dto.fourth.request;
import java.time.LocalDate;
public class SaveFruitRequest {
private String name;
private LocalDate warehousingDate;
private long price;
private int id;
public SaveFruitRequest(String name, String warehousingDate, long price,int id) {
this.name = name;
this.warehousingDate = LocalDate.parse(warehousingDate);
this.price = price;
this.id = id;
}
public SaveFruitRequest(){
}
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
public long getId() {
return id;
}
}
package com.group.libraryapp.dto.fourth.response;
public class FruitResponse {
private long salesAmount;
private boolean isSell;
public FruitResponse(long salesAmount,boolean isSell) {
this.salesAmount = salesAmount;
this.isSell = isSell;
}
public long getSalesAmount() {
return salesAmount;
}
public boolean isSell() {
return isSell;
}
}
package com.group.libraryapp.dto.fourth.response;
import java.util.List;
public class FruitFinalResponse {
private long salesAmount;
private long notSalesAmount;
public FruitFinalResponse(long salesAmount, long notSalesAmount) {
this.salesAmount = salesAmount;
this.notSalesAmount = notSalesAmount;
}
public long getSalesAmount() {
return salesAmount;
}
public long getNotSalesAmount() {
return notSalesAmount;
}
}