Skip to content

Commit

Permalink
fix: CartGroupMemberResponse Convert Response (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Jul 12, 2024
1 parent e10c1c0 commit 828511f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.api.ttoklip.domain.privacy.dto;

import com.api.ttoklip.domain.privacy.domain.Interest;
public record InterestResponse(Long id, String categoryName) {
public record InterestResponse(String categoryName) {

public static InterestResponse from(final Interest interest) {
return new InterestResponse(interest.getId(), interest.getCategory().getName());
return new InterestResponse(interest.getCategory().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.api.ttoklip.domain.town.cart.constant.CartResponseConstant;
import com.api.ttoklip.domain.town.cart.post.dto.request.CartCreateRequest;
import com.api.ttoklip.domain.town.cart.post.dto.request.UpdateStatusRequest;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartMemberResponse;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartGroupMemberResponse;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartSingleResponse;
import com.api.ttoklip.domain.town.cart.post.entity.CartMember;
import com.api.ttoklip.domain.town.cart.post.entity.TradeStatus;
import com.api.ttoklip.domain.town.cart.post.service.CartPostService;
import com.api.ttoklip.global.success.Message;
Expand All @@ -31,8 +30,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "Cart", description = "우리동네 - 함께해요 API 입니다.")
@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -185,6 +182,7 @@ public SuccessResponse<Long> countParticipants(final @PathVariable Long cartId)
Long count = cartPostService.countParticipants(cartId);
return new SuccessResponse<>(count);
}

@Operation(summary = "참여자 확인", description = "특정 카트의 참여자를 확인합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "참여자 확인 성공",
Expand All @@ -196,9 +194,9 @@ public SuccessResponse<Long> countParticipants(final @PathVariable Long cartId)
description = "참여자를 확인하였습니다."
)))})
@GetMapping("/participants/members/{cartId}")
public SuccessResponse<List<CartMemberResponse>> checkParticipants(final @PathVariable Long cartId) {
List<CartMemberResponse>cartMembers = cartPostService.checkParticipants(cartId);
return new SuccessResponse<>(cartMembers);
public SuccessResponse<CartGroupMemberResponse> checkParticipants(final @PathVariable Long cartId) {
CartGroupMemberResponse cartGroupMemberResponse = cartPostService.checkParticipants(cartId);
return new SuccessResponse<>(cartGroupMemberResponse);
// Long count = cartPostService.countParticipants(cartId);
// return new SuccessResponse<>(count);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package com.api.ttoklip.domain.town.cart.post.dto.response;

import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.privacy.domain.Interest;
import com.api.ttoklip.domain.privacy.dto.InterestResponse;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class CartMemberResponse {
private String nickname;
private String profileImgUrl;
private String email;
private List<InterestResponse> interests;


@Builder
private CartMemberResponse(final String nickname, final String profileImgUrl, final String email,
final List<InterestResponse> interests) {
this.nickname = nickname;
this.profileImgUrl = profileImgUrl;
this.email = email;
this.interests = interests;
}

public static CartMemberResponse of(final Member member, final List<Interest> interests) {

List<InterestResponse> interestResponses = interests.stream()
.map(InterestResponse::from)
.toList();

return CartMemberResponse.builder()
.nickname(member.getNickname())
.profileImgUrl(member.getProfile().getProfileImgUrl())
.email(member.getEmail())
.interests(interestResponses)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import static com.api.ttoklip.domain.town.cart.post.entity.QCartMember.cartMember;
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;

import com.api.ttoklip.domain.privacy.domain.QInterest;
import com.api.ttoklip.domain.privacy.domain.QProfile;
import com.api.ttoklip.domain.town.cart.comment.CartComment;
import com.api.ttoklip.domain.town.cart.post.entity.Cart;
import com.api.ttoklip.global.exception.ApiException;
Expand All @@ -33,6 +35,8 @@ public Cart findByIdActivated(final Long cartId) {
matchId(cartId), getCartActivate()
)
.leftJoin(cart.member, member).fetchJoin()
.leftJoin(cart.member.interests, QInterest.interest).fetchJoin()
.leftJoin(cart.member.profile, QProfile.profile).fetchJoin()
.fetchOne();
return Optional.ofNullable(findCart)
.orElseThrow(() -> new ApiException(ErrorType.CART_NOT_FOUND));
Expand Down Expand Up @@ -137,4 +141,4 @@ public List<Cart> findRecent3() {
.limit(3)
.fetch();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.mypage.main.dto.response.UserCartSingleResponse;
import com.api.ttoklip.domain.notification.aop.annotation.SendNotification;
import com.api.ttoklip.domain.privacy.dto.InterestResponse;
import com.api.ttoklip.domain.town.cart.comment.CartComment;
import com.api.ttoklip.domain.town.cart.image.service.CartImageService;
import com.api.ttoklip.domain.town.cart.itemUrl.service.ItemUrlService;
import com.api.ttoklip.domain.town.cart.post.dto.request.CartCreateRequest;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartGroupMemberResponse;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartMemberResponse;
import com.api.ttoklip.domain.town.cart.post.dto.response.CartSingleResponse;
import com.api.ttoklip.domain.town.cart.post.editor.CartPostEditor;
Expand All @@ -26,8 +26,6 @@
import com.api.ttoklip.global.success.Message;
import jakarta.persistence.EntityManager;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -257,26 +255,29 @@ public Message removeParticipant(final Long cartId) {

public Long countParticipants(final Long cartId) {
Cart cart = cartRepository.findByIdActivated(cartId);
System.out.println("카트카트카트"+cart.getCartMembers().listIterator());
System.out.println("카트카트카트" + cart.getCartMembers().listIterator());
return cartRepository.countParticipants(cartId);
}
public List<CartMemberResponse> checkParticipants(final Long cartId){

public CartGroupMemberResponse checkParticipants(final Long cartId) {
Long currentMemberId = getCurrentMember().getId();
if(cartMemberRepository.findByMemberIdAndCartId(currentMemberId, cartId).isEmpty()){
throw new ApiException(ErrorType.NOT_PARTICIPATED);
}

isAllReadyParticipants(cartId, currentMemberId);

Cart cart = cartRepository.findByIdActivated(cartId);
List<CartMember> cartMembers=cart.getCartMembers();
return cartMembers.stream()
.map(member -> CartMemberResponse.builder()
.nickname(member.getMember().getNickname())
.profileImgUrl(member.getMember().getProfile().getProfileImgUrl())
.email(member.getMember().getEmail())
.interests(member.getMember().getInterests().stream()
.map(InterestResponse::from)
.collect(Collectors.toList()))
.build())
.collect(Collectors.toList());

List<CartMemberResponse> cartMemberResponses = cart.getCartMembers().stream()
.map(cartMember -> CartMemberResponse.of(
cartMember.getMember(), cartMember.getMember().getInterests()
)
).toList();

return CartGroupMemberResponse.of(cartMemberResponses);
}

private void isAllReadyParticipants(final Long cartId, final Long currentMemberId) {
cartMemberRepository.findByMemberIdAndCartId(currentMemberId, cartId)
.orElseThrow(() -> new ApiException(ErrorType.NOT_PARTICIPATED));
}
/* -------------------------------------------- PARTICIPANT 끝 -------------------------------------------- */

Expand Down

0 comments on commit 828511f

Please sign in to comment.