Added first working prototype.

This commit is contained in:
Andrew Lalis 2021-11-22 12:25:59 +01:00
parent 5c4b21749b
commit 108edac5e7
5 changed files with 37 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class RailSignalApiApplication {
public static void main(String[] args) {
System.setProperty("org.apache.tomcat.websocket.DISABLE_BUILTIN_EXTENSIONS", "true");
SpringApplication.run(RailSignalApiApplication.class, args);
}

View File

@ -13,10 +13,13 @@ import java.util.Optional;
@Repository
public interface SignalRepository extends JpaRepository<Signal, Long> {
Optional<Signal> findByIdAndRailSystem(long id, RailSystem railSystem);
Optional<Signal> findByIdAndRailSystemId(long id, long railSystemId);
boolean existsByNameAndRailSystem(String name, RailSystem railSystem);
@Query("SELECT DISTINCT s FROM Signal s " +
"LEFT JOIN s.branchConnections bc " +
"WHERE bc.branch = :branch")
List<Signal> findAllConnectedToBranch(Branch branch);
List<Signal> findAllByRailSystemOrderByName(RailSystem railSystem);
}

View File

@ -6,6 +6,8 @@ import nl.andrewl.railsignalapi.rest.dto.SignalResponse;
import nl.andrewl.railsignalapi.service.SignalService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "/api/railSystems/{rsId}/signals")
@RequiredArgsConstructor
@ -16,4 +18,14 @@ public class SignalsApiController {
public SignalResponse createSignal(@PathVariable long rsId, @RequestBody SignalCreationPayload payload) {
return signalService.createSignal(rsId, payload);
}
@GetMapping
public List<SignalResponse> getSignals(@PathVariable long rsId) {
return signalService.getAllSignals(rsId);
}
@GetMapping(path = "/{sigId}")
public SignalResponse getSignal(@PathVariable long rsId, @PathVariable long sigId) {
return signalService.getSignal(rsId, sigId);
}
}

View File

@ -3,6 +3,7 @@ package nl.andrewl.railsignalapi.rest.dto;
import nl.andrewl.railsignalapi.model.Signal;
import nl.andrewl.railsignalapi.model.SignalBranchConnection;
import java.util.Comparator;
import java.util.List;
public record SignalResponse(
@ -15,6 +16,7 @@ public record SignalResponse(
signal.getId(),
signal.getName(),
signal.getBranchConnections().stream()
.sorted(Comparator.comparing(SignalBranchConnection::getDirection))
.map(ConnectionData::new)
.toList()
);

View File

@ -22,10 +22,7 @@ import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Service
@ -93,12 +90,14 @@ public class SignalService {
log.warn("Warning! Train is entering a non-free branch {}.", toBranch.getName());
}
if (toBranch.getStatus() != BranchStatus.OCCUPIED) {
log.info("Updating branch {} status from {} to {}.", toBranch.getName(), toBranch.getStatus(), BranchStatus.OCCUPIED);
toBranch.setStatus(BranchStatus.OCCUPIED);
branchRepository.save(toBranch);
broadcastToConnectedSignals(toBranch);
}
if (updateType == SignalUpdateType.END) {
if (fromBranch.getStatus() != BranchStatus.FREE) {
log.info("Updating branch {} status from {} to {}.", fromBranch.getName(), fromBranch.getStatus(), BranchStatus.FREE);
fromBranch.setStatus(BranchStatus.FREE);
branchRepository.save(fromBranch);
broadcastToConnectedSignals(fromBranch);
@ -125,4 +124,20 @@ public class SignalService {
}
}
@Transactional(readOnly = true)
public SignalResponse getSignal(long rsId, long sigId) {
var s = signalRepository.findByIdAndRailSystemId(sigId, rsId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
return new SignalResponse(s);
}
@Transactional(readOnly = true)
public List<SignalResponse> getAllSignals(long rsId) {
var rs = railSystemRepository.findById(rsId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Rail system not found."));
return signalRepository.findAllByRailSystemOrderByName(rs).stream()
.map(SignalResponse::new)
.toList();
}
}