[인프런 워밍업 클럽 BE 0기] 6일차 과제

[인프런 워밍업 클럽 BE 0기] 6일차 과제

문제 1

과제 #4에서 만들었던 API를 강의 내용 처럼 Controller - Service - Repository로 분리해보세요!

  • Controller

@RestController
public class FruitController {

    private final JdbcTemplate jdbcTemplate;
    private final FruitService fruitService;

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

    @PostMapping("/api/v1/fruit")
    public void fruitStore(@RequestBody fruitStoreRequest requset) {
        fruitService.saveFruit(requset);
    }

    @GetMapping("/api/v1/fruit/stat")
    public List<FruitStoreResponse> getFruit() {
        return fruitService.getFruit();
    }

    @PutMapping("/api/v1/fruit")
    public void updatefruitStore(@RequestBody fruitUpdateRequest request) {
        fruitService.updateFruit(request);
    }
}
  • Service

@Service
public class FruitService {

    private final FruitRepository fruitRepository;

    public FruitService(FruitRepository fruitRepository) {
        this.fruitRepository = fruitRepository;
    }

    public void saveFruit(fruitStoreRequest request) {
        fruitRepository.saveFruit(request.getName(), request.getWarehousingDate(), request.getPrice(), request.getSales());
    }

    public List<FruitStoreResponse> getFruit() {
        return fruitRepository.getFruit();
    }

    public void updateFruit(fruitUpdateRequest request) {
        if(fruitRepository.isFruitNotExist(request.getId())) {
            throw new IllegalArgumentException();
        }
        fruitRepository.updateFruit(request.getSales(), request.getId());
    }
}
  • Repository

@Repository
public class FruitRepository {
    private final JdbcTemplate jdbcTemplate;

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

    public boolean isFruitNotExist(long id) {
        String readSql = "SELECT * FROM fruit WHERE id = ?";
        return jdbcTemplate.query(readSql, (rs, rowNum) ->0, id).isEmpty();
    }

    public void updateFruit(boolean sales, long id) {
        String sql = "UPDATE fruit SET sales = true WHERE id = ?";
        jdbcTemplate.update(sql, sales, id);
    }

    public void saveFruit(String name, LocalDate warehousingDate, long price, boolean sales) {
        String sql = "INSERT INTO fruit(name, warehousingDate, price, sales) VALUES(?,?,?,?)";
        jdbcTemplate.update(sql, name, warehousingDate, price, sales);
    }

    public List<FruitStoreResponse> getFruit() {
        String sql = "SELECT * FROM fruit";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            String name = rs.getString("name");
            LocalDate warehousingDate = rs.getDate("warehousingDate").toLocalDate();
            long price = rs.getLong("price");
            boolean sales = rs.getBoolean("sales");
            long id = rs.getLong("id");
            return new FruitStoreResponse(name,warehousingDate,price, sales, id);
        });
    }
}

 Repository에서 getFruit부분 문제 #4 다시 작성해야함. sales를 long함수에서 boolean함수로 고침.

 

문제 2

문제 1에서 코드가 분리되면 FruitController/ FruitService / FruitRepository가 생겼을 것 입니다.

기존에 작성했던 FruitRepositoryFruitMemoryRepositoryFruitMySqlRepository로 나누고 @Primary어노테이션을 활용해 두 Repository를 바꿔가며 동작시킬 수 있도록 코드를 변경해보세요

@Qualifier 어노테이션을 사용해도 좋습니다!

  •  FruitMySqlRepository

@Primary
@Repository
public class FruitMySqlRepository implements FruitRepository{
    private JdbcTemplate jdbcTemplate;

    public void FruitRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean isFruitNotExist(long id) {
        String readSql = "SELECT * FROM fruit WHERE id = ?";
        return jdbcTemplate.query(readSql, (rs, rowNum) ->0, id).isEmpty();
    }

    public void updateFruit(boolean sales, long id) {
        String sql = "UPDATE fruit SET sales = true WHERE id = ?";
        jdbcTemplate.update(sql, sales, id);
    }

    public void saveFruit(String name, LocalDate warehousingDate, long price, boolean sales) {
        String sql = "INSERT INTO fruit(name, warehousingDate, price, sales) VALUES(?,?,?,?)";
        jdbcTemplate.update(sql, name, warehousingDate, price, sales);
    }

    public List<FruitStoreResponse> getFruit() {
        String sql = "SELECT * FROM fruit";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            String name = rs.getString("name");
            LocalDate warehousingDate = rs.getDate("warehousingDate").toLocalDate();
            long price = rs.getLong("price");
            boolean sales = rs.getBoolean("sales");
            long id = rs.getLong("id");
            return new FruitStoreResponse(name,warehousingDate,price, sales, id);
        });
    }
}
  • FruitMemoryRepository

@Repository
public class FruitMemoryRepository implements FruitRepository{

}

Memory로 바꾸는 방법은 아직 이해하지 못하였음

 

댓글을 작성해보세요.

채널톡 아이콘