Added delete endpoint for rail systems.

This commit is contained in:
Andrew Lalis 2021-11-24 12:33:58 +01:00
parent 4546993f0f
commit 25e59cd92c
4 changed files with 77 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
import nl.andrewl.railsignalapi.service.RailSystemService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -23,4 +24,10 @@ public class RailSystemsApiController {
public RailSystemResponse createRailSystem(@RequestBody RailSystemCreationPayload payload) {
return railSystemService.createRailSystem(payload);
}
@DeleteMapping(path = "/{rsId}")
public ResponseEntity<?> deleteRailSystem(@PathVariable long rsId) {
railSystemService.delete(rsId);
return ResponseEntity.noContent().build();
}
}

View File

@ -1,7 +1,9 @@
package nl.andrewl.railsignalapi.service;
import lombok.RequiredArgsConstructor;
import nl.andrewl.railsignalapi.dao.BranchRepository;
import nl.andrewl.railsignalapi.dao.RailSystemRepository;
import nl.andrewl.railsignalapi.dao.SignalRepository;
import nl.andrewl.railsignalapi.model.RailSystem;
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
@ -17,6 +19,8 @@ import java.util.List;
@RequiredArgsConstructor
public class RailSystemService {
private final RailSystemRepository railSystemRepository;
private final SignalRepository signalRepository;
private final BranchRepository branchRepository;
@Transactional
public List<RailSystemResponse> getRailSystems() {
@ -31,4 +35,15 @@ public class RailSystemService {
RailSystem rs = new RailSystem(payload.name());
return new RailSystemResponse(railSystemRepository.save(rs));
}
@Transactional
public void delete(long rsId) {
var rs = railSystemRepository.findById(rsId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
var signals = signalRepository.findAllByRailSystemOrderByName(rs);
signalRepository.deleteAll(signals);
var branches = branchRepository.findAllByRailSystemOrderByName(rs);
branchRepository.deleteAll(branches);
railSystemRepository.delete(rs);
}
}

View File

@ -3,6 +3,7 @@ const $ = jQuery;
let railSystemSelect;
let railMapCanvas;
let railSystem = null;
let detailPanel = null;
let canvasTranslation = {x: 0, y: 0};
let canvasDragOrigin = null;
@ -23,6 +24,14 @@ $(document).ready(() => {
railMapCanvas.mouseup(onCanvasMouseUp);
railMapCanvas.mousemove(onCanvasMouseMove);
$('#addRailSystemInput').on("input", () => {
$('#addRailSystemButton').prop("disabled", $('#addRailSystemInput').val() === "");
});
$('#addRailSystemButton').click(addRailSystem);
$('#removeRailSystemButton').click(deleteRailSystem);
detailPanel = $('#railMapDetailPanel');
$.get("/api/railSystems")
.done(railSystems => {
railSystems.forEach(railSystem => {
@ -112,6 +121,7 @@ function getMousePoint(event) {
}
function railSystemChanged() {
detailPanel.empty();
railSystem = {};
railSystem.id = railSystemSelect.val();
$.get("/api/railSystems/" + railSystem.id + "/signals")
@ -138,10 +148,51 @@ function selectSignalById(id) {
}
function onSignalSelected(signal) {
const dp = $('#railMapDetailPanel');
dp.empty();
detailPanel.empty();
if (signal !== null) {
const tpl = Handlebars.compile($('#signalTemplate').html());
dp.html(tpl(signal));
detailPanel.html(tpl(signal));
}
}
function addRailSystem() {
let name = $('#addRailSystemInput').val().trim();
$.post({
url: "/api/railSystems",
data: JSON.stringify({name: name}),
contentType: "application/json"
})
.done((response) => {
refreshRailSystems();
})
.always(() => {
$('#addRailSystemInput').val("");
});
}
function deleteRailSystem() {
if (railSystem !== null && railSystem.id) {
$.ajax({
url: "/api/railSystems/" + railSystem.id,
type: "DELETE"
})
.always(() => {
refreshRailSystems(true);
});
}
}
function refreshRailSystems(selectFirst) {
$.get("/api/railSystems")
.done(railSystems => {
railSystemSelect.empty();
railSystems.forEach(railSystem => {
let option = $('<option value="' + railSystem.id + '">' + railSystem.name + '</option>')
railSystemSelect.append(option);
});
if (selectFirst) {
railSystemSelect.val(railSystems[0].id);
railSystemSelect.change();
}
});
}

View File

@ -30,7 +30,7 @@
</div>
</div>
<div class="col-auto">
<button class="btn btn-danger" type="button" id="removeRailSystemButton" disabled>Remove Selected System</button>
<button class="btn btn-danger" type="button" id="removeRailSystemButton">Remove Selected System</button>
</div>
</div>