diff --git a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperCommandFacade.java b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperCommandFacade.java new file mode 100644 index 0000000..535def2 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperCommandFacade.java @@ -0,0 +1,21 @@ +package slvtwn.khu.toyouserver.application; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import slvtwn.khu.toyouserver.dto.RollingPaperExchangeCountResponse; + +@Component +@RequiredArgsConstructor +public class RollingPaperCommandFacade { + + private final RollingPaperService rollingPaperService; + private final UserService userService; + + public RollingPaperExchangeCountResponse getExchangeCount(Long userId) { + return RollingPaperExchangeCountResponse.of( + userService.getProfile(userId).name(), + rollingPaperService.countSentRollingPapers(userId), + rollingPaperService.countReceivedRollingPapers(userId) + ); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java index 8bbd29f..fa6e94c 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/RollingPaperService.java @@ -90,6 +90,17 @@ public RollingPaperPagedResponse findReceivedRollingPapers(Long userId, Long gro return RollingPaperPagedResponse.from(cursorPageInfo, responses); } + public Long countSentRollingPapers(Long userId) { + return rollingPaperRepository.countAllBySenderSnapshot_SenderId(userId); + } + + public Long countReceivedRollingPapers(Long userId) { + List members = memberRepository.findByUserId(userId); + return rollingPaperRepository.findAllByMemberIn(members).stream() + .filter(rollingPaper -> !rollingPaper.getSenderSnapshot().getSenderId().equals(userId)) + .count(); + } + private List parseStickers(RollingPaperRequest request, RollingPaper rollingPaper) { return request.stickers().stream() .map(each -> new Sticker(rollingPaper, each.imageUrl(), each.x(), each.y(), diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperExchangeCountResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperExchangeCountResponse.java new file mode 100644 index 0000000..b71b589 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/RollingPaperExchangeCountResponse.java @@ -0,0 +1,7 @@ +package slvtwn.khu.toyouserver.dto; + +public record RollingPaperExchangeCountResponse(String userName, Long sentCount, Long receivedCount) { + public static RollingPaperExchangeCountResponse of(String userName, Long sentCount, Long receivedCount) { + return new RollingPaperExchangeCountResponse(userName, sentCount, receivedCount); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java index 71924da..642ca52 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/MemberRepository.java @@ -14,6 +14,8 @@ public interface MemberRepository extends JpaRepository { List findByUser(User user); + List findByUserId(Long userId); + @Query("select m from Member m join m.user u where m.group = :group and u.name like %:pattern%") List findByGroupAndUserNameLike(Group group, String pattern); diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java index 0dd6172..ef18613 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/RollingPaperRepository.java @@ -18,5 +18,9 @@ Slice findByMembersWithCursor(@Param("memberIds") List membe List findAllByMemberIn(List members); + Long countAllBySenderSnapshot_SenderId(Long senderId); + + List findAllBySenderSnapshot_SenderId(Long senderId); + void deleteAllByMemberIn(List members); } diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperFolderController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperFolderController.java new file mode 100644 index 0000000..e94cb63 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/RollingPaperFolderController.java @@ -0,0 +1,24 @@ +package slvtwn.khu.toyouserver.presentation; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import slvtwn.khu.toyouserver.application.RollingPaperCommandFacade; +import slvtwn.khu.toyouserver.common.authentication.UserAuthentication; +import slvtwn.khu.toyouserver.common.response.ToyouResponse; +import slvtwn.khu.toyouserver.dto.RollingPaperExchangeCountResponse; + +@RequestMapping("/rollingpapers/folders") +@RequiredArgsConstructor +@RestController +public class RollingPaperFolderController { + + private final RollingPaperCommandFacade rollingPaperCommandFacade; + + @GetMapping("/info") + public ToyouResponse findRollingPaperExchangeCount( + @UserAuthentication Long userId) { + return ToyouResponse.from(rollingPaperCommandFacade.getExchangeCount(userId)); + } +}