[인프런 워밍업 클럽 1기] BE 7일차
7개월 전
[인프런 워밍업 클럽 1기] BE 7일차
본 게시글은 다음 강의 내용을 진행하고 있습니다.
자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지] - https://inf.run/XKQg
코드
SQL 문
create table fruit (
id bigint auto_increment PRIMARY KEY ,
name varchar(25),
warehousing_date date,
price bigint,
status varchar(10) CHECK (status IN ('SOLD', 'NOT_SOLD'))
);
Fruit.java
@Entity
public class Fruit {
@Id
private Long id = null;
@Column(nullable = false, length = 25)
private String name;
private LocalDate warehousingDate;
private long price;
@Column(columnDefinition = "varchar(10) CHECK (status IN ('SOLD', 'NOT_SOLD'))")
private String status;
protected Fruit() {
}
public Fruit(String name, LocalDate warehousingDate, long price) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("잘못된 name(%s)이 들어왔습니다, name");
}
this.name = name;
this.price = price;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public long getPrice() {
return price;
}
public String getStatus() {
return status;
}
}
FruitServiceV2.java
@Service
public class FruitServiceV2 {
private final FruitRepository fruitRepository;
private EntityManager entityManager;
public FruitServiceV2(FruitRepository fruitRepository) {
this.fruitRepository = fruitRepository;
}
public SaveFruitRequest saveFruit(SaveFruitRequest request) {
fruitRepository.save(new Fruit(
request.getName(),
request.getWarehousingDate(),
request.getPrice(),
request.getStatus()));
return request;
}
public GetFruitStatResponse getFruitStat(String name) {
List<Fruit> fruit = fruitRepository.findByName(name);
if (fruit == null) {
throw new IllegalArgumentException("해당 과일이 없습니다.");
}
Long salesAmount = fruitRepository.sumPriceByNameAndStatus(name, "SOLD");
Long notSalesAmount = fruitRepository.sumPriceByNameAndStatus(name, "NOT_SOLD");
return new GetFruitStatResponse(
salesAmount != null ? salesAmount : 0L, notSalesAmount != null ? notSalesAmount : 0L);
}
public ResponseEntity<Void> recordSoldFruit(RecordSoldFruitRequest request) {
Fruit fruit = fruitRepository.findById(request.getId())
.orElseThrow(IllegalArgumentException::new);
fruit.setStatus("SOLD");
fruitRepository.save(fruit);
return ResponseEntity.ok().build();
}
}
FruitController.java
@RestController
public class FruitController {
private final FruitServiceV2 fruitService;
public FruitController(FruitServiceV2 fruitService) {
this.fruitService = fruitService;
}
@PostMapping("/api/v1/fruit")
public SaveFruitRequest saveFruit(@RequestBody SaveFruitRequest request) {
return fruitService.saveFruit(request);
}
@PutMapping("/api/v1/fruit")
public ResponseEntity<Void> recordSoldFruit(@RequestBody RecordSoldFruitRequest request){
return fruitService.recordSoldFruit(request);
}
@GetMapping("/api/v1/fruit/stat")
public GetFruitStatResponse getFruitStat(@RequestParam String name) {
return fruitService.getFruitStat(name);
}
}
코드
++FruitServiceV2.java
public CountFruitResponse countFruit(String name) {
long count = fruitRepository.countByName(name);
return new CountFruitResponse(count);
}
CountFruitResponse.java
public class CountFruitResponse {
private long count;
public CountFruitResponse(long count) {
this.count = count;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
}
++FruitController
@GetMapping("/api/v1/fruit/count")
public CountFruitResponse countFruit(@RequestParam String name) {
return fruitService.countFruit(name);
}
++FruitResponsitory
long countByName(String name);
결과
POST_MAN count
코드
FruitInfo.java
public class FruitInfo {
private String name;
private long price;
private LocalDate warehousingDate;
public FruitInfo(String name, long price, LocalDate warehousingDate) {
this.name = name;
this.price = price;
this.warehousingDate = warehousingDate;
}
public String getName() {
return name;
}
public long getPrice() {
return price;
}
public LocalDate getWarehousingDate() {
return warehousingDate;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(long price) {
this.price = price;
}
public void setWarehousingDate(LocalDate warehousingDate) {
this.warehousingDate = warehousingDate;
}
}
++FruitController.java
@GetMapping("/api/v1/fruit/list")
public List<FruitInfo> getFruitList(@RequestParam String option, @RequestParam long price) {
return fruitService.getFruitList(option, price);
}
++FruitRepository.java
List<Fruit> findByPriceGreaterThanEqual(long price);
List<Fruit> findByPriceLessThanEqual(long price);
++FruitServiceV2.java
public List<FruitInfo> getFruitList(String option, long price) {
List<Fruit> fruits = getFruitsByOption(option, price);
return convertToFruitInfoList(fruits);
}
private List<Fruit> getFruitsByOption(String option, long price) {
if ("GTE".equals(option)) {
return fruitRepository.findByPriceGreaterThanEqual(price);
} else if ("LTE".equals(option)) {
return fruitRepository.findByPriceLessThanEqual(price);
} else {
throw new IllegalArgumentException("GTE와 LTE 중 하나를 입력하세요");
}
}
private FruitInfo convertToFruitInfo(Fruit fruit) {
return new FruitInfo(fruit.getName(), fruit.getPrice(), fruit.getWarehousingDate());
}
private List<FruitInfo> convertToFruitInfoList(List<Fruit> fruits) {
return fruits.stream()
.map(this::convertToFruitInfo)
.collect(Collectors.toList());
}
결과
POST_MAN LTE 경우
POST MAN GTE 경우
댓글을 작성해보세요.