From 682017b37f2eb0022000a011653704a4c6c58cd2 Mon Sep 17 00:00:00 2001 From: Arachne <66822642+Arachneee@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:54:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=82=B4=20=ED=96=89=EC=82=AC=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?(#846)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/application/EventService.java | 11 ++++++++++- .../request/EventMineAppResponse.java | 16 ++++++++++++++++ .../event/domain/bill/MemberBillReport.java | 8 +++++--- .../event/domain/event/EventRepository.java | 3 +++ .../event/member/EventMemberRepository.java | 2 ++ .../event/presentation/EventController.java | 9 +++++++++ .../response/EventMineResponse.java | 16 ++++++++++++++++ .../response/EventsMineResponse.java | 17 +++++++++++++++++ .../eventmember/EventMemberBillReportTest.java | 10 ++++++---- 9 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 server/src/main/java/haengdong/event/application/request/EventMineAppResponse.java create mode 100644 server/src/main/java/haengdong/event/presentation/response/EventMineResponse.java create mode 100644 server/src/main/java/haengdong/event/presentation/response/EventsMineResponse.java diff --git a/server/src/main/java/haengdong/event/application/EventService.java b/server/src/main/java/haengdong/event/application/EventService.java index 7794e72c..6633f390 100644 --- a/server/src/main/java/haengdong/event/application/EventService.java +++ b/server/src/main/java/haengdong/event/application/EventService.java @@ -6,6 +6,7 @@ import haengdong.event.application.request.EventAppRequest; import haengdong.event.application.request.EventGuestAppRequest; import haengdong.event.application.request.EventLoginAppRequest; +import haengdong.event.application.request.EventMineAppResponse; import haengdong.event.application.request.EventUpdateAppRequest; import haengdong.event.application.response.EventAppResponse; import haengdong.event.application.response.EventDetailAppResponse; @@ -84,9 +85,10 @@ public EventAppResponse findByGuestPassword(EventLoginAppRequest request) { public List getMemberBillReports(String token) { Event event = getEvent(token); + List eventMembers = eventMemberRepository.findAllByEvent(event); List bills = billRepository.findAllByEvent(event); - MemberBillReport memberBillReport = MemberBillReport.createByBills(bills); + MemberBillReport memberBillReport = MemberBillReport.create(eventMembers, bills); return memberBillReport.getReports().entrySet().stream() .map(this::createMemberBillReportResponse) @@ -186,4 +188,11 @@ private EventImage getEventImage(Long imageId) { return eventImageRepository.findById(imageId) .orElseThrow(() -> new HaengdongException(HaengdongErrorCode.IMAGE_NOT_FOUND)); } + + public List findByUserId(Long userId) { + return eventRepository.findByUserId(userId).stream() + .map(event -> EventMineAppResponse.of( + event, !eventMemberRepository.existsByEventAndIsDeposited(event, false))) + .toList(); + } } diff --git a/server/src/main/java/haengdong/event/application/request/EventMineAppResponse.java b/server/src/main/java/haengdong/event/application/request/EventMineAppResponse.java new file mode 100644 index 00000000..b714b957 --- /dev/null +++ b/server/src/main/java/haengdong/event/application/request/EventMineAppResponse.java @@ -0,0 +1,16 @@ +package haengdong.event.application.request; + +import haengdong.event.domain.event.Event; +import java.time.LocalDateTime; +import java.time.ZoneId; + +public record EventMineAppResponse( + String eventId, + String eventName, + boolean isFinished, + LocalDateTime createdAt +) { + public static EventMineAppResponse of(Event event, boolean isFinished) { + return new EventMineAppResponse(event.getToken(), event.getName(), isFinished, LocalDateTime.ofInstant(event.getCreatedAt(), ZoneId.of("Asia/Seoul")) ); + } +} diff --git a/server/src/main/java/haengdong/event/domain/bill/MemberBillReport.java b/server/src/main/java/haengdong/event/domain/bill/MemberBillReport.java index bced7716..580595bf 100644 --- a/server/src/main/java/haengdong/event/domain/bill/MemberBillReport.java +++ b/server/src/main/java/haengdong/event/domain/bill/MemberBillReport.java @@ -2,10 +2,10 @@ import static java.util.stream.Collectors.toMap; +import haengdong.event.domain.event.member.EventMember; import java.util.List; import java.util.Map; import lombok.Getter; -import haengdong.event.domain.event.member.EventMember; @Getter public class MemberBillReport { @@ -16,13 +16,15 @@ private MemberBillReport(Map reports) { this.reports = reports; } - public static MemberBillReport createByBills(List bills) { + public static MemberBillReport create(List eventMembers, List bills) { Map reports = bills.stream() .flatMap(bill -> bill.getBillDetails().stream()) .collect(toMap( BillDetail::getEventMember, BillDetail::getPrice, - Long::sum + Long::sum, + () -> eventMembers.stream() + .collect(toMap(member -> member, member -> 0L)) )); return new MemberBillReport(reports); diff --git a/server/src/main/java/haengdong/event/domain/event/EventRepository.java b/server/src/main/java/haengdong/event/domain/event/EventRepository.java index 122fcb73..df5a8b19 100644 --- a/server/src/main/java/haengdong/event/domain/event/EventRepository.java +++ b/server/src/main/java/haengdong/event/domain/event/EventRepository.java @@ -1,5 +1,6 @@ package haengdong.event.domain.event; +import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,4 +11,6 @@ public interface EventRepository extends JpaRepository { Optional findByToken(String token); boolean existsByTokenAndUserId(String token, Long userId); + + List findByUserId(Long userId); } diff --git a/server/src/main/java/haengdong/event/domain/event/member/EventMemberRepository.java b/server/src/main/java/haengdong/event/domain/event/member/EventMemberRepository.java index a74e8ced..917708b1 100644 --- a/server/src/main/java/haengdong/event/domain/event/member/EventMemberRepository.java +++ b/server/src/main/java/haengdong/event/domain/event/member/EventMemberRepository.java @@ -7,4 +7,6 @@ public interface EventMemberRepository extends JpaRepository { List findAllByEvent(Event event); + + boolean existsByEventAndIsDeposited(Event event, boolean isDeposited); } diff --git a/server/src/main/java/haengdong/event/presentation/EventController.java b/server/src/main/java/haengdong/event/presentation/EventController.java index ac69a3eb..e64e131c 100644 --- a/server/src/main/java/haengdong/event/presentation/EventController.java +++ b/server/src/main/java/haengdong/event/presentation/EventController.java @@ -6,6 +6,7 @@ import haengdong.event.application.EventImageFacadeService; import haengdong.event.application.EventService; import haengdong.event.application.request.EventAppRequest; +import haengdong.event.application.request.EventMineAppResponse; import haengdong.event.application.response.EventAppResponse; import haengdong.event.application.response.EventImageUrlAppResponse; import haengdong.event.application.response.MemberBillReportAppResponse; @@ -15,6 +16,7 @@ import haengdong.event.presentation.response.EventDetailResponse; import haengdong.event.presentation.response.EventImagesResponse; import haengdong.event.presentation.response.EventResponse; +import haengdong.event.presentation.response.EventsMineResponse; import haengdong.event.presentation.response.MemberBillReportsResponse; import jakarta.validation.Valid; import java.util.List; @@ -48,6 +50,13 @@ public ResponseEntity findEvent(@PathVariable("eventId") St return ResponseEntity.ok(eventDetailResponse); } + @GetMapping("/api/events/mine") + public ResponseEntity findMyEvents(@Login Long userId) { + List responses = eventService.findByUserId(userId); + + return ResponseEntity.ok(EventsMineResponse.of(responses)); + } + @GetMapping("/api/events/{eventId}/reports") public ResponseEntity getMemberBillReports(@PathVariable("eventId") String token) { List memberBillReports = eventService.getMemberBillReports(token); diff --git a/server/src/main/java/haengdong/event/presentation/response/EventMineResponse.java b/server/src/main/java/haengdong/event/presentation/response/EventMineResponse.java new file mode 100644 index 00000000..c5af6cf2 --- /dev/null +++ b/server/src/main/java/haengdong/event/presentation/response/EventMineResponse.java @@ -0,0 +1,16 @@ +package haengdong.event.presentation.response; + +import haengdong.event.application.request.EventMineAppResponse; +import java.time.LocalDateTime; + +public record EventMineResponse( + String eventId, + String eventName, + boolean isFinished, + LocalDateTime createdAt +) { + + public static EventMineResponse of(EventMineAppResponse response) { + return new EventMineResponse(response.eventId(), response.eventName(), response.isFinished(), response.createdAt()); + } +} diff --git a/server/src/main/java/haengdong/event/presentation/response/EventsMineResponse.java b/server/src/main/java/haengdong/event/presentation/response/EventsMineResponse.java new file mode 100644 index 00000000..feca51f6 --- /dev/null +++ b/server/src/main/java/haengdong/event/presentation/response/EventsMineResponse.java @@ -0,0 +1,17 @@ +package haengdong.event.presentation.response; + +import haengdong.event.application.request.EventMineAppResponse; +import java.util.List; + +public record EventsMineResponse( + List events +) { + + public static EventsMineResponse of(List responses) { + List events = responses.stream() + .map(EventMineResponse::of) + .toList(); + + return new EventsMineResponse(events); + } +} diff --git a/server/src/test/java/haengdong/domain/eventmember/EventMemberBillReportTest.java b/server/src/test/java/haengdong/domain/eventmember/EventMemberBillReportTest.java index 3b056e60..7e79d334 100644 --- a/server/src/test/java/haengdong/domain/eventmember/EventMemberBillReportTest.java +++ b/server/src/test/java/haengdong/domain/eventmember/EventMemberBillReportTest.java @@ -16,19 +16,20 @@ class EventMemberBillReportTest { @DisplayName("지출 목록으로 참가자 정산 리포트를 생성한다.") @Test - void createByBills() { + void create() { Event event = Fixture.EVENT1; EventMember eventMember1 = new EventMember(1L, event, "소하", false); EventMember eventMember2 = new EventMember(2L, event, "감자", false); EventMember eventMember3 = new EventMember(3L, event, "쿠키", false); EventMember eventMember4 = new EventMember(4L, event, "고구마", false); + EventMember eventMember5 = new EventMember(5L, event, "조커", false); List eventMembers = List.of(eventMember1, eventMember2, eventMember3, eventMember4); List bills = List.of( Bill.create(event, "뽕족", 60_000L, eventMembers), Bill.create(event, "인생네컷", 20_000L, eventMembers) ); - - MemberBillReport memberBillReport = MemberBillReport.createByBills(bills); + List eventMembersReal = List.of(eventMember1, eventMember2, eventMember3, eventMember4, eventMember5); + MemberBillReport memberBillReport = MemberBillReport.create(eventMembersReal, bills); assertThat(memberBillReport.getReports()) .containsAllEntriesOf( @@ -36,7 +37,8 @@ void createByBills() { eventMember1, 20_000L, eventMember2, 20_000L, eventMember3, 20_000L, - eventMember4, 20_000L + eventMember4, 20_000L, + eventMember5, 0L ) ); }