mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
begin StationService implementation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user