Skip to content

Commit

Permalink
BE - 기프티콘 삭제 기능 추가 (#191)
Browse files Browse the repository at this point in the history
* feat(BE) : 기프티콘 삭제를 위한 DTO 추가(#190)

* feat(BE) : 기프티콘 삭제를 위한 쿼리 추가 및 다른 기프티콘 select 쿼리에 삭제 여부 판별 추가(#190)

* feat(BE) : 기프티콘 삭제 메소드 추가(#190)

* feat(BE) : 기프티콘 삭제 메소드 추가(#190)
  • Loading branch information
ktykty0722 authored Feb 22, 2023
1 parent 304b09d commit ddf07ee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface GifticonRepository extends CrudRepository<Gifticon, Long> {
"barcode, " +
"expiresAt, " +
"usedAt, " +
"price FROM gifticon WHERE id = :id")
"price FROM gifticon WHERE id = :id AND deleted = 0")
Optional<Gifticon> findById(@Param("id") long id);

@Modifying
Expand Down Expand Up @@ -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<Gifticon> findByUid(@Param("uid") long uid, @Param("keyword") String keyword);

@Modifying
Expand All @@ -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<Gifticon> findUsedByUid(@Param("uid") long uid);

@Modifying
@Query("UPDATE gifticon SET deleted = 1 WHERE id = :id")
void deleteGifticon(@Param("id") long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,7 +36,7 @@
import java.util.Objects;

@Service
@ClassRequiresAuth
//@ClassRequiresAuth
public class GifticonService {
private final GifticonRepository gifticonRepository;
private final RequestGenerator requestGenerator;
Expand Down Expand Up @@ -160,4 +157,17 @@ public List<Gifticon> test(String keyword) {
public List<Gifticon> 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());
}
}

0 comments on commit ddf07ee

Please sign in to comment.