Skip to content

Commit

Permalink
Merge pull request #40 from Central-MakeUs/dev
Browse files Browse the repository at this point in the history
fix: 수정사항 반영
  • Loading branch information
KarmaPol authored Feb 6, 2024
2 parents 45f18da + 5e7f6db commit d1333d0
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.mm.api.domain.buy.controller;

import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -20,6 +18,7 @@
import com.mm.api.common.swaggerAnnotation.SwaggerResponseBuy;
import com.mm.api.domain.buy.dto.request.RejectBuyRefundStatusRequest;
import com.mm.api.domain.buy.dto.response.BuyListResponse;
import com.mm.api.domain.buy.dto.response.BuyMeListResponse;
import com.mm.api.domain.buy.dto.response.BuyResponse;
import com.mm.api.domain.buy.service.BuyService;
import com.mm.coresecurity.oauth.OAuth2UserDetails;
Expand All @@ -44,7 +43,7 @@ public CommonResponse<BuyListResponse> getBuys(@RequestParam(required = false, d
return CommonResponse.ok(responses);
}

@Operation(summary = "구매 인증 상태를 변경합니다.", description = "refundStatus = [UNDER_EXAMINATION,IN_PROGRESS, COMPLETED, REJECTED]")
@Operation(summary = "구매 인증 상태를 변경합니다.", description = "refundStatus = [IN_PROGRESS, COMPLETED, REJECTED]")
@PatchMapping("/buys/{buyId}/refund-status")
public CommonResponse<BuyResponse> updateBuyRefundStatus(@PathVariable Long buyId,
@RequestParam String refundStatus) {
Expand All @@ -63,11 +62,20 @@ public CommonResponse<BuyResponse> rejectBuyRefundStatus(@PathVariable Long buyI
// 관리자 + 회원(자신만)
@Operation(summary = "구매 인증을 삭제합니다.")
@DeleteMapping("/buys/{buyId}")
public CommonResponse<?> deleteBuy(@PathVariable Long buyId) {
public CommonResponse<?> deleteBuy(@PathVariable Long buyId,
@AuthenticationPrincipal OAuth2UserDetails userDetails) {
buyService.deleteBuy(buyId);
return CommonResponse.noContent();
}

@Operation(summary = "구매 인증 단건을 조회합니다.")
@GetMapping("/buys/{buyId}")
public CommonResponse<BuyResponse> getBuy(@PathVariable Long buyId,
@AuthenticationPrincipal OAuth2UserDetails userDetails) {
BuyResponse response = buyService.getBuyResponse(buyId);
return CommonResponse.ok(response);
}

// 회원만
@Operation(summary = "구매 인증을 작성합니다.", description = "form으로 input type을 file로 지정해서 이미지를 첨부합니다.")
@PostMapping(value = "/buys/{memberId}/{itemId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Expand All @@ -77,10 +85,11 @@ public CommonResponse<BuyResponse> postBuy(@PathVariable Long memberId, @PathVar
return CommonResponse.ok(buyResponse);
}

@Operation(summary = "내 구매 인증을 전부 가져옵니다.")
@Operation(summary = "내 구매 인증을 페이지 단위로 가져옵니다.")
@GetMapping("/buys/me")
public CommonResponse<List<BuyResponse>> getBuysMe(@AuthenticationPrincipal OAuth2UserDetails userDetails) {
List<BuyResponse> responses = buyService.getBuysMe(userDetails);
public CommonResponse<BuyMeListResponse> getBuysMe(@RequestParam(required = false, defaultValue = "1") Integer page,
@AuthenticationPrincipal OAuth2UserDetails userDetails) {
BuyMeListResponse responses = buyService.getBuysMe(page, userDetails);
return CommonResponse.ok(responses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mm.api.domain.buy.dto.response;

import java.util.List;

public record BuyMeListResponse(Boolean isLastPage, List<BuyResponse> buyResponses) {
}
15 changes: 13 additions & 2 deletions api/src/main/java/com/mm/api/domain/buy/service/BuyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.mm.api.domain.buy.dto.request.RejectBuyRefundStatusRequest;
import com.mm.api.domain.buy.dto.response.BuyListResponse;
import com.mm.api.domain.buy.dto.response.BuyMeListResponse;
import com.mm.api.domain.buy.dto.response.BuyResponse;
import com.mm.api.exception.CustomException;
import com.mm.api.exception.ErrorCode;
Expand Down Expand Up @@ -86,12 +87,22 @@ public BuyResponse rejectBuyRefundStatus(Long buyId, RejectBuyRefundStatusReques
return BuyResponse.of(buy);
}

public List<BuyResponse> getBuysMe(OAuth2UserDetails userDetails) {
public BuyMeListResponse getBuysMe(Integer page, OAuth2UserDetails userDetails) {
Member member = getMember(userDetails.getId());
return buyRepository.findAllByMember(member)

List<BuyResponse> buyResponses = buyCustomRepository.getBuysMeByPage(page, member)
.stream()
.map(BuyResponse::of)
.toList();

Long pageNum = buyCustomRepository.getBuysMePageNum(member);
Boolean isLastPage = pageNum.equals(page.longValue());

return new BuyMeListResponse(isLastPage, buyResponses);
}

public BuyResponse getBuyResponse(Long buyId) {
return BuyResponse.of(getBuy(buyId));
}

private Buy getBuy(Long buyId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ public class ItemController {
@Operation(summary = "상품 글을 작성합니다.", description = """
itemCategoryType = [
CLOTHES,
BAG,
SHOES,
FASHION,
STUFF,
JEWELRY,
BEAUTY,
FOOD,
LIFE
ETC
]
""")
@PostMapping("/items")
Expand All @@ -58,14 +53,9 @@ public CommonResponse<ItemResponse> createItem(@RequestBody ItemCreateRequest re
@Operation(summary = "상품 글을 업데이트합니다.", description = """
itemCategoryType = [
CLOTHES,
BAG,
SHOES,
FASHION,
STUFF,
JEWELRY,
BEAUTY,
FOOD,
LIFE
ETC
]
""")
@PutMapping("/items/{id}")
Expand All @@ -85,14 +75,9 @@ public CommonResponse<?> deleteItem(@RequestParam Long id) {
@Operation(summary = "상품 글을 페이지 단위로 읽어옵니다.", description = """
itemCategoryType = [
CLOTHES,
BAG,
SHOES,
FASHION,
STUFF,
JEWELRY,
BEAUTY,
FOOD,
LIFE
ETC
]
아무것도 넣지 않으면 전체에서 읽어옵니다.
""")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.mm.api.domain.point.controller;

import java.util.List;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mm.api.common.response.CommonResponse;
import com.mm.api.common.swaggerAnnotation.SwaggerResponsePoint;
import com.mm.api.domain.buy.dto.response.BuyResponse;
import com.mm.api.domain.point.dto.response.PointsResponse;
import com.mm.api.domain.point.service.PointService;
import com.mm.coresecurity.oauth.OAuth2UserDetails;

Expand All @@ -34,17 +32,17 @@ public CommonResponse<Integer> getMyPoint(@AuthenticationPrincipal OAuth2UserDet

@Operation(summary = "현재 사용자의 누적 포인트 내역을 가져옵니다.")
@GetMapping("/points/history/cumulate")
public CommonResponse<List<BuyResponse>> getCumulativeHistory(
public CommonResponse<PointsResponse> getCumulativeHistory(
@AuthenticationPrincipal OAuth2UserDetails userDetails) {
List<BuyResponse> responses = pointService.getCumulativeHistory(userDetails);
return CommonResponse.ok(responses);
PointsResponse response = pointService.getCumulativeHistory(userDetails);
return CommonResponse.ok(response);
}

@Operation(summary = "현재 사용자의 예상 포인트 내역을 가져옵니다..")
@GetMapping("/points/history/expect")
public CommonResponse<List<BuyResponse>> getExpectedHistory(
public CommonResponse<PointsResponse> getExpectedHistory(
@AuthenticationPrincipal OAuth2UserDetails userDetails) {
List<BuyResponse> responses = pointService.getExpectedHistory(userDetails);
return CommonResponse.ok(responses);
PointsResponse response = pointService.getExpectedHistory(userDetails);
return CommonResponse.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mm.api.domain.point.dto.response;

import java.util.List;

import com.mm.api.domain.buy.dto.response.BuyResponse;

public record PointsResponse(Integer totalPoint, List<BuyResponse> buyResponses) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.mm.api.domain.buy.dto.response.BuyResponse;
import com.mm.api.domain.point.dto.response.PointsResponse;
import com.mm.api.exception.CustomException;
import com.mm.api.exception.ErrorCode;
import com.mm.coredomain.domain.Buy;
Expand All @@ -29,26 +30,48 @@ public Integer getMyPoint(OAuth2UserDetails userDetails) {
return getMember(memberId).getPoint();
}

public List<BuyResponse> getCumulativeHistory(OAuth2UserDetails userDetails) {
public PointsResponse getCumulativeHistory(OAuth2UserDetails userDetails) {
Long memberId = userDetails.getId();
Member member = getMember(memberId);

return buyRepository.findAllByMember(member)
List<Buy> buys = buyRepository.findAllByMember(member);
List<BuyResponse> buyResponses = buys
.stream()
.filter(this::isRefundCumulative)
.map(BuyResponse::of)
.toList();
Integer totalPoint = buys.stream()
.filter(this::isRefundCumulativeTotalPoint)
.map(Buy::getRefund)
.reduce(0, Integer::sum);

return new PointsResponse(totalPoint, buyResponses);
}

public List<BuyResponse> getExpectedHistory(OAuth2UserDetails userDetails) {
public PointsResponse getExpectedHistory(OAuth2UserDetails userDetails) {
Long memberId = userDetails.getId();
Member member = getMember(memberId);

return buyRepository.findAllByMember(member)
List<Buy> buys = buyRepository.findAllByMember(member);
List<BuyResponse> buyResponses = buyRepository.findAllByMember(member)
.stream()
.filter(this::isRefundExpected)
.map(BuyResponse::of)
.toList();
Integer totalPoint = buys.stream()
.filter(this::isRefundExpectedTotalPoint)
.map(Buy::getRefund)
.reduce(0, Integer::sum);

return new PointsResponse(totalPoint, buyResponses);
}

private boolean isRefundCumulativeTotalPoint(Buy buy) {
return buy.getRefundStatus().equals(RefundStatus.COMPLETED);
}

private boolean isRefundExpectedTotalPoint(Buy buy) {
return buy.getRefundStatus().equals(RefundStatus.IN_PROGRESS);
}

private boolean isRefundCumulative(Buy buy) {
Expand All @@ -57,8 +80,7 @@ private boolean isRefundCumulative(Buy buy) {
}

private boolean isRefundExpected(Buy buy) {
return buy.getRefundStatus().equals(RefundStatus.COMPLETED) ||
buy.getRefundStatus().equals(RefundStatus.REJECTED);
return buy.getRefundStatus().equals(RefundStatus.IN_PROGRESS);
}

private Member getMember(Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
@Getter
public enum ItemCategoryType {
CLOTHES("의류", 3),
BAG("가방", 3),
SHOES("슈즈", 3),
FASHION("패션잡화", 3),
JEWELRY("주얼리", 3),
BEAUTY("뷰티", 3),
FOOD("푸드", 3),
LIFE("라이프", 3);
ETC("기타", 3);

private final String value;
private final Integer refundPercent;
Expand All @@ -27,7 +23,7 @@ public static ItemCategoryType fromValue(String input) {
return categoryType;
}
}
return ItemCategoryType.FASHION;
return ItemCategoryType.ETC;
}

public static ItemCategoryType of(String input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mm.coredomain.domain;

public enum RefundStatus {
UNDER_EXAMINATION,
IN_PROGRESS,
COMPLETED,
REJECTED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import java.util.List;

import com.mm.coredomain.domain.Buy;
import com.mm.coredomain.domain.Member;

public interface BuyCustomRepository {
List<Buy> getBuysByPage(Integer page);

List<Buy> getBuysMeByPage(Integer page, Member member);

Long getPageNum();

Long getBuysMePageNum(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Repository;

import com.mm.coredomain.domain.Buy;
import com.mm.coredomain.domain.Member;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -17,6 +18,7 @@ public class BuyCustomRepositoryImpl implements BuyCustomRepository {
private final JPAQueryFactory jpaQueryFactory;

private static final Long PAGE_OFFSET = 10L;
private static final Long PAGE_OFFSET_ME = 9L;

@Override
public List<Buy> getBuysByPage(Integer page) {
Expand All @@ -26,6 +28,15 @@ public List<Buy> getBuysByPage(Integer page) {
.fetch();
}

@Override
public List<Buy> getBuysMeByPage(Integer page, Member member) {
return jpaQueryFactory.selectFrom(buy)
.where(buy.member.eq(member))
.offset((page - 1) * PAGE_OFFSET_ME)
.limit(PAGE_OFFSET_ME)
.fetch();
}

@Override
public Long getPageNum() {
Long count = jpaQueryFactory.select(buy.count())
Expand All @@ -36,4 +47,16 @@ public Long getPageNum() {
}
return count / PAGE_OFFSET;
}

@Override
public Long getBuysMePageNum(Member member) {
Long count = jpaQueryFactory.select(buy.count())
.from(buy)
.where(buy.member.eq(member))
.fetchOne();
if (count % PAGE_OFFSET_ME != 0) {
return count / PAGE_OFFSET_ME + 1;
}
return count / PAGE_OFFSET_ME;
}
}
Loading

0 comments on commit d1333d0

Please sign in to comment.