From 9d7daa0870cbf6c2719a5c151ca320adff657642 Mon Sep 17 00:00:00 2001 From: Kwon Da Woon Date: Fri, 5 Apr 2024 14:50:01 +0900 Subject: [PATCH] =?UTF-8?q?KNU-HAEDAL#1=20feat:=20response=20dto=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EA=B7=B8=EC=97=90=20=EB=A7=9E?= =?UTF-8?q?=EA=B2=8C=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GifticonController.java | 33 +++++++--- .../response/GifticonDetailResponse.java | 52 ++++++++++++++++ .../gifticon/response/GifticonResponse.java | 61 +++++++++++++++++++ .../service/gifticon/GifticonService.java | 14 +++-- 4 files changed, 146 insertions(+), 14 deletions(-) create mode 100644 src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonDetailResponse.java create mode 100644 src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonResponse.java diff --git a/src/main/java/team/haedal/gifticionfunding/controller/GifticonController.java b/src/main/java/team/haedal/gifticionfunding/controller/GifticonController.java index 0f8e266..55fe284 100644 --- a/src/main/java/team/haedal/gifticionfunding/controller/GifticonController.java +++ b/src/main/java/team/haedal/gifticionfunding/controller/GifticonController.java @@ -4,13 +4,17 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import team.haedal.gifticionfunding.dto.gifticon.request.GifticonCreateRequest; import team.haedal.gifticionfunding.dto.gifticon.request.GifticonUpdateRequest; +import team.haedal.gifticionfunding.dto.gifticon.response.GifticonDetailResponse; +import team.haedal.gifticionfunding.dto.gifticon.response.GifticonResponse; import team.haedal.gifticionfunding.entity.gifticon.Gifticon; import team.haedal.gifticionfunding.entity.gifticon.GifticonCreate; import team.haedal.gifticionfunding.service.gifticon.GifticonService; +import java.net.URI; import java.util.List; @Slf4j @@ -22,33 +26,42 @@ public class GifticonController { @GetMapping() @ResponseStatus(HttpStatus.OK) - public List getGifticons() { - return gifticonService.findGifticonAll(); + public ResponseEntity> getGifticons() { + log.info("상품 전체조회"); + List gifticons = gifticonService.findGifticonAll(); + return ResponseEntity.ok(GifticonResponse.fromList(gifticons)); } @GetMapping("/{gifticonId}") @ResponseStatus(HttpStatus.OK) - public Gifticon getGifticon(@PathVariable("gifticonId") Long gifticonId){ - log.info("gifticonId : {}", gifticonId); - return gifticonService.findGifticon(gifticonId); + public ResponseEntity getGifticon(@PathVariable("gifticonId") Long gifticonId) { + log.info("gifticonId : {} 상품조회", gifticonId); + Gifticon gifticon = gifticonService.findGifticon(gifticonId); + return ResponseEntity.ok(GifticonDetailResponse.from(gifticon)); } @PostMapping() @ResponseStatus(HttpStatus.CREATED) - public Long createGifticon(@RequestBody @Valid GifticonCreateRequest request) { - return gifticonService.registerGifticon(request.toCommand()); + public ResponseEntity createGifticon(@RequestBody @Valid GifticonCreateRequest request) { + log.info("상품 생성 name: {},price:{}",request.getName(), request.getPrice()); + final Long id = gifticonService.registerGifticon(request.toCommand()); + return ResponseEntity.created(URI.create("/api/gifticons/"+id)).build(); } @PutMapping("/{gifticonId}") @ResponseStatus(HttpStatus.NO_CONTENT) - public Gifticon putGifticon(@PathVariable("gifticonId") Long gifticonId, @RequestBody GifticonUpdateRequest request) { - return gifticonService.updateGifticon(gifticonId, request.toCommand()); + public ResponseEntity putGifticon(@PathVariable("gifticonId") Long gifticonId, @RequestBody GifticonUpdateRequest request) { + log.info("gifticonId : {} 상품 수정", gifticonId); + Gifticon gifticon=gifticonService.updateGifticon(gifticonId, request.toCommand()); + return ResponseEntity.ok(GifticonDetailResponse.from(gifticon)); } @DeleteMapping("/{gifticonId}") @ResponseStatus(HttpStatus.NO_CONTENT) - public void deleteGifticon(@PathVariable("gifticonId") Long gifticonId) { + public ResponseEntity deleteGifticon(@PathVariable("gifticonId") Long gifticonId) { + log.info("gifticonId : {} 상품 삭제", gifticonId); gifticonService.deleteGifticon(gifticonId); + return ResponseEntity.noContent().build(); } diff --git a/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonDetailResponse.java b/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonDetailResponse.java new file mode 100644 index 0000000..680bbc3 --- /dev/null +++ b/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonDetailResponse.java @@ -0,0 +1,52 @@ +package team.haedal.gifticionfunding.dto.gifticon.response; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import team.haedal.gifticionfunding.entity.gifticon.Category; +import team.haedal.gifticionfunding.entity.gifticon.Gifticon; + +import java.time.LocalDate; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class GifticonDetailResponse { + private Long id; + private Long price; + private String name; + private Category category; + private Long stock; + private String imageUrl; + private String description; + private String brand; + private LocalDate expirationPeriod; + + @Builder + private GifticonDetailResponse(final Long id, final Long price, final String name, final Category category, final Long stock, final String imageUrl, final String description, final String brand, final LocalDate expirationPeriod) { + this.id = id; + this.price = price; + this.name = name; + this.category = category; + this.stock = stock; + this.imageUrl = imageUrl; + this.description = description; + this.brand = brand; + this.expirationPeriod = expirationPeriod; + } + + public static GifticonDetailResponse from(final Gifticon gifticon){ + return GifticonDetailResponse.builder() + .id(gifticon.getId()) + .price(gifticon.getPrice()) + .name(gifticon.getName()) + .category(gifticon.getCategory()) + .stock(gifticon.getStock()) + .imageUrl(gifticon.getImageUrl()) + .description(gifticon.getDescription()) + .brand(gifticon.getBrand()) + .expirationPeriod(gifticon.getExpirationPeriod()) + .build(); + } + +} diff --git a/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonResponse.java b/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonResponse.java new file mode 100644 index 0000000..69028fd --- /dev/null +++ b/src/main/java/team/haedal/gifticionfunding/dto/gifticon/response/GifticonResponse.java @@ -0,0 +1,61 @@ +package team.haedal.gifticionfunding.dto.gifticon.response; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.cglib.core.Local; +import team.haedal.gifticionfunding.entity.gifticon.Category; +import team.haedal.gifticionfunding.entity.gifticon.Gifticon; + +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class GifticonResponse { + private Long id; + private Long price; + private String name; + private Category category; + private Long stock; + private String imageUrl; + private String description; + private String brand; + private LocalDate expirationPeriod; + + + @Builder + private GifticonResponse(final Long id, final Long price, final String name, final Category category, final Long stock, final String imageUrl, final String description, final String brand, final LocalDate expirationPeriod) { + this.id = id; + this.price = price; + this.name = name; + this.category = category; + this.stock = stock; + this.imageUrl = imageUrl; + this.description = description; + this.brand = brand; + this.expirationPeriod = expirationPeriod; + } + + public static GifticonResponse from(final Gifticon gifticon){ + return GifticonResponse.builder() + .id(gifticon.getId()) + .price(gifticon.getPrice()) + .name(gifticon.getName()) + .category(gifticon.getCategory()) + .stock(gifticon.getStock()) + .imageUrl(gifticon.getImageUrl()) + .description(gifticon.getDescription()) + .brand(gifticon.getBrand()) + .expirationPeriod(gifticon.getExpirationPeriod()) + .build(); + } + + public static List fromList(final List gifticons){ + return gifticons.stream() + .map(GifticonResponse::from) + .collect(Collectors.toUnmodifiableList()); + } +} diff --git a/src/main/java/team/haedal/gifticionfunding/service/gifticon/GifticonService.java b/src/main/java/team/haedal/gifticionfunding/service/gifticon/GifticonService.java index 9a8a0bd..58570cb 100644 --- a/src/main/java/team/haedal/gifticionfunding/service/gifticon/GifticonService.java +++ b/src/main/java/team/haedal/gifticionfunding/service/gifticon/GifticonService.java @@ -7,6 +7,7 @@ import team.haedal.gifticionfunding.entity.gifticon.Gifticon; import team.haedal.gifticionfunding.entity.gifticon.GifticonCreate; import team.haedal.gifticionfunding.entity.gifticon.GifticonUpdate; +import team.haedal.gifticionfunding.global.error.NotFoundGifticonException; import team.haedal.gifticionfunding.global.validation.GifticonUpdateValidator; import team.haedal.gifticionfunding.repository.gifticon.GifticonJpaRepository; @@ -37,7 +38,7 @@ public List findGifticonAll(){ public Gifticon findGifticon(Long gifticonId){ Gifticon findGifticon= gifticonJpaRepository.findById(gifticonId).orElse(null); if(findGifticon == null){ - throw new IllegalArgumentException("해당 상품이 존재하지 않습니다."); + throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다."); } return findGifticon; } @@ -47,7 +48,7 @@ public Gifticon findGifticon(Long gifticonId){ public Gifticon updateGifticon(Long gifticonId, GifticonUpdate gifticonUpadate){ Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null); if(findGifticon == null){ - throw new IllegalArgumentException("해당 상품이 존재하지 않습니다."); + throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다."); } //gifticonUpdate 검증 GifticonUpdateValidator.validate(gifticonUpadate); @@ -58,6 +59,11 @@ public Gifticon updateGifticon(Long gifticonId, GifticonUpdate gifticonUpadate){ //상품 삭제 @Transactional public void deleteGifticon(Long gifticonId){ + //상품이 있는지 확인 + Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null); + if(findGifticon == null){ + throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다."); + } gifticonJpaRepository.deleteById(gifticonId); } @@ -68,7 +74,7 @@ public void deleteGifticon(Long gifticonId){ public void addStock(Long gifticonId, Long stock){ Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null); if(findGifticon == null){ - throw new IllegalArgumentException("해당 상품이 존재하지 않습니다."); + throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다."); } findGifticon.addStock(stock); } @@ -80,7 +86,7 @@ public void addStock(Long gifticonId, Long stock){ public void removeStock(Long gifticonId, Long stock){ Gifticon findGifticon = gifticonJpaRepository.findById(gifticonId).orElse(null); if(findGifticon == null){ - throw new IllegalArgumentException("해당 상품이 존재하지 않습니다."); + throw new NotFoundGifticonException("해당 상품이 존재하지 않습니다."); } findGifticon.removeStock(stock); }