Skip to content

Commit

Permalink
feat: 내 행사 조회 기능 추가 (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee authored Dec 10, 2024
1 parent 6981dcc commit 682017b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,9 +85,10 @@ public EventAppResponse findByGuestPassword(EventLoginAppRequest request) {

public List<MemberBillReportAppResponse> getMemberBillReports(String token) {
Event event = getEvent(token);
List<EventMember> eventMembers = eventMemberRepository.findAllByEvent(event);
List<Bill> bills = billRepository.findAllByEvent(event);

MemberBillReport memberBillReport = MemberBillReport.createByBills(bills);
MemberBillReport memberBillReport = MemberBillReport.create(eventMembers, bills);

return memberBillReport.getReports().entrySet().stream()
.map(this::createMemberBillReportResponse)
Expand Down Expand Up @@ -186,4 +188,11 @@ private EventImage getEventImage(Long imageId) {
return eventImageRepository.findById(imageId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.IMAGE_NOT_FOUND));
}

public List<EventMineAppResponse> findByUserId(Long userId) {
return eventRepository.findByUserId(userId).stream()
.map(event -> EventMineAppResponse.of(
event, !eventMemberRepository.existsByEventAndIsDeposited(event, false)))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -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")) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,13 +16,15 @@ private MemberBillReport(Map<EventMember, Long> reports) {
this.reports = reports;
}

public static MemberBillReport createByBills(List<Bill> bills) {
public static MemberBillReport create(List<EventMember> eventMembers, List<Bill> bills) {
Map<EventMember, Long> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,4 +11,6 @@ public interface EventRepository extends JpaRepository<Event, Long> {
Optional<Event> findByToken(String token);

boolean existsByTokenAndUserId(String token, Long userId);

List<Event> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface EventMemberRepository extends JpaRepository<EventMember, Long> {

List<EventMember> findAllByEvent(Event event);

boolean existsByEventAndIsDeposited(Event event, boolean isDeposited);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -48,6 +50,13 @@ public ResponseEntity<EventDetailResponse> findEvent(@PathVariable("eventId") St
return ResponseEntity.ok(eventDetailResponse);
}

@GetMapping("/api/events/mine")
public ResponseEntity<EventsMineResponse> findMyEvents(@Login Long userId) {
List<EventMineAppResponse> responses = eventService.findByUserId(userId);

return ResponseEntity.ok(EventsMineResponse.of(responses));
}

@GetMapping("/api/events/{eventId}/reports")
public ResponseEntity<MemberBillReportsResponse> getMemberBillReports(@PathVariable("eventId") String token) {
List<MemberBillReportAppResponse> memberBillReports = eventService.getMemberBillReports(token);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package haengdong.event.presentation.response;

import haengdong.event.application.request.EventMineAppResponse;
import java.util.List;

public record EventsMineResponse(
List<EventMineResponse> events
) {

public static EventsMineResponse of(List<EventMineAppResponse> responses) {
List<EventMineResponse> events = responses.stream()
.map(EventMineResponse::of)
.toList();

return new EventsMineResponse(events);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ 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<EventMember> eventMembers = List.of(eventMember1, eventMember2, eventMember3, eventMember4);
List<Bill> bills = List.of(
Bill.create(event, "뽕족", 60_000L, eventMembers),
Bill.create(event, "인생네컷", 20_000L, eventMembers)
);

MemberBillReport memberBillReport = MemberBillReport.createByBills(bills);
List<EventMember> eventMembersReal = List.of(eventMember1, eventMember2, eventMember3, eventMember4, eventMember5);
MemberBillReport memberBillReport = MemberBillReport.create(eventMembersReal, bills);

assertThat(memberBillReport.getReports())
.containsAllEntriesOf(
Map.of(
eventMember1, 20_000L,
eventMember2, 20_000L,
eventMember3, 20_000L,
eventMember4, 20_000L
eventMember4, 20_000L,
eventMember5, 0L
)
);
}
Expand Down

0 comments on commit 682017b

Please sign in to comment.