문제1
Fruit
@Entity
public class Fruit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = null;
@Column(nullable = false, length = 20)
private String name;
@Column(nullable = false)
private Long price;
@Column(nullable = false, name = "stocked_date")
private LocalDate warehousingDate;
@Column(nullable = false)
private int sold = 0;
public Fruit() {
}
public Fruit(String name, Long price, LocalDate warehousingDate) {
this.name = name;
this.price = price;
this.warehousingDate = warehousingDate;
}
public Long getId() {
return id;
}
public int getSold() {
return sold;
}
public String getName() {
return name;
}
public Long getPrice() {
return price;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public void updateSold() {
this.sold = 1;
}
}
FruitRepository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
List<Fruit> findByNameAndSold(String name, int sold);
long countByName(String name);
List<Fruit> findAllByPriceGreaterThanEqualAndSold(long price,int sold);
List<Fruit> findAllByPriceLessThanEqualAndSold(long price,int sold);
}
Controller
@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 soldFruit(@RequestBody FruitUpdateRequest request) {
fruitService.soldFruit(request);
}
@GetMapping("/api/v1/fruit/stat")
public FruitSoldResponse getFruitIsSoldOrNot(@RequestParam String name) {
return fruitService.getFruitIsSoldOrNot(name);
}
Service
@Service
public class FruitService {
private final FruitRepository fruitRepository;
public FruitService(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
@Transactional
public void saveFruit(FruitCreateRequest request) {
fruitRepository.save(new Fruit(request.getName(), request.getPrice(), request.getWarehousingDate()));
}
@Transactional
public void soldFruit(FruitUpdateRequest request) {
Fruit fruit = fruitRepository.findById(request.getId())
.orElseThrow(IllegalArgumentException::new);
fruit.updateSold();
fruitRepository.save(fruit);
}
@Transactional(readOnly = true)
public FruitSoldResponse getFruitIsSoldOrNot(String name) {
long sold = fruitRepository.findByNameAndSold(name, 1).stream().mapToLong(Fruit::getPrice).sum();
long notSold = fruitRepository.findByNameAndSold(name, 0).stream().mapToLong(Fruit::getPrice).sum();
return new FruitSoldResponse(sold, notSold);
}
Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
List<Fruit> findByNameAndSold(String name, int sold);
}
문제2
Controller
@GetMapping("/api/v1/fruit/count")
public FruitCountResponse getFruitCount(@RequestParam String name){
return fruitService.getFruitCount(name);
}
Response
public class FruitCountResponse {
private String name;
private long count;
public FruitCountResponse() {
}
public FruitCountResponse(String name, long count) {
this.name = name;
this.count = count;
}
public String getName() {
return name;
}
public long getCount() {
return count;
}
}
Service
@Transactional(readOnly = true)
public FruitCountResponse getFruitCount(String name){
long count = fruitRepository.countByName(name);
return new FruitCountResponse(name, count);
}
Repository
long countByName(String name);
문제3
Controller
@GetMapping("/api/v1/fruit/list")
public List<FruitListResponse> listFruit(@RequestParam String option, long price){
return fruitService.getFruitList(option,price);
}
Response
public class FruitListResponse {
private String name;
private long price;
private LocalDate warehousingDate;
public FruitListResponse() {
}
public FruitListResponse(String name, long price, LocalDate warehousingDate) {
this.name = name;
this.price = price;
this.warehousingDate = warehousingDate;
}
public FruitListResponse(Fruit fruit){
this.name = fruit.getName();
this.price = fruit.getPrice();
this.warehousingDate = fruit.getWarehousingDate();
}
public String getName() {
return name;
}
public long getPrice() {
return price;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
}
Service
@Transactional(readOnly = true)
public List<FruitListResponse> getFruitList(String option,long price){
List<Fruit> fruits;
if (option.equals("GTE")) {
fruits = fruitRepository.findAllByPriceGreaterThanEqualAndSold(price, 0);
} else if (option.equals("LTE")) {
fruits = fruitRepository.findAllByPriceLessThanEqualAndSold(price, 0);
} else {
throw new IllegalArgumentException();
}
return fruits.stream()
.map(FruitListResponse::new)
.collect(Collectors.toList());
}
@Transactional(readOnly = true)
public List<FruitListResponse> getFruitList(String option,long price){
List<Fruit> fruits;
if (option.equals("GTE")) {
fruits = fruitRepository.findAllByPriceGreaterThanEqualAndSold(price, 0);
} else if (option.equals("LTE")) {
fruits = fruitRepository.findAllByPriceLessThanEqualAndSold(price, 0);
} else {
throw new IllegalArgumentException();
}
return fruits.stream()
.map(FruitListResponse::new)
.collect(Collectors.toList());
}
Repository
List<Fruit> findAllByPriceGreaterThanEqualAndSold(long price,int sold);
List<Fruit> findAllByPriceLessThanEqualAndSold(long price,int sold);
'인프런 워밍업 스터디 > 과제' 카테고리의 다른 글
[인프런 워밍업 스터디 클럽 0기 - BE] #6 과제 (0) | 2024.02.26 |
---|---|
[인프런 워밍업 스터디 클럽 0기 - BE] #5 과제 (0) | 2024.02.23 |
[인프런 워밍업 스터디 클럽 0기 - BE] #4 과제 (0) | 2024.02.22 |
[인프런 워밍업 스터디 클럽 0기 - BE] #3 과제 (0) | 2024.02.21 |
[인프런 워밍업 스터디 클럽 0기 - BE] #2 과제 (0) | 2024.02.20 |