inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[인프런 워밍업 클럽] BE 0기 과제 #4 과일가게API

박상범
0

문제1.

과일의 정보를 입력하기 위해 데이터베이스와 테이블을 만들어주었다.

image

그 후 요청한 과일 정보를 저장하기 위한 FruitSaveRequest클래스를 만들어준다.

package com.group.libraryapp.dto.fruit.request;

import java.time.LocalDate;

public class FruitSaveRequest {
    private String name;
    private LocalDate warehousingDate;
    private long price;

    public String getName() {
        return name;
    }

    public LocalDate getWarehousingDate() {
        return warehousingDate;
    }

    public long getPrice() {
        return price;
    }
}

image

@PutMapping("/api/v1/fruit")
    public void recordFruit(@RequestBody FruitSoldRequest request) {
        String readSql = "SELECT * FROM fruit WHERE id = ?";
        boolean isFruitNoException = jdbcTemplate.query(readSql, (rs, rowNum) -> 0, request.getId()).isEmpty();
        if (isFruitNoException) {
            throw new IllegalArgumentException();
        }
        String sql = "UPDATE fruit SET sold = 1 WHERE id = ?";
        jdbcTemplate.update(sql, request.getId());
    }

여기서 FruitSoldRequest는 id만을 가진 클래스이다.
image

문제 3.

@GetMapping("/api/v1/fruit/stat")
    public FruitResponse getPrice(@RequestParam String name){
        String readSql = "SELECT * FROM fruit WHERE name = ?";
        boolean isFruitNoException = jdbcTemplate.query(readSql, (rs, rowNum) -> 0, name).isEmpty();
        if (isFruitNoException) {
            throw new IllegalArgumentException();
        }
        String sql = "SELECT sum(price) FROM fruit WHERE name = ? GROUP BY sold";
        List<Long> priceSum = jdbcTemplate.query(sql, (rs, rowNum) -> {
            long sum = rs.getLong("sum(price)");
            return sum;
        }, name);
        return new FruitResponse(priceSum.get(0),priceSum.get(1));
    }

 

image

답변 0