refactor passedStations and members to be of type List instead of Map, as a key is not needed

This commit is contained in:
2025-12-18 15:38:37 +01:00
parent fc123a6e94
commit 4af2cbf7b7

View File

@@ -4,54 +4,55 @@ import de.pnreichmuth.timekeep_backend.entities.Racer;
import de.pnreichmuth.timekeep_backend.entities.Station; import de.pnreichmuth.timekeep_backend.entities.Station;
import de.pnreichmuth.timekeep_backend.entities.Team; import de.pnreichmuth.timekeep_backend.entities.Team;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Map; import java.util.List;
import java.util.Objects; import java.util.Objects;
public record TeamWSTO(String teamName, public record TeamWSTO(String teamName,
boolean firstSemesterTeam, boolean firstSemesterTeam,
Map<String, RacerWSTO> teamMembers, List<RacerWSTO> teamMembers,
Map<String, StationWSTO> passedStations) { List<StationWSTO> passedStations) {
public TeamWSTO{ public TeamWSTO{
Objects.requireNonNull(teamName, "Cannot identify Team without a name"); Objects.requireNonNull(teamName, "Cannot identify Team without a name");
teamMembers = Map.copyOf(teamMembers); teamMembers = List.copyOf(teamMembers);
passedStations = Map.copyOf(passedStations); passedStations = List.copyOf(passedStations);
} }
public static TeamWSTO of(Team team){ public static TeamWSTO of(Team team){
Map<String, RacerWSTO> tmpMemberMap = new HashMap<>(); List<RacerWSTO> tempMemberList = new ArrayList<>();
for(Map.Entry<String, Racer> member : team.getMembers().entrySet()){ team.getMembers().forEach(member -> {
tmpMemberMap.put(member.getKey(), RacerWSTO.of(member.getValue())); tempMemberList.add(RacerWSTO.of(member));
} });
List<StationWSTO> tempStationList = new ArrayList<>();
team.getPassedStations().forEach(passedStation -> {
tempStationList.add(StationWSTO.of(passedStation));
});
Map<String, StationWSTO> tmpStationMap = new HashMap<>();
for(Map.Entry<String,Station> station : team.getPassedStations().entrySet()){
tmpStationMap.put(station.getKey(), StationWSTO.of(station.getValue()));
}
return new TeamWSTO( return new TeamWSTO(
team.getTeamName(), team.getTeamName(),
team.isFirstSemesterTeam(), team.isFirstSemesterTeam(),
Map.copyOf(tmpMemberMap), List.copyOf(tempMemberList),
Map.copyOf(tmpStationMap) List.copyOf(tempStationList)
); );
} }
public static Team toEntity(TeamWSTO teamWSTO){ public static Team toEntity(TeamWSTO teamWSTO){
Map<String, Racer> tmpMemberMap = new HashMap<>(); List<Racer> tempMemberList = new ArrayList<>();
for(Map.Entry<String, RacerWSTO> member : teamWSTO.teamMembers().entrySet()){ teamWSTO.teamMembers().forEach(member -> {
tmpMemberMap.put(member.getKey(), RacerWSTO.toEntity(member.getValue())); tempMemberList.add(RacerWSTO.toEntity(member));
} });
Map<String, Station> tmpStationMap = new HashMap<>(); List<Station> tempStationList = new ArrayList<>();
for (Map.Entry<String, StationWSTO> station : teamWSTO.passedStations().entrySet()) { teamWSTO.passedStations().forEach(passedStation -> {
tmpStationMap.put(station.getKey(), StationWSTO.toEntity(station.getValue())); tempStationList.add(StationWSTO.toEntity(passedStation));
} });
return new Team( return new Team(
teamWSTO.teamName(), teamWSTO.teamName(),
teamWSTO.firstSemesterTeam(), teamWSTO.firstSemesterTeam(),
tmpMemberMap, tempMemberList,
tmpStationMap tempStationList
); );
} }
} }