Skip to content

Commit

Permalink
Merge pull request #167 from ShallWeProject/feat/138-admin-gift-modify
Browse files Browse the repository at this point in the history
[Feat] 관리자 경험 선물 수정, 삭제 api 기능구현
  • Loading branch information
leeseunghakhello authored Nov 25, 2023
2 parents b29253b + 21e4197 commit 3e70e77
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public interface ExperienceGiftService {

AdminMainRes mainAdminExperienceGift(UserPrincipal userPrincipal);
List<AdminExperienceRes> getExperienceGift(UserPrincipal userPrincipal);

void modifyExperienceGift(Long experienceGiftId,UserPrincipal userPrincipal, AdminExperienceReq adminExperienceReq);

void deleteExperienceGift(Long experienceGiftId, UserPrincipal userPrincipal);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -121,13 +122,68 @@ public List<AdminExperienceRes> getExperienceGift(UserPrincipal userPrincipal) {
ShopOwner shopOwner = shopOwnerRepository.findById(userId)
.orElseThrow(InvalidShopOwnerException::new);

List<ExperienceGift> experienceGifts = experienceGiftRepository.findByShopOwnerId(shopOwner.getId());
List<ExperienceGift> 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<Explanation> 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 ) {
Expand All @@ -144,7 +200,7 @@ public ExperienceDetailRes createExperience(UserPrincipal userPrincipal,Experien
@Override
public List<ExperienceRes> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,31 +23,35 @@ public class ExperienceGiftQuerydslRepositoryImpl implements ExperienceGiftQuery
@Override
public List<ExperienceGift> 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();
}

@Override
public List<ExperienceGift> 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();
}

@Override
public List<ExperienceGift> 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();
}

@Override
public List<ExperienceGift> 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();
}
Expand All @@ -57,7 +62,8 @@ public List<ExperienceGift> 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();
Expand All @@ -69,7 +75,8 @@ public List<ExperienceGift> 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();
Expand All @@ -81,6 +88,7 @@ public List<ExperienceGift> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,8 +11,8 @@
@Repository
public interface ExperienceGiftRepository extends JpaRepository<ExperienceGift, Long>, ExperienceGiftQuerydslRepository{

List<ExperienceGift> findByTitleContains(String title);
List<ExperienceGift> findByTitleContainsAndStatus(String title,Status status);
Optional<ExperienceGift> findByExperienceGiftId(Long experienceGiftId);

List<ExperienceGift> findByShopOwnerId(Long id);
List<ExperienceGift> findByShopOwnerIdAndStatus(Long id, Status status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,33 @@ public ResponseCustom<List<AdminExperienceRes>> 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<Void> deleteExperienceGift(
@PathVariable Long experienceGiftId,
@Parameter(description = "AccessToken 을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal
) {
experienceGiftService.deleteExperienceGift(experienceGiftId, userPrincipal);
return ResponseCustom.OK();
}
}

0 comments on commit 3e70e77

Please sign in to comment.