Skip to content

Commit

Permalink
fix: deal response service
Browse files Browse the repository at this point in the history
- deal response에 review여부 및 deal_id 추가
- deal list get API에 시간 범위로 거르는 기능 추가
  • Loading branch information
ah9mon committed Aug 14, 2023
1 parent a4e50d0 commit 46ecece
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/anywayclear/controller/DealController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import javax.validation.Valid;
import java.net.URI;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/api/deals")
Expand All @@ -30,8 +32,14 @@ public ResponseEntity<Void> createDeal(@Valid @RequestBody DealCreateRequest req
}

@GetMapping
public ResponseEntity<MultiResponse<DealResponse, Deal>> getDealList(@AuthenticationPrincipal OAuth2User oAuth2User, Pageable pageable) {
String userId = (String) oAuth2User.getAttributes().get("userId");
return ResponseEntity.ok(dealService.getDealList(userId, pageable));
public ResponseEntity<MultiResponse<DealResponse, Deal>> getDealList(
@RequestParam(value = "user-id") String userId,
@RequestParam(value = "start-date", required = false) String startDateString,
@RequestParam(value = "end-date", required = false) String endDateString,
Pageable pageable) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate = LocalDateTime.parse(startDateString + " 00:00:00", formatter);
LocalDateTime endDate = LocalDateTime.parse(endDateString + " 00:00:00", formatter);
return ResponseEntity.ok(dealService.getDealList(userId, startDate, endDate, pageable));
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/anywayclear/dto/response/DealResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,34 @@
@Getter
@Setter
public class DealResponse {
private long dealId;
private int endPrice;
private boolean isPaid;
private Member consumer;
private Member seller;
private ProduceResponse produce;
private boolean isReviewed;

@Builder
public DealResponse(int endPrice, boolean isPaid, Member consumer, Member seller, ProduceResponse produce) {
public DealResponse(long dealId,int endPrice, boolean isPaid, Member consumer, Member seller, ProduceResponse produce, boolean isReviewed) {
this.dealId = dealId;
this.endPrice = endPrice;
this.isPaid = isPaid;
this.consumer = consumer;
this.seller = seller;
this.produce = produce;
this.isReviewed = isReviewed;
}

public static DealResponse toResponse(Deal deal) {
return DealResponse.builder()
.dealId(deal.getId())
.endPrice(deal.getEndPrice())
.isPaid(deal.isPaid())
.consumer(deal.getConsumer())
.seller(deal.getSeller())
.produce(ProduceResponse.toResponse(deal.getProduce()))
.isReviewed((deal.getReview() != null) ? true : false)
.build();
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/anywayclear/dto/response/ReviewResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public class ReviewResponse {
private int score;
private LocalDateTime createdAt;
private DealResponse deal;
private String memberUserId;
private String memberNickname;
private String reviewerId;
private String reviewerNickname;

@Builder
public ReviewResponse(Long id, String comment, int score, LocalDateTime createdAt, DealResponse deal, String memberUserId, String memberNickname) {
public ReviewResponse(Long id, String comment, int score, LocalDateTime createdAt, DealResponse deal, String reviewerId, String reviewerNickname) {
this.id = id;
this.comment = comment;
this.score = score;
this.createdAt = createdAt;
this.deal = deal;
this.memberUserId = memberUserId;
this.memberNickname = memberNickname;
this.reviewerId = reviewerId;
this.reviewerNickname = reviewerNickname;
}

public static ReviewResponse toResponse(Review review) {
Expand All @@ -35,8 +35,8 @@ public static ReviewResponse toResponse(Review review) {
.score(review.getScore())
.createdAt(review.getCreatedAt())
.deal(DealResponse.toResponse(review.getDeal()))
.memberUserId(review.getMember().getUserId())
.memberNickname(review.getMember().getNickname())
.reviewerId(review.getMember().getUserId())
.reviewerNickname(review.getMember().getNickname())
.build();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/anywayclear/repository/DealRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;

public interface DealRepository extends JpaRepository<Deal, Long> {

Page<Deal> findAllByConsumer(Member member, Pageable pageable);

Page<Deal> findAllBySeller(Member member, Pageable pageable);

Page<Deal> findAllByConsumerAndProduce_EndDateBetween(
Member member,
LocalDateTime startDate,
LocalDateTime endDate,
Pageable pageable);

Page<Deal> findAllBySellerAndProduce_EndDateBetween(
Member member,
LocalDateTime startDate,
LocalDateTime endDate,
Pageable pageable);
}
19 changes: 14 additions & 5 deletions src/main/java/com/anywayclear/service/DealService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.anywayclear.exception.ExceptionCode.INVALID_DEAL;
Expand All @@ -38,13 +39,21 @@ public DealResponse getDeal(Long id) {
}

@Transactional(readOnly = true)
public MultiResponse<DealResponse, Deal> getDealList(String userId, Pageable pageable) {
public MultiResponse<DealResponse, Deal> getDealList(String userId, LocalDateTime startDate, LocalDateTime endDate, Pageable pageable) {
Member member = memberRepository.findByUserId(userId).orElseThrow(() -> new CustomException(INVALID_MEMBER));
Page<Deal> dealPage;
if (member.getRole().equals("ROLE_SELLER")) { // 판매자 일 경우
dealPage = dealRepository.findAllBySeller(member, pageable);
} else { // 소비자 일 경우
dealPage = dealRepository.findAllByConsumer(member, pageable);
if (startDate != null && endDate != null) {
if (member.getRole().equals("ROLE_SELLER")) { // 판매자 일 경우
dealPage = dealRepository.findAllBySellerAndProduce_EndDateBetween(member, startDate, endDate, pageable);
} else { // 소비자 일 경우
dealPage = dealRepository.findAllByConsumerAndProduce_EndDateBetween(member, startDate, endDate, pageable);
}
} else {
if (member.getRole().equals("ROLE_SELLER")) { // 판매자 일 경우
dealPage = dealRepository.findAllBySeller(member, pageable);
} else { // 소비자 일 경우
dealPage = dealRepository.findAllByConsumer(member, pageable);
}
}
List<DealResponse> dealResponseList = dealPage.map(DealResponse::toResponse).getContent();
return new MultiResponse<>(dealResponseList, dealPage);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/anywayclear/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public MultiResponse<ReviewResponse, Review> getReviewList(String userId, Pageab
Page<Review> reviewPage;

boolean isSeller = member.getRole().equals("ROLE_SELLER");
boolean hasQuery = q != null;
boolean hasSort = sortedBy != null;
boolean hasQuery = (q != null);
boolean hasSort = (sortedBy != null);

if (isSeller) {
if (!hasQuery && !hasSort) {
Expand Down

0 comments on commit 46ecece

Please sign in to comment.