From 21e4197860920faa648b5fba5856627cf9204ec0 Mon Sep 17 00:00:00 2001 From: iseunghag Date: Sat, 25 Nov 2023 13:43:35 +0900 Subject: [PATCH] =?UTF-8?q?#138=20feat:=EA=B4=80=EB=A6=AC=EC=9E=90=20?= =?UTF-8?q?=EA=B2=BD=ED=97=98=20=EC=84=A0=EB=AC=BC=20=EC=88=98=EC=A0=95,?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20api=20=EA=B8=B0=EB=8A=A5=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/ExperienceGiftService.java | 4 ++ .../ExperienceGiftServiceImpl.java | 60 ++++++++++++++++++- .../experiencegift/domain/ExperienceGift.java | 13 ++++ .../ExperienceGiftQuerydslRepositoryImpl.java | 20 +++++-- .../repository/ExperienceGiftRepository.java | 5 +- .../dto/request/AdminExperienceReq.java | 4 +- .../AdminExperienceGiftController.java | 29 +++++++++ 7 files changed, 123 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftService.java b/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftService.java index 5b7552f3..b1957c68 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftService.java +++ b/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftService.java @@ -40,4 +40,8 @@ public interface ExperienceGiftService { AdminMainRes mainAdminExperienceGift(UserPrincipal userPrincipal); List getExperienceGift(UserPrincipal userPrincipal); + + void modifyExperienceGift(Long experienceGiftId,UserPrincipal userPrincipal, AdminExperienceReq adminExperienceReq); + + void deleteExperienceGift(Long experienceGiftId, UserPrincipal userPrincipal); } diff --git a/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftServiceImpl.java b/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftServiceImpl.java index 99e5d871..3d965a34 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftServiceImpl.java +++ b/src/main/java/com/shallwe/domain/experiencegift/application/ExperienceGiftServiceImpl.java @@ -1,5 +1,6 @@ package com.shallwe.domain.experiencegift.application; +import com.shallwe.domain.common.Status; import com.shallwe.domain.experiencegift.domain.*; import com.shallwe.domain.experiencegift.domain.repository.*; import com.shallwe.domain.experiencegift.dto.request.AdminExperienceReq; @@ -121,13 +122,68 @@ public List getExperienceGift(UserPrincipal userPrincipal) { ShopOwner shopOwner = shopOwnerRepository.findById(userId) .orElseThrow(InvalidShopOwnerException::new); - List experienceGifts = experienceGiftRepository.findByShopOwnerId(shopOwner.getId()); + List experienceGifts = experienceGiftRepository.findByShopOwnerIdAndStatus(shopOwner.getId(),Status.ACTIVE); return experienceGifts.stream() .map(AdminExperienceRes::toDto) .collect(Collectors.toList()); } + @Override + @Transactional + public void modifyExperienceGift(Long experienceGiftId,UserPrincipal userPrincipal, AdminExperienceReq adminExperienceReq) { + ShopOwner shopOwner = shopOwnerRepository.findById(userPrincipal.getId()) + .orElseThrow(InvalidUserException::new); + + ExperienceGift experienceGift = experienceGiftRepository.findById(experienceGiftId) + .orElseThrow(ExperienceGiftNotFoundException::new); + + Subtitle subtitle = subtitleRepository.findByTitle(adminExperienceReq.getSubtitle()) + .orElseGet(() -> subtitleRepository.save(new Subtitle(adminExperienceReq.getSubtitle()))); + + + ExpCategory expCategory = null; + SttCategory sttCategory = null; + + // 경험 카테고리가 주어졌는지 확인하고 처리 + if (StringUtils.hasText(adminExperienceReq.getExpCategory())) { + expCategory = expCategoryRepository.findByExpCategory(adminExperienceReq.getExpCategory()) + .orElseThrow(ExpCategoryAlreadyExist::new); + } + + // 상황 카테고리가 주어졌는지 확인하고 처리 + if (StringUtils.hasText(adminExperienceReq.getSttCategory())) { + sttCategory = sttCategoryRepository.findBySttCategory(adminExperienceReq.getSttCategory()) + .orElseThrow(SttCategoryAlreadyExist::new); + } + + experienceGift.update(adminExperienceReq, subtitle, expCategory, sttCategory, shopOwner); + + List newExplanations = adminExperienceReq.getExplanation().stream() + .map(explanationReq -> Explanation.toDto(explanationReq, experienceGift)) + .collect(Collectors.toList()); + + explanationRepository.saveAll(newExplanations); + + } + + @Override + @Transactional + public void deleteExperienceGift(Long experienceGiftId, UserPrincipal userPrincipal) { + ShopOwner shopOwner = shopOwnerRepository.findById(userPrincipal.getId()) + .orElseThrow(InvalidUserException::new); + + ExperienceGift experienceGift = experienceGiftRepository.findById(experienceGiftId) + .orElseThrow(ExperienceGiftNotFoundException::new); + + if (!experienceGift.getShopOwner().equals(shopOwner)) { + throw new ExperienceGiftNotFoundException(); + } + + experienceGift.updateStatus(Status.DELETE); + experienceGiftRepository.save(experienceGift); + } + @Override @Transactional public ExperienceDetailRes createExperience(UserPrincipal userPrincipal,ExperienceReq experienceReq ) { @@ -144,7 +200,7 @@ public ExperienceDetailRes createExperience(UserPrincipal userPrincipal,Experien @Override public List searchExperience(UserPrincipal userPrincipal, String title) { userRepository.findById(userPrincipal.getId()).orElseThrow(InvalidUserException::new); - return experienceGiftRepository.findByTitleContains(title) + return experienceGiftRepository.findByTitleContainsAndStatus(title,Status.ACTIVE) .stream().map(ExperienceRes::toDto).collect(Collectors.toList()); } diff --git a/src/main/java/com/shallwe/domain/experiencegift/domain/ExperienceGift.java b/src/main/java/com/shallwe/domain/experiencegift/domain/ExperienceGift.java index d067a83f..61318756 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/domain/ExperienceGift.java +++ b/src/main/java/com/shallwe/domain/experiencegift/domain/ExperienceGift.java @@ -58,4 +58,17 @@ public static ExperienceGift toDto(AdminExperienceReq req, Subtitle subtitle, Ex .location(req.getLocation()) .build(); } + + public void update(AdminExperienceReq adminExperienceReq, Subtitle subtitle, ExpCategory expCategory, SttCategory sttCategory, ShopOwner shopOwner) { + this.title = adminExperienceReq.getTitle(); + this.giftImgKey=adminExperienceReq.getGiftImgUrl(); + this.description=adminExperienceReq.getDescription(); + this.location=adminExperienceReq.getLocation(); + this.price=adminExperienceReq.getPrice(); + this.subtitle = subtitle; + this.expCategory = expCategory; + this.sttCategory = sttCategory; + this.shopOwner = shopOwner; + } + } diff --git a/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftQuerydslRepositoryImpl.java b/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftQuerydslRepositoryImpl.java index 96ec6de5..a5d35412 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftQuerydslRepositoryImpl.java +++ b/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftQuerydslRepositoryImpl.java @@ -1,6 +1,7 @@ package com.shallwe.domain.experiencegift.domain.repository; import com.querydsl.jpa.impl.JPAQueryFactory; +import com.shallwe.domain.common.Status; import com.shallwe.domain.experiencegift.domain.ExperienceGift; import com.shallwe.domain.reservation.domain.QReservation; import lombok.RequiredArgsConstructor; @@ -22,7 +23,8 @@ public class ExperienceGiftQuerydslRepositoryImpl implements ExperienceGiftQuery @Override public List findGiftsBySttCategoryIdOrderByPriceDesc(Long SttCategoryId) { return queryFactory.selectFrom(experienceGift) - .where(experienceGift.sttCategory.sttCategoryId.eq(SttCategoryId)) + .where(experienceGift.sttCategory.sttCategoryId.eq(SttCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .orderBy(experienceGift.price.desc()) .fetch(); } @@ -30,7 +32,8 @@ public List findGiftsBySttCategoryIdOrderByPriceDesc(Long SttCat @Override public List findGiftsBySttCategoryIdOrderByPriceAsc(Long SttCategoryId) { return queryFactory.selectFrom(experienceGift) - .where(experienceGift.sttCategory.sttCategoryId.eq(SttCategoryId)) + .where(experienceGift.sttCategory.sttCategoryId.eq(SttCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .orderBy(experienceGift.price.asc()) .fetch(); } @@ -38,7 +41,8 @@ public List findGiftsBySttCategoryIdOrderByPriceAsc(Long SttCate @Override public List findGiftsByExpCategoryIdOrderByPriceDesc(Long ExpCategoryId) { return queryFactory.selectFrom(experienceGift) - .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId)) + .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .orderBy(experienceGift.price.desc()) .fetch(); } @@ -46,7 +50,8 @@ public List findGiftsByExpCategoryIdOrderByPriceDesc(Long ExpCat @Override public List findGiftsByExpCategoryIdOrderByPriceAsc(Long ExpCategoryId) { return queryFactory.selectFrom(experienceGift) - .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId)) + .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .orderBy(experienceGift.price.asc()) .fetch(); } @@ -57,7 +62,8 @@ public List findPopularGiftsBySttCategoryId(Long sttCategoryId) return queryFactory.selectFrom(experienceGift) .leftJoin(reservation).on(experienceGift.experienceGiftId.eq(reservation.experienceGift.experienceGiftId)) - .where(experienceGift.sttCategory.sttCategoryId.eq(sttCategoryId)) + .where(experienceGift.sttCategory.sttCategoryId.eq(sttCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .groupBy(experienceGift.experienceGiftId) .orderBy(reservation.id.count().desc()) .fetch(); @@ -69,7 +75,8 @@ public List findPopularGiftsByExpCategoryId(Long ExpCategoryId) return queryFactory.selectFrom(experienceGift) .leftJoin(reservation).on(experienceGift.experienceGiftId.eq(reservation.experienceGift.experienceGiftId)) - .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId)) + .where(experienceGift.expCategory.expCategoryId.eq(ExpCategoryId) + .and(experienceGift.status.eq(Status.ACTIVE))) .groupBy(experienceGift.experienceGiftId) .orderBy(reservation.id.count().desc()) .fetch(); @@ -81,6 +88,7 @@ public List findAllPopularGifts() { return queryFactory.selectFrom(experienceGift) .leftJoin(reservation).on(experienceGift.experienceGiftId.eq(reservation.experienceGift.experienceGiftId)) + .where(experienceGift.status.eq(Status.ACTIVE)) .groupBy(experienceGift.experienceGiftId) .orderBy(reservation.id.count().desc()) .fetch(); diff --git a/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftRepository.java b/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftRepository.java index 7b424618..ae630e76 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftRepository.java +++ b/src/main/java/com/shallwe/domain/experiencegift/domain/repository/ExperienceGiftRepository.java @@ -1,5 +1,6 @@ package com.shallwe.domain.experiencegift.domain.repository; +import com.shallwe.domain.common.Status; import com.shallwe.domain.experiencegift.domain.ExperienceGift; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,8 +11,8 @@ @Repository public interface ExperienceGiftRepository extends JpaRepository, ExperienceGiftQuerydslRepository{ - List findByTitleContains(String title); + List findByTitleContainsAndStatus(String title,Status status); Optional findByExperienceGiftId(Long experienceGiftId); - List findByShopOwnerId(Long id); + List findByShopOwnerIdAndStatus(Long id, Status status); } diff --git a/src/main/java/com/shallwe/domain/experiencegift/dto/request/AdminExperienceReq.java b/src/main/java/com/shallwe/domain/experiencegift/dto/request/AdminExperienceReq.java index c256153d..1dab8108 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/dto/request/AdminExperienceReq.java +++ b/src/main/java/com/shallwe/domain/experiencegift/dto/request/AdminExperienceReq.java @@ -16,7 +16,7 @@ public class AdminExperienceReq { @Schema(type = "String", description = "지역", maxLength = 30) @NotBlank(message = "지역은 필수 입력 값입니다.") - private String title; + private String subtitle; @Schema(type = "String", description = "경험 카테고리") private String expCategory; @@ -26,7 +26,7 @@ public class AdminExperienceReq { @Schema(type = "String", description = "상품명", maxLength = 100) @NotBlank(message = "상품명은 필수 입력 값입니다.") - private String subtitle; + private String title; @Schema(type = "String", description = "썸네일") private String giftImgUrl; diff --git a/src/main/java/com/shallwe/domain/experiencegift/presentation/AdminExperienceGiftController.java b/src/main/java/com/shallwe/domain/experiencegift/presentation/AdminExperienceGiftController.java index 339abc14..4bad22be 100644 --- a/src/main/java/com/shallwe/domain/experiencegift/presentation/AdminExperienceGiftController.java +++ b/src/main/java/com/shallwe/domain/experiencegift/presentation/AdminExperienceGiftController.java @@ -65,4 +65,33 @@ public ResponseCustom> getExperienceGift( ) { return ResponseCustom.OK(experienceGiftService.getExperienceGift(userPrincipal)); } + + @Operation(summary = "관리자 경험 선물 수정", description = "관리자 경험 선물 수정합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "관리자 경험 선물 수정 성공"), + @ApiResponse(responseCode = "400", description = "관리자 경험 선물 수정 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}), + }) + @PutMapping("/{experienceGiftId}") + public ResponseCustom modifyExperienceGift( + @PathVariable Long experienceGiftId, + @Parameter(description = "AccessToken 을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal, + @RequestBody AdminExperienceReq adminExperienceReq + ) { + this.experienceGiftService.modifyExperienceGift(experienceGiftId,userPrincipal, adminExperienceReq); + return ResponseCustom.OK(); + } + + @Operation(summary = "관리자 경험 선물 삭제", description = "관리자 경험 선물을 삭제합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "관리자 경험 선물 삭제 성공"), + @ApiResponse(responseCode = "400", description = "관리자 경험 선물 삭제 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}), + }) + @DeleteMapping("/{experienceGiftId}") + public ResponseCustom deleteExperienceGift( + @PathVariable Long experienceGiftId, + @Parameter(description = "AccessToken 을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal + ) { + experienceGiftService.deleteExperienceGift(experienceGiftId, userPrincipal); + return ResponseCustom.OK(); + } }