Skip to content

Commit

Permalink
feat: 기프티콘 전체 조회 api 구현
Browse files Browse the repository at this point in the history
정렬 타입
- 최신 등록순
- 유효기간순
- 높은 가격순
- 낮은 가격순
  • Loading branch information
Jimin0304 committed Nov 16, 2023
1 parent 72cf9d1 commit 8179cd4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorCode {
INVALID_EXPIRE_DATE_FORMAT(401, "G_001", "올바르지 않은 날짜 형식입니다."),
IMG_INFO_NOT_FOUND(401, "G_002", "올바르지 않은 기프티콘 이미지입니다."),
GIFTICON_NOT_FOUND(404, "G_003", "기프티콘 정보를 찾을 수 없습니다."),
INVALID_SORT_TYPE(401, "G_004", "올바르지 않은 기프티콘 정렬 타입입니다."),
;

private final int status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public ResponseEntity<BasicResponse> getGifticon(@ReqMember SecurityUserDetails
);
}

@GetMapping("/all/{sortType}")
@Operation(summary = "기프티콘 전체 조회", description = "기프티콘을 전체 조회합니다.")
public ResponseEntity<BasicResponse> getAllGifticons(@ReqMember SecurityUserDetails securityUserDetails,
@PathVariable ("sortType") String sortType) {
return basicResponse.ok(
gifticonService.getAllGifticons(securityUserDetails.member(), sortType)
);
}

@DeleteMapping("/{gifticonId}")
@Operation(summary = "기프티콘 삭제", description = "기프티콘을 삭제합니다.")
public ResponseEntity<BasicResponse> delGifticon(@ReqMember SecurityUserDetails securityUserDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface GifticonRepository extends JpaRepository<Gifticon, Long> {
Expand All @@ -17,4 +19,16 @@ public interface GifticonRepository extends JpaRepository<Gifticon, Long> {
@Modifying
@Query(value = "delete from Gifticon g where g.member = :member and g.id = :gifticonId")
void deleteByIdAndMember(@Param("member") Member member, @Param("gifticonId") long gifticonId);

@Query(value = "select g from Gifticon g where g.member = :member order by g.createdAt DESC")
List<Gifticon> findAllByMemberAndLatest(@Param("member") Member member);

@Query(value = "select g from Gifticon g where g.member = :member order by ABS(DATEDIFF(:now, g.expireDate)) ASC")
List<Gifticon> findAllByMemberAndExpireDate(@Param("member") Member member, @Param("now") LocalDate now);

@Query(value = "select g from Gifticon g where g.member = :member order by g.price DESC")
List<Gifticon> findAllByMemberAndHighPrice(@Param("member") Member member);

@Query(value = "select g from Gifticon g where g.member = :member order by g.price ASC")
List<Gifticon> findAllByMemberAndLowPrice(@Param("member") Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.CSP2.switchcon.common.exception.BusinessException;
import com.CSP2.switchcon.common.exception.EntityNotFoundException;
import com.CSP2.switchcon.common.exception.ErrorCode;
import com.CSP2.switchcon.common.exception.InvalidValueException;
import com.CSP2.switchcon.gifticon.domain.Gifticon;
import com.CSP2.switchcon.gifticon.dto.GifticonRequestDTO;
import com.CSP2.switchcon.gifticon.dto.GifticonResponseDTO;
Expand All @@ -22,6 +23,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -106,6 +108,33 @@ public GifticonResponseDTO getGifticon(Member member, long gifticonId) {
return GifticonResponseDTO.from(gifticon);
}

@Transactional
public List<GifticonResponseDTO> getAllGifticons(Member member, String sortType) {
List<Gifticon> gifticons;
LocalDate now = LocalDate.now();

switch(sortType) {
case "latest":
gifticons = gifticonRepository.findAllByMemberAndLatest(member);
break ;
case "expiringSoon":
gifticons = gifticonRepository.findAllByMemberAndExpireDate(member, now);
break ;
case "highPrice":
gifticons = gifticonRepository.findAllByMemberAndHighPrice(member);
break ;
case "lowPrice":
gifticons = gifticonRepository.findAllByMemberAndLowPrice(member);
break ;
default:
throw new InvalidValueException(ErrorCode.INVALID_SORT_TYPE);
}

return gifticons.stream()
.map(GifticonResponseDTO::from)
.collect(Collectors.toList());
}

@Transactional
public void delGifticon(Member member, long gifticonId) {
gifticonRepository.deleteByIdAndMember(member, gifticonId);
Expand Down

0 comments on commit 8179cd4

Please sign in to comment.