From a0efea3e0b534355407604fa60fb64a6162084bd Mon Sep 17 00:00:00 2001 From: "Ted M. Young" Date: Tue, 23 Jan 2024 15:32:35 -0800 Subject: [PATCH] Completed sorting feature (issue #193) --- .../in/web/member/EnsembleSortOrder.java | 22 ++++++++++++++ .../in/web/member/EnsembleSummaryView.java | 5 ++-- .../in/web/member/MemberController.java | 4 +-- .../web/member/EnsembleSummaryViewTest.java | 30 +++++++++++++++++-- 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSortOrder.java diff --git a/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSortOrder.java b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSortOrder.java new file mode 100644 index 00000000..dd1a9370 --- /dev/null +++ b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSortOrder.java @@ -0,0 +1,22 @@ +package com.jitterted.mobreg.adapter.in.web.member; + +import com.jitterted.mobreg.domain.Ensemble; + +import java.util.Comparator; + +import static java.util.Comparator.comparing; + +public enum EnsembleSortOrder { + ASCENDING_ORDER(comparing(Ensemble::startDateTime)), + DESCENDING_ORDER(comparing(Ensemble::startDateTime).reversed()); + + private final Comparator comparator; + + EnsembleSortOrder(Comparator comparator) { + this.comparator = comparator; + } + + public Comparator comparator() { + return comparator; + } +} \ No newline at end of file diff --git a/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryView.java b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryView.java index 49e8a5bc..6d82956f 100644 --- a/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryView.java +++ b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryView.java @@ -10,7 +10,6 @@ import java.time.ZonedDateTime; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.stream.Stream; @@ -27,9 +26,9 @@ public record EnsembleSummaryView(long id, SpectatorAction spectatorAction, ParticipantAction participantAction) { - public static List from(List ensembles, MemberId memberId, MemberService memberService) { + public static List from(List ensembles, MemberId memberId, MemberService memberService, EnsembleSortOrder sortOrder) { return ensembles.stream() - .sorted(Comparator.comparing(Ensemble::startDateTime).reversed()) + .sorted(sortOrder.comparator()) .map(ensemble -> toView(ensemble, memberId, memberService)) .toList(); } diff --git a/src/main/java/com/jitterted/mobreg/adapter/in/web/member/MemberController.java b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/MemberController.java index 13c7913b..32627ab9 100644 --- a/src/main/java/com/jitterted/mobreg/adapter/in/web/member/MemberController.java +++ b/src/main/java/com/jitterted/mobreg/adapter/in/web/member/MemberController.java @@ -59,11 +59,11 @@ public void addEnsemblesToModel(Model model, Member member) { MemberId memberId = member.getId(); List availableEnsembles = ensembleService.allAvailableForRegistration(ZonedDateTime.now()); - List availableEnsembleSummaryViews = EnsembleSummaryView.from(availableEnsembles, memberId, memberService); + List availableEnsembleSummaryViews = EnsembleSummaryView.from(availableEnsembles, memberId, memberService, EnsembleSortOrder.ASCENDING_ORDER); model.addAttribute("upcomingEnsembles", availableEnsembleSummaryViews); List pastEnsembles = ensembleService.allInThePastFor(memberId, ZonedDateTime.now()); - List pastEnsembleSummaryViews = EnsembleSummaryView.from(pastEnsembles, memberId, memberService); + List pastEnsembleSummaryViews = EnsembleSummaryView.from(pastEnsembles, memberId, memberService, EnsembleSortOrder.DESCENDING_ORDER); model.addAttribute("pastEnsembles", pastEnsembleSummaryViews); } diff --git a/src/test/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryViewTest.java b/src/test/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryViewTest.java index c1d3d56b..cbd09d85 100644 --- a/src/test/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryViewTest.java +++ b/src/test/java/com/jitterted/mobreg/adapter/in/web/member/EnsembleSummaryViewTest.java @@ -29,12 +29,14 @@ class EnsembleSummaryViewTest { private static final StubMemberService STUB_MEMBER_SERVICE = new StubMemberService(); private static final MemberId IRRELEVANT_MEMBER_ID = MemberId.of(42L); private static final int IRRELEVANT_ENSEMBLE_ID = 13; + private static final Comparator ASCENDING_ORDER = Comparator.comparing(Ensemble::startDateTime); + private static final Comparator DESCENDING_ORDER = Comparator.comparing(Ensemble::startDateTime).reversed(); @Nested class AllViews { @Test - void ensemblesSortedDescendingByDate() { + void pastEnsemblesAreSortedDescendingByDate() { Ensemble ensembleYesterday = new EnsembleBuilder() .scheduled(ZonedDateTime.now().minusDays(1)) .id(7).named("Yesterday").build(); @@ -48,12 +50,36 @@ void ensemblesSortedDescendingByDate() { MemberService memberService = new DefaultMemberService(new InMemoryMemberRepository()); MemberId memberId = MemberId.of(71L); - List viewsDescending = EnsembleSummaryView.from(ensembles, memberId, memberService); + List viewsDescending = EnsembleSummaryView.from(ensembles, memberId, memberService, EnsembleSortOrder.DESCENDING_ORDER); assertThat(viewsDescending) .extracting(EnsembleSummaryView::dateTime) + .as("Not sorted Descending") .isSortedAccordingTo(Comparator.reverseOrder()); } + + @Test + void upcomingEnsemblesAreSortedAscendingByDate() { + Ensemble ensembleTomorrow = new EnsembleBuilder() + .scheduled(ZonedDateTime.now().plusDays(1)) + .id(7).named("Tomorrow").build(); + Ensemble ensembleNextWeek = new EnsembleBuilder() + .scheduled(ZonedDateTime.now().plusWeeks(1)) + .id(3).named("Next Week").build(); + Ensemble ensembleNextMonth = new EnsembleBuilder() + .scheduled(ZonedDateTime.now().plusMonths(1)) + .id(5).named("Next Month").build(); + List ensembles = List.of(ensembleNextMonth, ensembleTomorrow, ensembleNextWeek); + MemberService memberService = new DefaultMemberService(new InMemoryMemberRepository()); + MemberId memberId = MemberId.of(71L); + + List viewsAscending = EnsembleSummaryView.from(ensembles, memberId, memberService, EnsembleSortOrder.ASCENDING_ORDER); + + assertThat(viewsAscending) + .extracting(EnsembleSummaryView::dateTime) + .as("Not sorted Ascending") + .isSortedAccordingTo(Comparator.naturalOrder()); + } } @Test