[인프런 워밍업 클럽 0기] 6일차 과제
9개월 전
<과제>
문제 1. 과제 #4 에서 만들었던 API를 강의 내용처럼 Controller - Service - Repository로 분리해보세요!
FruitController
package com.group.libraryapp.controller.assignment;
import com.group.libraryapp.dto.fruit.FruitCreateRequest;
import com.group.libraryapp.dto.fruit.FruitUpdateRequest;
import com.group.libraryapp.service.assignment.FruitService;
import org.springframework.web.bind.annotation.*;
@RestController
public class FruitController {
private final FruitService fruitService;
public FruitController(FruitService fruitService) {
this.fruitService = fruitService;
}
@PostMapping("/api/v1/fruit")
public void saveFruit(@RequestBody FruitCreateRequest request) {
fruitService.saveFruit(request);
}
@PutMapping("/api/v1/fruit")
public void updateFruit(@RequestBody FruitUpdateRequest request) {
fruitService.updateFruit(request);
}
}
FruitService
package com.group.libraryapp.service.assignment;
import com.group.libraryapp.dto.fruit.FruitCreateRequest;
import com.group.libraryapp.dto.fruit.FruitUpdateRequest;
import com.group.libraryapp.repository.assignment.FruitRepository;
import org.springframework.stereotype.Service;
@Service
public class FruitService {
private final FruitRepository fruitRepository;
public FruitService(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
public void saveFruit(FruitCreateRequest request) {
fruitRepository.saveFruit(request.getName(), request.getWarehousingDate(), request.getPrice());
}
public void updateFruit(FruitUpdateRequest request) {
boolean isFruitNotExist = fruitRepository.isFruitNotExist(request.getId());
if (isFruitNotExist) {
throw new IllegalArgumentException();
}
fruitRepository.updateFruit(request.getName(), request.getWarehousingDate(), request.getPrice(), request.getId());
}
}
FruitRepository
package com.group.libraryapp.repository.assignment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
@Repository
public class FruitRepository {
private final JdbcTemplate jdbcTemplate;
public FruitRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveFruit(String name, LocalDate warehousingDate, long price) {
String sql = "INSERT INTO fruit (name, warehousingDate, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, name, warehousingDate, price);
}
public boolean isFruitNotExist(long id) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
return jdbcTemplate.query(readSql, (rs, rowNum) -> 0, id).isEmpty();
}
public void updateFruit(String name, LocalDate warehousingDate, long price, long id) {
String sql = "UPDATE fruit SET name = ?, warehousingDate = ?, price = ? WHERE id = ?";
jdbcTemplate.update(sql, name, warehousingDate, price, id);
}
}
문제 2. 문제 1에서 분리되면 FruitController / FruitService / FruitRepository가 생겼을 것입니다. 기존에 작성했던 FruitRepository를 FruitMemoryRepository와 FruitMySqlRepository로 나누고 @Primary 어노테이션을 활용해 두 Repository를 바꿔가며 동작시킬 수 있도록 코드를 변경해보세요!
FruitRepository
package com.group.libraryapp.repository.assignment;
public interface FruitRepository {
void saveFruit();
boolean isFruitNotExist();
void updateFruit();
}
FruitMemoryRepository
package com.group.libraryapp.repository.assignment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
@Repository
public class FruitMemoryRepository {
private final JdbcTemplate jdbcTemplate;
public FruitMemoryRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveFruit(String name, LocalDate warehousingDate, long price) {
String sql = "INSERT INTO fruit (name, warehousingDate, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, name, warehousingDate, price);
}
public boolean isFruitNotExist(long id) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
return jdbcTemplate.query(readSql, (rs, rowNum) -> 0, id).isEmpty();
}
public void updateFruit(String name, LocalDate warehousingDate, long price, long id) {
String sql = "UPDATE fruit SET name = ?, warehousingDate = ?, price = ? WHERE id = ?";
jdbcTemplate.update(sql, name, warehousingDate, price, id);
}
}
FruitMySqlRepository(이 repository를 사용한다고 가정)
package com.group.libraryapp.repository.assignment;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
@Primary
@Repository
public class FruitMySqlRepository {
private final JdbcTemplate jdbcTemplate;
public FruitMySqlRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void saveFruit(String name, LocalDate warehousingDate, long price) {
String sql = "INSERT INTO fruit (name, warehousingDate, price) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, name, warehousingDate, price);
}
public boolean isFruitNotExist(long id) {
String readSql = "SELECT * FROM fruit WHERE id = ?";
return jdbcTemplate.query(readSql, (rs, rowNum) -> 0, id).isEmpty();
}
public void updateFruit(String name, LocalDate warehousingDate, long price, long id) {
String sql = "UPDATE fruit SET name = ?, warehousingDate = ?, price = ? WHERE id = ?";
jdbcTemplate.update(sql, name, warehousingDate, price, id);
}
}
댓글을 작성해보세요.