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