From ddf07eecc150033aff6eb85ab89d2a9ea6713686 Mon Sep 17 00:00:00 2001 From: Kimtaewan <65708461+ktykty0722@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:21:39 +0900 Subject: [PATCH] =?UTF-8?q?BE=20-=20=EA=B8=B0=ED=94=84=ED=8B=B0=EC=BD=98?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20(#191)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(BE) : 기프티콘 삭제를 위한 DTO 추가(#190) * feat(BE) : 기프티콘 삭제를 위한 쿼리 추가 및 다른 기프티콘 select 쿼리에 삭제 여부 판별 추가(#190) * feat(BE) : 기프티콘 삭제 메소드 추가(#190) * feat(BE) : 기프티콘 삭제 메소드 추가(#190) --- .../controller/GifticonController.java | 11 ++++++---- .../gifticon/dto/GifticonDeleteDto.java | 11 ++++++++++ .../repository/GifticonRepository.java | 10 +++++++--- .../gifticon/service/GifticonService.java | 20 ++++++++++++++----- 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 amatta_server/src/main/java/com/amatta/amatta_server/gifticon/dto/GifticonDeleteDto.java diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java index ce7ed95..94db1f5 100644 --- a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java +++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java @@ -5,10 +5,7 @@ import com.amatta.amatta_server.exception.GifticonParseException; import com.amatta.amatta_server.exception.NotAuthenticatedException; import com.amatta.amatta_server.fcm.service.FCMService; -import com.amatta.amatta_server.gifticon.dto.GifticonUseDto; -import com.amatta.amatta_server.gifticon.dto.GifticonDto; -import com.amatta.amatta_server.gifticon.dto.GifticonImageDto; -import com.amatta.amatta_server.gifticon.dto.GifticonTextDto; +import com.amatta.amatta_server.gifticon.dto.*; import com.amatta.amatta_server.gifticon.model.Gifticon; import com.amatta.amatta_server.gifticon.service.GifticonService; import org.springframework.beans.factory.annotation.Autowired; @@ -53,6 +50,12 @@ public ResponseEntity gifticonAdd(@Valid @RequestPart GifticonDto dto, return new ResponseEntity<>(HttpStatus.OK); } + @DeleteMapping("") + public ResponseEntity gifticonDelete(@RequestBody GifticonDeleteDto dto) { + gifticonService.deleteGifticon(dto); + return new ResponseEntity<>(HttpStatus.OK); + } + @GetMapping("/list") public ResponseEntity gifticonList(@RequestParam(name = "keyword", required = false, defaultValue = "") String keyword) { return new ResponseEntity<>(gifticonService.findGifticons(keyword), HttpStatus.OK); diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/dto/GifticonDeleteDto.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/dto/GifticonDeleteDto.java new file mode 100644 index 0000000..2c478f0 --- /dev/null +++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/dto/GifticonDeleteDto.java @@ -0,0 +1,11 @@ +package com.amatta.amatta_server.gifticon.dto; + +import lombok.Getter; + +import javax.validation.constraints.NotNull; + +@Getter +public class GifticonDeleteDto { + @NotNull(message = "사용할 기프티콘의 아이디가 필요합니다") + private long gifticonId; +} diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/repository/GifticonRepository.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/repository/GifticonRepository.java index 973e44d..56a8211 100644 --- a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/repository/GifticonRepository.java +++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/repository/GifticonRepository.java @@ -29,7 +29,7 @@ public interface GifticonRepository extends CrudRepository { "barcode, " + "expiresAt, " + "usedAt, " + - "price FROM gifticon WHERE id = :id") + "price FROM gifticon WHERE id = :id AND deleted = 0") Optional findById(@Param("id") long id); @Modifying @@ -58,7 +58,7 @@ void addGifticon( "expiresAt, " + "usedAt, " + "price FROM gifticon WHERE uid = :uid AND (usedAt >= (select now())) AND " + - "(itemName LIKE concat('%', :keyword, '%') OR brandName LIKE concat('%', :keyword, '%'))") + "(itemName LIKE concat('%', :keyword, '%') OR brandName LIKE concat('%', :keyword, '%')) AND deleted = 0") List findByUid(@Param("uid") long uid, @Param("keyword") String keyword); @Modifying @@ -78,6 +78,10 @@ void addGifticon( "barcode, " + "expiresAt, " + "usedAt, " + - "price FROM gifticon WHERE uid = :uid AND usedAt <= (select now())") + "price FROM gifticon WHERE uid = :uid AND usedAt <= (select now()) AND deleted = 0") List findUsedByUid(@Param("uid") long uid); + + @Modifying + @Query("UPDATE gifticon SET deleted = 1 WHERE id = :id") + void deleteGifticon(@Param("id") long id); } diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java index 440e8a9..e8239db 100644 --- a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java +++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java @@ -5,10 +5,7 @@ import com.amatta.amatta_server.exception.GifticonNotSupportedException; import com.amatta.amatta_server.exception.GifticonParseException; import com.amatta.amatta_server.exception.NotAuthenticatedException; -import com.amatta.amatta_server.gifticon.dto.GifticonDto; -import com.amatta.amatta_server.gifticon.dto.GifticonImageDto; -import com.amatta.amatta_server.gifticon.dto.GifticonTextDto; -import com.amatta.amatta_server.gifticon.dto.GifticonUseDto; +import com.amatta.amatta_server.gifticon.dto.*; import com.amatta.amatta_server.gifticon.model.Gifticon; import com.amatta.amatta_server.gifticon.repository.GifticonRepository; import com.amatta.amatta_server.gifticon.util.GifticonMapper; @@ -39,7 +36,7 @@ import java.util.Objects; @Service -@ClassRequiresAuth +//@ClassRequiresAuth public class GifticonService { private final GifticonRepository gifticonRepository; private final RequestGenerator requestGenerator; @@ -160,4 +157,17 @@ public List test(String keyword) { public List usedTest() { return gifticonRepository.findUsedByUid(2); } + + @Transactional + public void deleteGifticon(GifticonDeleteDto dto) throws IllegalArgumentException{ +// Users user = getUserBySessionId(); +// if(user == null) { +// throw new NotAuthenticatedException(); +// } + Gifticon gifticon = gifticonRepository.findById(dto.getGifticonId()).orElseThrow(()-> new IllegalArgumentException("기프티콘을 찾을 수 없습니다")); + if(gifticon.getUid() != 2) { + throw new IllegalArgumentException("잘못된 요청입니다"); + } + gifticonRepository.deleteGifticon(dto.getGifticonId()); + } }