[인프런 워밍업 스터디1기] 7일차 진도표

[인프런 워밍업 스터디1기] 7일차 진도표

image

 <문제 1>

public class FruitService {
    private final FruitRepository fruitRepository;

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

    @Transactional
    public void storeFruit(String name, LocalDate warehousingDate, long price) {
        Fruit fruit = fruitRepository.save(new Fruit(name, warehousingDate, price));
    }

    @Transactional
    public void sellFruit(long fruitId) {
        Fruit fruit = fruitRepository.findById(fruitId)
                .orElseThrow(IllegalArgumentException::new);
        fruit.updateSold();
    }

    @Transactional(readOnly = true)
    public FruitStatResponse getFruitStat(String name) {
        FruitStatResponse response = new FruitStatResponse();
        List<Fruit> soldFruits= fruitRepository.findAllByNameAndSold(name, (byte)1);
        List<Fruit> notSoldFruits= fruitRepository.findAllByNameAndSold(name, (byte)0);

        response.setSalesAmount(soldFruits.stream().map(fruit -> fruit.getPrice()).reduce(new Long(0), (a, b) -> (a+b)));
        response.setNotSalesAmount(notSoldFruits.stream().map(fruit -> fruit.getPrice()).reduce(new Long(0), (a, b) -> (a+b)));

        return response;
    }
    ...
  }
package com.group.libraryapp.dto.fruit.response;

public class FruitCountResponse {

    private final long count;

    public FruitCountResponse(long count) {
        this.count = count;
    }

    public long getCount() {
        return count;
    }
}

 

controller

@GetMapping("/api/v1/fruit/count")
public FruitCountResponse countFruit (@RequestParam String name){
    return fruitService.countFruit(name);
}

 

service

@Transactional
public FruitCountResponse countFruit (String name){
    long countFruit = fruitJpaRepository.countByName(name);
    return new FruitCountResponse(countFruit);
}

댓글을 작성해보세요.

채널톡 아이콘