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

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

 

Controller, Service, Repository 분리하기

 

 

문제 1

 

FruitController

@RestController
public class FruitController {

    private final FruitService fruitService;

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

    @PostMapping("/api/v1/fruit")
    @ResponseStatus(HttpStatus.OK)
    public void saveFruit(@RequestBody Fruit fruit) {
        fruitService.saveFruit(fruit);
    }

    @PutMapping("/api/v1/fruit")
    @ResponseStatus(HttpStatus.OK)
    public void sellFruit(@RequestBody FruitSellRequest fruitSellRequest) {
        fruitService.sellFruit(fruitSellRequest.getId());
    }

    @GetMapping("/api/v1/fruit/stat")
    public Map<String, Long> fruitStat(@RequestParam String name) {
        return fruitService.getFruitStat(name);
    }
}

 

FruitService

@Service
public class FruitService {

    private final FruitRepository fruitRepository;

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

    public void saveFruit(Fruit fruit) {
        fruitRepository.saveFruit(fruit);
    }

    public void sellFruit(long fruitId) {
        fruitRepository.sellFruit(fruitId);
    }

    public Map<String, Long> getFruitStat(String name) {
        long soldAmount = fruitRepository.calculateSoldAmount(name);
        long unsoldAmount = fruitRepository.calculateUnsoldAmount(name);

        Map<String, Long> result = new HashMap<>();
        result.put("soldAmount", soldAmount);
        result.put("unsoldAmount", unsoldAmount);

        return result;
    }
}

 

FruitRepository

@Repository
public class FruitRepository {

    private final JdbcTemplate jdbcTemplate;

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

    public void saveFruit(Fruit fruit) {
        String sql = "insert into fruit (name, warehousingDate, price) values (?, ?, ?)";
        jdbcTemplate.update(sql, fruit.getName(), fruit.getWareHousingDate(), fruit.getPrice());
    }

    public void sellFruit(int fruitId) {
        String sql = "UPDATE fruit set is_sold = true where id = ?";
        jdbcTemplate.update(sql, fruitId);
    }

    public long calculateSoldAmount(String name) {
        String sql = "select sum(price) from fruit where name = ? and is_sold = true";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, Long.class);
    }

    public long calculateUnsoldAmount(String name) {
        String sql = "select sum(price) from fruit where name = ? and is_sold = false";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, Long.class);
    }
}

 

문제 2

 

FruitRepository

public interface FruitRepository {
    void saveFruit(Fruit fruit);
    void sellFruit(int fruitId);
    long calculateSoldAmount(String name);
    long calculateUnsoldAmount(String name);
}

 

FruitMysqlRepository

@Primary
@Repository
public class FruitMysqlRepository implements FruitRepository{

    private final JdbcTemplate jdbcTemplate;

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

    public void saveFruit(Fruit fruit) {
        String sql = "insert into fruit (name, warehousingDate, price) values (?, ?, ?)";
        jdbcTemplate.update(sql, fruit.getName(), fruit.getWareHousingDate(), fruit.getPrice());
    }

    public void sellFruit(long fruitId) {
        String sql = "UPDATE fruit set is_sold = true where id = ?";
        jdbcTemplate.update(sql, fruitId);
    }

    public long calculateSoldAmount(String name) {
        String sql = "select sum(price) from fruit where name = ? and is_sold = true";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, Long.class);
    }

    public long calculateUnsoldAmount(String name) {
        String sql = "select sum(price) from fruit where name = ? and is_sold = false";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, Long.class);
    }
}

 

FruitMemoryRepository

@Repository
public class FruitMemoryRepository implements FruitRepository{

    private final Map<Long, Fruit> fruitMap = new HashMap<>();
    private int idCounter = 1;

    public void saveFruit(Fruit fruit) {
        fruit.setId(idCounter++);
        fruitMap.put(fruit.getId(), fruit);
    }

    public void sellFruit(long fruitId) {
        Fruit fruit = fruitMap.get(fruitId);
        if (fruit != null) {
            fruit.setSold(true);
        }
    }

    public long calculateSoldAmount(String name) {
        long soldAmount = 0;
        for (Fruit fruit : fruitMap.values()) {
            if (fruit.getName().equals(name) && fruit.isSold()) {
                soldAmount += fruit.getPrice();
            }
        }
        return soldAmount;
    }

    public long calculateUnsoldAmount(String name) {
        long unsoldAmount = 0;
        for (Fruit fruit : fruitMap.values()) {
            if (fruit.getName().equals(name) && !fruit.isSold()) {
                unsoldAmount += fruit.getPrice();
            }
        }
        return unsoldAmount;
    }
}

 

댓글을 작성해보세요.

채널톡 아이콘