Files
timekeep-backend/src/main/java/de/pnreichmuth/timekeep_backend/entities/Station.java

57 lines
1.8 KiB
Java

package de.pnreichmuth.timekeep_backend.entities;
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.util.*;
@Entity
@Getter
@NoArgsConstructor
public class Station {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String name;
private String location;
private String passwordHash;
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name="team_passed_stations")
private Set<Team> passedTeams;
@ElementCollection
private Map<Team, LocalDate> passingTimes;
public Station(String name, String location){
this.name = name;
this.location = location;
this.passwordHash = null;
this.passedTeams = new HashSet<>(1);
this.passingTimes = new HashMap<>(1);
}
public void teamPassed(Team team){
if(!passedTeams.add(team)){
throw new TeamExistsException("Team %s was already seen at this Station: %s".formatted(team.getTeamName(), this.location), team);
}
passingTimes.put(team, LocalDate.now());
}
public void removePassedTeam(Team team){
if (!passedTeams.contains(team)) throw new TeamNotFoundException("Team %s was never seen at this station: %s".formatted(team.getTeamName(), this.location));
passedTeams.remove(team);
passingTimes.remove(team);
}
public String setPasswordHash(String passwordHash){
if(this.passwordHash != null) throw new IllegalStateException("Password has already been set");
this.passwordHash = passwordHash;
return this.passwordHash;
}
}