feat(http): /api/projects/:pid/connection-requirements full CRUD
This commit is contained in:
115
internal/server/connection_requirements.go
Normal file
115
internal/server/connection_requirements.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mgit.msbls.de/m/mcables/internal/db"
|
||||
)
|
||||
|
||||
type connReqCreate struct {
|
||||
FromDeviceID int64 `json:"from_device_id"`
|
||||
ToDeviceID int64 `json:"to_device_id"`
|
||||
PreferredCableTypeID *int64 `json:"preferred_cable_type_id,omitempty"`
|
||||
MustConnect *bool `json:"must_connect,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
// connReqPatch uses RawMessage for preferred_cable_type_id so the wire
|
||||
// tri-state ({} / null / int) is preserved.
|
||||
type connReqPatch struct {
|
||||
PreferredCableTypeID json.RawMessage `json:"preferred_cable_type_id,omitempty"`
|
||||
MustConnect *bool `json:"must_connect,omitempty"`
|
||||
Notes *string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
func (h *handlers) listConnectionRequirements(w http.ResponseWriter, r *http.Request) {
|
||||
pid, ok := parseInt64Path(r, "pid")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
|
||||
return
|
||||
}
|
||||
rs, err := h.store.ListConnectionRequirements(pid)
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, rs)
|
||||
}
|
||||
|
||||
func (h *handlers) createConnectionRequirement(w http.ResponseWriter, r *http.Request) {
|
||||
pid, ok := parseInt64Path(r, "pid")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
|
||||
return
|
||||
}
|
||||
var body connReqCreate
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
cr, err := h.store.CreateConnectionRequirement(pid, db.ConnectionRequirementCreate{
|
||||
FromDeviceID: body.FromDeviceID,
|
||||
ToDeviceID: body.ToDeviceID,
|
||||
PreferredCableTypeID: body.PreferredCableTypeID,
|
||||
MustConnect: body.MustConnect,
|
||||
Notes: body.Notes,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, cr)
|
||||
}
|
||||
|
||||
func (h *handlers) patchConnectionRequirement(w http.ResponseWriter, r *http.Request) {
|
||||
pid, ok := parseInt64Path(r, "pid")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
|
||||
return
|
||||
}
|
||||
id, ok := parseInt64Path(r, "id")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "id must be a positive integer")
|
||||
return
|
||||
}
|
||||
var body connReqPatch
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
ctRef, err := parseFrameRef(body.PreferredCableTypeID)
|
||||
if err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), "preferred_cable_type_id must be an integer or null")
|
||||
return
|
||||
}
|
||||
cr, err := h.store.UpdateConnectionRequirement(pid, id, db.ConnectionRequirementUpdate{
|
||||
PreferredCableTypeID: ctRef,
|
||||
MustConnect: body.MustConnect,
|
||||
Notes: body.Notes,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, cr)
|
||||
}
|
||||
|
||||
func (h *handlers) deleteConnectionRequirement(w http.ResponseWriter, r *http.Request) {
|
||||
pid, ok := parseInt64Path(r, "pid")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
|
||||
return
|
||||
}
|
||||
id, ok := parseInt64Path(r, "id")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "id must be a positive integer")
|
||||
return
|
||||
}
|
||||
if err := h.store.DeleteConnectionRequirement(pid, id); err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -59,6 +59,12 @@ func New(store *db.Store, frontend fs.FS) http.Handler {
|
||||
mux.HandleFunc("PATCH /api/projects/{pid}/device-types/{id}", h.patchDeviceType)
|
||||
mux.HandleFunc("DELETE /api/projects/{pid}/device-types/{id}", h.deleteDeviceType)
|
||||
|
||||
// Connection requirements — the solver's per-project input.
|
||||
mux.HandleFunc("GET /api/projects/{pid}/connection-requirements", h.listConnectionRequirements)
|
||||
mux.HandleFunc("POST /api/projects/{pid}/connection-requirements", h.createConnectionRequirement)
|
||||
mux.HandleFunc("PATCH /api/projects/{pid}/connection-requirements/{id}", h.patchConnectionRequirement)
|
||||
mux.HandleFunc("DELETE /api/projects/{pid}/connection-requirements/{id}", h.deleteConnectionRequirement)
|
||||
|
||||
// Frontend (embedded). Serve "/" → index.html via http.FileServerFS.
|
||||
// Wrap in noCache so the browser revalidates with the ETag/Last-Modified
|
||||
// the file server already emits — without this, browsers cache aggressively
|
||||
|
||||
Reference in New Issue
Block a user