Skip to content

Commit

Permalink
Merge pull request #99 from MarketPlace-O2O-Platform/refactor/#93
Browse files Browse the repository at this point in the history
[Refactor/#93] 쿠폰 관련 리팩토링 진행
  • Loading branch information
kangwook1 authored Jan 17, 2025
2 parents 66ddd6e + e8849e8 commit 1bcb193
Show file tree
Hide file tree
Showing 23 changed files with 234 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Tag(name = "[공감]", description = "[공감] 임시 매장 공감하기")
@RestController
@RequiredArgsConstructor
@RequestMapping("/cheer")
@RequestMapping("/api/cheer")
public class CheerController {

private final CheerService cheerService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.appcenter.marketplace.domain.coupon;

import com.appcenter.marketplace.domain.coupon.dto.req.CouponUpdateReq;
import com.appcenter.marketplace.domain.coupon.dto.req.CouponReq;
import com.appcenter.marketplace.domain.market.Market;
import com.appcenter.marketplace.global.common.BaseEntity;
import jakarta.persistence.*;
Expand Down Expand Up @@ -54,11 +54,11 @@ public Coupon(String name, String description, LocalDateTime deadLine, Integer s
this.isDeleted = isDeleted;
}

public void update(CouponUpdateReq couponUpdateReq) {
this.name = couponUpdateReq.getCouponName();
this.description = couponUpdateReq.getDescription();
this.deadLine = couponUpdateReq.getDeadLine();
this.stock = couponUpdateReq.getStock();
public void update(CouponReq couponUpReq) {
this.name = couponUpReq.getCouponName();
this.description = couponUpReq.getDescription();
this.deadLine = couponUpReq.getDeadLine();
this.stock = couponUpReq.getStock();
}

public void updateHidden() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.appcenter.marketplace.domain.coupon.controller;

import com.appcenter.marketplace.domain.coupon.dto.res.CouponMemberRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponPageRes;
import com.appcenter.marketplace.domain.coupon.service.CouponService;
import com.appcenter.marketplace.global.common.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -9,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
Expand All @@ -23,11 +25,17 @@ public class CouponController {

private final CouponService couponService;

@Operation(summary = "유효 쿠폰 조회 리스트", description = "사장님이 공개처리 & 만료되지 않은 쿠폰 리스트가 조회됩니다." +
"<br> 매장 상세 정보에서 조회가 이루어집니다.")
@Operation(summary = "유효 쿠폰 조회 리스트", description = "사장님이 공개처리 & 만료되지 않은 쿠폰 리스트가 조회됩니다. <br>" +
"매장 상세 정보에서 조회가 이루어집니다. <br> " +
"couponId는 다음 페이징 처리를 위해 사용되는 파라미터 입니다. 다음")
@GetMapping
public ResponseEntity<CommonResponse<List<CouponMemberRes>>> getCouponList(Long marketId) {
return ResponseEntity.status(COUPON_FOUND.getStatus()).body(CommonResponse.from(COUPON_FOUND.getMessage(),couponService.getCouponList(marketId)));
public ResponseEntity<CommonResponse<CouponPageRes<CouponMemberRes>>> getCouponList(
@RequestParam(name= "marketId")Long marketId,
@RequestParam(name="couponId", required = false) Long couponId,
@RequestParam(name="size", defaultValue = "10") Integer size
) {
return ResponseEntity.status(COUPON_FOUND.getStatus())
.body(CommonResponse.from(COUPON_FOUND.getMessage(),couponService.getCouponList(marketId, couponId, size)));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.appcenter.marketplace.domain.coupon.controller;

import com.appcenter.marketplace.domain.coupon.dto.req.CouponReq;
import com.appcenter.marketplace.domain.coupon.dto.req.CouponUpdateReq;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponHiddenRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponPageRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponRes;
import com.appcenter.marketplace.domain.coupon.service.CouponOwnerService;
import com.appcenter.marketplace.global.common.CommonResponse;
Expand Down Expand Up @@ -41,16 +41,19 @@ public ResponseEntity<CommonResponse<CouponRes>> getCoupon(@PathVariable Long co

@Operation(summary = "사장님 매장별 전체 쿠폰 조회", description = "사장님의 특정 매장(MarketId)의 전체 쿠폰 리스트를 조회합니다.")
@GetMapping("/coupons")
public ResponseEntity<CommonResponse<List<CouponRes>>> getCouponList(@RequestParam(name= "marketId")Long id) {
return ResponseEntity.status(COUPON_FOUND.getStatus()).body(CommonResponse.from(COUPON_FOUND.getMessage(),couponService.getCouponList(id)));
public ResponseEntity<CommonResponse<CouponPageRes<CouponRes>>> getCouponList(@RequestParam(name= "marketId")Long marketId,
@RequestParam(name="couponId", required = false) Long couponId,
@RequestParam(name="size", defaultValue = "10") Integer size
) {
return ResponseEntity.status(COUPON_FOUND.getStatus()).body(CommonResponse.from(COUPON_FOUND.getMessage(),couponService.getCouponList(marketId, couponId, size)));
}

@Operation(summary = "사장님 쿠폰 내용 수정", description = "사장님이 생성한 쿠폰의 내용을 수정합니다. " +
"<br> '숨김처리'를 제외한 내용을 수정할 수 있습니다. ")
@PutMapping("/coupons/{couponId}")
public ResponseEntity<CommonResponse<CouponRes>> updateCoupon(@RequestBody @Valid CouponUpdateReq couponUpdateReq,
public ResponseEntity<CommonResponse<CouponRes>> updateCoupon(@RequestBody @Valid CouponReq couponReq,
@PathVariable Long couponId ){
return ResponseEntity.status(COUPON_UPDATE.getStatus()).body(CommonResponse.from(COUPON_UPDATE.getMessage(),couponService.updateCoupon(couponUpdateReq, couponId)));
return ResponseEntity.status(COUPON_UPDATE.getStatus()).body(CommonResponse.from(COUPON_UPDATE.getMessage(),couponService.updateCoupon(couponReq, couponId)));
}

@Operation(summary = "숨김/공개 처리 기능", description = "사장님은 생성한 쿠폰을 숨김 / 공개 처리 할 수 있습니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.appcenter.marketplace.domain.coupon.dto.res;

import lombok.Getter;

import java.util.List;

@Getter
public class CouponPageRes<T> {
private final List<T> couponResDtos;
private final boolean hasNext;

public CouponPageRes(List<T> couponResDtos, boolean hasNext){
this.couponResDtos = couponResDtos;
this.hasNext = hasNext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import java.util.List;

public interface CouponRepositoryCustom {
List<CouponRes> findCouponsForOwnerByMarketId(Long marketId, Long couponId, Integer size);

List<CouponRes> findOwnerCouponResDtoByMarketId(Long marketId);

List<CouponMemberRes> findMemberCouponResDtoByMarketId(Long marketId);
List<CouponMemberRes> findCouponsForMemberByMarketId(Long marketId, Long couponId, Integer size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.appcenter.marketplace.domain.coupon.dto.res.CouponRes;
import com.appcenter.marketplace.domain.coupon.dto.res.QCouponMemberRes;
import com.appcenter.marketplace.domain.coupon.dto.res.QCouponRes;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

Expand All @@ -19,7 +20,7 @@ public class CouponRepositoryCustomImpl implements CouponRepositoryCustom {

// 사장님) '숨김처리'에 관계없이 발행한 모든 쿠폰을 확인할 수 있습니다.
@Override
public List<CouponRes> findOwnerCouponResDtoByMarketId(Long marketId) {
public List<CouponRes> findCouponsForOwnerByMarketId(Long marketId, Long couponId, Integer size) {

return jpaQueryFactory.select(new QCouponRes(coupon.id,
coupon.market.id,
Expand All @@ -30,25 +31,39 @@ public List<CouponRes> findOwnerCouponResDtoByMarketId(Long marketId) {
coupon.isHidden))
.from(coupon)
.join(market).on(coupon.market.id.eq(market.id))
.where(coupon.market.id.eq(marketId)
.where(ltCouponId(couponId)
.and(coupon.market.id.eq(marketId))
.and(coupon.isDeleted.eq(false)))
.orderBy(coupon.id.desc())
.limit(size + 1)
.fetch();
}

// 유저) 사장님이 발행한 쿠폰 중 '공개처리'가 된 쿠폰들만 유저는 리스트에서 확인 가능합니다.
@Override
public List<CouponMemberRes> findMemberCouponResDtoByMarketId(Long marketId) {
public List<CouponMemberRes> findCouponsForMemberByMarketId(Long marketId, Long couponId, Integer size) {

return jpaQueryFactory.select(new QCouponMemberRes(coupon.id,
coupon.name,
coupon.description,
coupon.deadLine))
.from(coupon)
.innerJoin(coupon).on(coupon.market.id.eq(market.id))
.where(coupon.market.id.eq(marketId)
.where(ltCouponId(couponId)
.and(coupon.market.id.eq(marketId))
.and(coupon.isDeleted.eq(false))
.and(coupon.isHidden.eq(false)))
.orderBy(coupon.id.desc())
.limit(size + 1)
.fetch();

}

private BooleanBuilder ltCouponId(Long couponId) {
BooleanBuilder builder = new BooleanBuilder();
if( couponId != null){
builder.and(coupon.id.lt(couponId));
}
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.appcenter.marketplace.domain.coupon.service;

import com.appcenter.marketplace.domain.coupon.dto.req.CouponReq;
import com.appcenter.marketplace.domain.coupon.dto.req.CouponUpdateReq;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponHiddenRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponPageRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponRes;

import java.util.List;

public interface CouponOwnerService {

// 쿠폰 등록 메서드
Expand All @@ -16,10 +14,10 @@ public interface CouponOwnerService {
CouponRes getCoupon(Long couponId);

// 매장별 전체 쿠폰 확인 메서드
List<CouponRes> getCouponList(Long marketId);
CouponPageRes<CouponRes> getCouponList(Long marketId, Long CouponId, Integer size);

// 쿠폰 내용 수정 메서드
CouponRes updateCoupon(CouponUpdateReq couponUpdateReq, Long couponId);
CouponRes updateCoupon(CouponReq couponReq, Long couponId);

// 쿠폰 숨김 처리 메서드
CouponHiddenRes updateCouponHidden(Long couponId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.appcenter.marketplace.domain.coupon.service;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponMemberRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponPageRes;

import java.util.List;

public interface CouponService {

List<CouponMemberRes> getCouponList(Long marketId);
CouponPageRes<CouponMemberRes> getCouponList(Long marketId, Long couponId, Integer size);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.appcenter.marketplace.domain.coupon.Coupon;
import com.appcenter.marketplace.domain.coupon.dto.req.CouponReq;
import com.appcenter.marketplace.domain.coupon.dto.req.CouponUpdateReq;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponHiddenRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponPageRes;
import com.appcenter.marketplace.domain.coupon.dto.res.CouponRes;
import com.appcenter.marketplace.domain.coupon.repository.CouponRepository;
import com.appcenter.marketplace.domain.coupon.service.CouponOwnerService;
Expand Down Expand Up @@ -41,16 +41,18 @@ public CouponRes getCoupon(Long couponId) {

@Override
@Transactional
public List<CouponRes> getCouponList(Long marketId) {
public CouponPageRes<CouponRes> getCouponList(Long marketId, Long couponId, Integer size) {
Market market = findMarketById(marketId);
return couponRepository.findOwnerCouponResDtoByMarketId(market.getId());
List<CouponRes> couponResList = couponRepository.findCouponsForOwnerByMarketId(market.getId(), couponId, size);

return checkNextPageAndReturn(couponResList, size);
}

@Override
@Transactional
public CouponRes updateCoupon(CouponUpdateReq couponUpdateReq, Long couponId) {
public CouponRes updateCoupon(CouponReq couponReq, Long couponId) {
Coupon coupon = findCouponById(couponId);
coupon.update(couponUpdateReq);
coupon.update(couponReq);
return CouponRes.toDto(coupon);
}

Expand Down Expand Up @@ -85,4 +87,15 @@ private Coupon findCouponById(Long couponId) {

else throw new CustomException(COUPON_IS_DELETED);
}

private <T> CouponPageRes<T> checkNextPageAndReturn(List<T> couponList, Integer size) {
boolean hasNext = false;

if(couponList.size() > size){
hasNext = true;
couponList.remove(size.intValue());
}

return new CouponPageRes<>(couponList, hasNext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,25 @@ public class CouponServiceImpl implements CouponService {

@Override
@Transactional
public List<CouponMemberRes> getCouponList(Long marketId) {
public CouponPageRes<CouponMemberRes> getCouponList(Long marketId, Long couponId, Integer size) {
Market market = findMarketById(marketId);
List<CouponMemberRes> couponList = couponRepository.findMemberCouponResDtoByMarketId(marketId);
return couponList;
List<CouponMemberRes> couponList = couponRepository.findCouponsForMemberByMarketId(market.getId(), couponId, size);
return checkNextPageAndReturn(couponList, size);
}

private Market findMarketById(Long marketId) {
return marketRepository.findById(marketId).orElseThrow(() -> new CustomException(MARKET_NOT_EXIST));

}

private <T>CouponPageRes<T> checkNextPageAndReturn(List<T> couponList, Integer size) {
boolean hasNext = false;

if(couponList.size() > size){
hasNext = true;
couponList.remove(size.intValue());
}

return new CouponPageRes<>(couponList, hasNext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ResponseEntity<CommonResponse<List<MarketRes>>> getTopFavoriteMarketList(
@GetMapping("/top-latest-coupon")
public ResponseEntity<CommonResponse<List<TopLatestCouponRes>>> getLatestTopCouponList(
@RequestParam Long memberId,
@RequestParam(defaultValue = "10", name = "count") Integer size) {
@RequestParam(defaultValue = "10", name = "size") Integer size) {
return ResponseEntity.ok(CommonResponse.from(COUPON_FOUND.getMessage(),
marketService.getTopLatestCoupons(memberId, size)));
}
Expand All @@ -152,13 +152,13 @@ public ResponseEntity<CommonResponse<List<TopLatestCouponRes>>> getLatestTopCoup
public ResponseEntity<CommonResponse<MarketPageRes<LatestCouponRes>>> getLatestMarketByCoupon(
@RequestParam(required = true) Long memberId,
@Parameter(description = "각 페이지의 마지막 couponId (e.g. 5)")
@RequestParam(required = false, name = "lastPageIndex") Long couponId,
@Parameter(description = "위에 작성한 couponId의 createdAt (e.g. 2024-11-20T00:59:33.469 OR 2024-11-20T00:59:33.469664 )")
@RequestParam(required = false, name = "lastPageIndex") Long marketId,
@Parameter(description = "위에 작성한 marketId의 createdAt (e.g. 2024-11-20T00:59:33.469 OR 2024-11-20T00:59:33.469664 )")
@RequestParam(required = false, name = "lastCreatedAt") LocalDateTime lastCreatedAt,
@RequestParam(defaultValue = "10", name = "pageSize") Integer size) {
return ResponseEntity
.ok(CommonResponse.from(MARKET_FOUND.getMessage(),
marketService.getLatestCouponPage(memberId, lastCreatedAt, couponId, size)));
marketService.getLatestCouponPage(memberId, lastCreatedAt, marketId, size)));
}

@Operation(summary = "마감 임박 쿠폰 TOP 조회",
Expand All @@ -167,7 +167,7 @@ public ResponseEntity<CommonResponse<MarketPageRes<LatestCouponRes>>> getLatestM
"만약 쿠폰의 마감일자가 같을 시, 최신 등록 매장 순으로 보여지게 됩니다.")
@GetMapping("/top-closing-coupon")
public ResponseEntity<CommonResponse<List<TopClosingCouponRes>>> getClosingTopCouponList(
@RequestParam(defaultValue = "10", name="count") Integer size){
@RequestParam(defaultValue = "10", name="size") Integer size){
return ResponseEntity.ok(CommonResponse.from(COUPON_FOUND.getMessage(),
marketService.getTopClosingCoupons(size)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.appcenter.marketplace.domain.member_coupon;

public enum MemberCouponType {
ISSUED, EXPIRED, USED
}
Loading

0 comments on commit 1bcb193

Please sign in to comment.