Inflearn

인프런 워밍업 클럽 0기 - BE 4일차 과제

느리지만 꾸준하게 2024. 2. 22. 23:33

1번

Controller

package com.group.libraryapp.controller.fruit;

import com.group.libraryapp.dto.fruit.FruitRequest;
import com.group.libraryapp.dto.fruit.FruitResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class FruitController {

    @PostMapping("/fruit")
    public FruitResponse fruitInfo(@RequestBody FruitRequest request) {
        return new FruitResponse(request.getName(), request.getWarehousingDate(), request.getPrice());
    }
}

 

 

FruitRequest

package com.group.libraryapp.dto.fruit;

import java.time.LocalDate;

public class FruitRequest {

    private long id;
    private String name;
    private LocalDate warehousingDate;
    private long price;

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public LocalDate getWarehousingDate() {
        return warehousingDate;
    }

    public long getPrice() {
        return price;
    }
}

 

 

FruitResponse

package com.group.libraryapp.dto.fruit;

import java.time.LocalDate;

public class FruitResponse {

    private String name;
    private LocalDate warehousingDate;
    private long price;

    public FruitResponse(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;
    }
}

 

 

자바의 정수 표현 방법 int vs long

  • 정수형 데이터 타입을 사용할 때에는 사용자가 필요한 데이터의 최소 / 최대 크기를 고려하여 결정
  • 타입을 표현할 수 있는 범위를 벗어난 데이터를 저장하면 오버플로우가 발생하고 전혀 다른 값이 저장될 수 있다.
  • 오버플로우 (overflow) : 해당 타입이 표현할 수 있는 최대 표현 범위보다 큰 수를 저장할 때 발생하는 현상
  • 가격이라는 데이터의 특성 고려해서 long 사용

 

 

POST 결과 200 OK

 

참고 : https://velog.io/@iams0m/%EC%B6%94%EA%B0%80%EC%A0%81%EC%9D%B8-API-%EB%A7%8C%EB%93%A4%EC%96%B4%EB%B3%B4%EA%B8%B0

 

 

1번 다른 예시

FruitController

package com.group.libraryapp.controller.fruit;

import com.group.libraryapp.dto.fruit.FruitCreateRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
public class FruitController {

    private final JdbcTemplate jdbcTemplate;

    public FruitController(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @PostMapping("/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());
    }
}

 

FruitCreateRequest

package com.group.libraryapp.dto.fruit;

import java.time.LocalDate;

public class FruitCreateRequest {

    private String name;

    private LocalDate warehousingDate;
    private int price;

    public FruitCreateRequest(String name, LocalDate warehousingDate, int price) {
        this.name = name;
        this.warehousingDate = warehousingDate;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public LocalDate getWarehousingDate() {
        return warehousingDate;
    }

    public int getPrice() {
        return price;
    }
}

 

 

error 해결중,,,

 

참고 : https://velog.io/@jo_kim/inflearnbestudy4

 

 

2번

@PutMapping("/fruit")
    public void updateFruit(@RequestBody FruitRequest request) {
        String readSql = "SELECT sold_quantity FROM fruit WHERE id = ?";
        List<Long> result = jdbcTemplate.query(readSql, (rs, rowNum) -> {
            return rs.getLong("sold_quantity");
        }, request.getId());

        String sql = "UPDATE fruit SET sold_quantity = ? WHERE id = ?";
        jdbcTemplate.update(sql, result.get(0) + 1 ,request.getId());
    }

 

 

 

3번

FruitController

@GetMapping("/fruit/stat")
    public FruitStatResponse getFruitStat(@RequestParam String name) {
        String soldSql = "SELECT SUM(price) AS salesAmount FROM fruit WHERE sold_quantity > 0 AND name = ?";
        String notSoldSql = "SELECT SUM(price) as notSalesAmount from fruit where sold_quantity > 0 and name = ?";

        Long salesAmount = jdbcTemplate.queryForObject(soldSql, (rs, rowNum) -> rs.getLong("salesAmount"), name);
        Long notSalesAmount = jdbcTemplate.queryForObject(notSoldSql, (rs, rowNum) -> rs.getLong("notSalesAmount"), name);

        return new FruitStatResponse(salesAmount, notSalesAmount);

    }

 

 

FruitStatResponse

package com.group.libraryapp.dto.fruit;

public class FruitStatResponse {

    private final long salesAmount;
    private final long notSalesAmount;

    public FruitStatResponse(long salesAmount, long notSalesAmount) {
        this.salesAmount = salesAmount;
        this.notSalesAmount = notSalesAmount;
    }

    public long getSalesAmount() {
        return salesAmount;
    }

    public long getNotSalesAmount() {
        return notSalesAmount;
    }
}