Added delete endpoint for rail systems.
This commit is contained in:
parent
4546993f0f
commit
25e59cd92c
|
@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
|
||||||
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
|
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
|
||||||
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
|
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
|
||||||
import nl.andrewl.railsignalapi.service.RailSystemService;
|
import nl.andrewl.railsignalapi.service.RailSystemService;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -23,4 +24,10 @@ public class RailSystemsApiController {
|
||||||
public RailSystemResponse createRailSystem(@RequestBody RailSystemCreationPayload payload) {
|
public RailSystemResponse createRailSystem(@RequestBody RailSystemCreationPayload payload) {
|
||||||
return railSystemService.createRailSystem(payload);
|
return railSystemService.createRailSystem(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping(path = "/{rsId}")
|
||||||
|
public ResponseEntity<?> deleteRailSystem(@PathVariable long rsId) {
|
||||||
|
railSystemService.delete(rsId);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package nl.andrewl.railsignalapi.service;
|
package nl.andrewl.railsignalapi.service;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import nl.andrewl.railsignalapi.dao.BranchRepository;
|
||||||
import nl.andrewl.railsignalapi.dao.RailSystemRepository;
|
import nl.andrewl.railsignalapi.dao.RailSystemRepository;
|
||||||
|
import nl.andrewl.railsignalapi.dao.SignalRepository;
|
||||||
import nl.andrewl.railsignalapi.model.RailSystem;
|
import nl.andrewl.railsignalapi.model.RailSystem;
|
||||||
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
|
import nl.andrewl.railsignalapi.rest.dto.RailSystemCreationPayload;
|
||||||
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
|
import nl.andrewl.railsignalapi.rest.dto.RailSystemResponse;
|
||||||
|
@ -17,6 +19,8 @@ import java.util.List;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RailSystemService {
|
public class RailSystemService {
|
||||||
private final RailSystemRepository railSystemRepository;
|
private final RailSystemRepository railSystemRepository;
|
||||||
|
private final SignalRepository signalRepository;
|
||||||
|
private final BranchRepository branchRepository;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<RailSystemResponse> getRailSystems() {
|
public List<RailSystemResponse> getRailSystems() {
|
||||||
|
@ -31,4 +35,15 @@ public class RailSystemService {
|
||||||
RailSystem rs = new RailSystem(payload.name());
|
RailSystem rs = new RailSystem(payload.name());
|
||||||
return new RailSystemResponse(railSystemRepository.save(rs));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ const $ = jQuery;
|
||||||
let railSystemSelect;
|
let railSystemSelect;
|
||||||
let railMapCanvas;
|
let railMapCanvas;
|
||||||
let railSystem = null;
|
let railSystem = null;
|
||||||
|
let detailPanel = null;
|
||||||
|
|
||||||
let canvasTranslation = {x: 0, y: 0};
|
let canvasTranslation = {x: 0, y: 0};
|
||||||
let canvasDragOrigin = null;
|
let canvasDragOrigin = null;
|
||||||
|
@ -23,6 +24,14 @@ $(document).ready(() => {
|
||||||
railMapCanvas.mouseup(onCanvasMouseUp);
|
railMapCanvas.mouseup(onCanvasMouseUp);
|
||||||
railMapCanvas.mousemove(onCanvasMouseMove);
|
railMapCanvas.mousemove(onCanvasMouseMove);
|
||||||
|
|
||||||
|
$('#addRailSystemInput').on("input", () => {
|
||||||
|
$('#addRailSystemButton').prop("disabled", $('#addRailSystemInput').val() === "");
|
||||||
|
});
|
||||||
|
$('#addRailSystemButton').click(addRailSystem);
|
||||||
|
$('#removeRailSystemButton').click(deleteRailSystem);
|
||||||
|
|
||||||
|
detailPanel = $('#railMapDetailPanel');
|
||||||
|
|
||||||
$.get("/api/railSystems")
|
$.get("/api/railSystems")
|
||||||
.done(railSystems => {
|
.done(railSystems => {
|
||||||
railSystems.forEach(railSystem => {
|
railSystems.forEach(railSystem => {
|
||||||
|
@ -112,6 +121,7 @@ function getMousePoint(event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function railSystemChanged() {
|
function railSystemChanged() {
|
||||||
|
detailPanel.empty();
|
||||||
railSystem = {};
|
railSystem = {};
|
||||||
railSystem.id = railSystemSelect.val();
|
railSystem.id = railSystemSelect.val();
|
||||||
$.get("/api/railSystems/" + railSystem.id + "/signals")
|
$.get("/api/railSystems/" + railSystem.id + "/signals")
|
||||||
|
@ -138,10 +148,51 @@ function selectSignalById(id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSignalSelected(signal) {
|
function onSignalSelected(signal) {
|
||||||
const dp = $('#railMapDetailPanel');
|
detailPanel.empty();
|
||||||
dp.empty();
|
|
||||||
if (signal !== null) {
|
if (signal !== null) {
|
||||||
const tpl = Handlebars.compile($('#signalTemplate').html());
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-auto">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue