begin StationService implementation

This commit is contained in:
2025-12-19 15:40:10 +01:00
parent 70e64b153a
commit 1d6a6b522c
2 changed files with 32 additions and 0 deletions

View File

@@ -48,4 +48,10 @@ public class Station {
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;
}
}

View File

@@ -1,16 +1,42 @@
package de.pnreichmuth.timekeep_backend.services;
import de.pnreichmuth.timekeep_backend.entities.Station;
import de.pnreichmuth.timekeep_backend.exceptions.StationExistsException;
import de.pnreichmuth.timekeep_backend.repositories.StationRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Service
@Slf4j
@RequiredArgsConstructor
public class StationService {
private final StationRepository stationRepository;
private void checkIfStationIsDuplicate(Station station){
List<Station> stations = stationRepository.findAll();
if(stations.isEmpty()) return;
if(
stations.stream().anyMatch(
dbStation -> dbStation.getName().equals(station.getName())
&& dbStation.getLocation().equals(station.getLocation())
&& !dbStation.getId().equals(station.getId())
)
) throw new StationExistsException(
"A station named %s already exists at the location %s".formatted(station.getName(), station.getLocation()),
station
);
}
public Optional<Station> createStation(String name, String location){
Station station = new Station(name,location);
checkIfStationIsDuplicate(station);
stationRepository.save(station);
return Optional.of(station);
}
}