Skip to content

Commit

Permalink
Completed sorting feature (issue #193)
Browse files Browse the repository at this point in the history
  • Loading branch information
tedyoung committed Jan 23, 2024
1 parent 4934809 commit a0efea3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<Ensemble> comparator;

EnsembleSortOrder(Comparator<Ensemble> comparator) {
this.comparator = comparator;
}

public Comparator<Ensemble> comparator() {
return comparator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,9 +26,9 @@ public record EnsembleSummaryView(long id,
SpectatorAction spectatorAction,
ParticipantAction participantAction) {

public static List<EnsembleSummaryView> from(List<Ensemble> ensembles, MemberId memberId, MemberService memberService) {
public static List<EnsembleSummaryView> from(List<Ensemble> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void addEnsemblesToModel(Model model, Member member) {
MemberId memberId = member.getId();

List<Ensemble> availableEnsembles = ensembleService.allAvailableForRegistration(ZonedDateTime.now());
List<EnsembleSummaryView> availableEnsembleSummaryViews = EnsembleSummaryView.from(availableEnsembles, memberId, memberService);
List<EnsembleSummaryView> availableEnsembleSummaryViews = EnsembleSummaryView.from(availableEnsembles, memberId, memberService, EnsembleSortOrder.ASCENDING_ORDER);
model.addAttribute("upcomingEnsembles", availableEnsembleSummaryViews);

List<Ensemble> pastEnsembles = ensembleService.allInThePastFor(memberId, ZonedDateTime.now());
List<EnsembleSummaryView> pastEnsembleSummaryViews = EnsembleSummaryView.from(pastEnsembles, memberId, memberService);
List<EnsembleSummaryView> pastEnsembleSummaryViews = EnsembleSummaryView.from(pastEnsembles, memberId, memberService, EnsembleSortOrder.DESCENDING_ORDER);
model.addAttribute("pastEnsembles", pastEnsembleSummaryViews);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ensemble> ASCENDING_ORDER = Comparator.comparing(Ensemble::startDateTime);
private static final Comparator<Ensemble> 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();
Expand All @@ -48,12 +50,36 @@ void ensemblesSortedDescendingByDate() {
MemberService memberService = new DefaultMemberService(new InMemoryMemberRepository());
MemberId memberId = MemberId.of(71L);

List<EnsembleSummaryView> viewsDescending = EnsembleSummaryView.from(ensembles, memberId, memberService);
List<EnsembleSummaryView> 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<Ensemble> ensembles = List.of(ensembleNextMonth, ensembleTomorrow, ensembleNextWeek);
MemberService memberService = new DefaultMemberService(new InMemoryMemberRepository());
MemberId memberId = MemberId.of(71L);

List<EnsembleSummaryView> viewsAscending = EnsembleSummaryView.from(ensembles, memberId, memberService, EnsembleSortOrder.ASCENDING_ORDER);

assertThat(viewsAscending)
.extracting(EnsembleSummaryView::dateTime)
.as("Not sorted Ascending")
.isSortedAccordingTo(Comparator.naturalOrder());
}
}

@Test
Expand Down

0 comments on commit a0efea3

Please sign in to comment.