feat(backend): ports CRUD endpoints for slice 7
New store methods on internal/db/ports.go:
- CreatePort / GetPort / UpdatePort / DeletePort (all project-scoped)
- ListPortsForDevice for the inspector's per-device list
New handlers (internal/server/ports.go):
- GET /api/projects/:pid/devices/:id/ports
- POST /api/projects/:pid/devices/:id/ports ← {type_id, label?, x_offset, y_offset}
- PATCH /api/projects/:pid/ports/:id ← partial
- DELETE /api/projects/:pid/ports/:id (cables ref → ON DELETE SET NULL)
Lets slice 7's +Port tool add/remove instance ports without going
through the type-seeded auto-creation path from slice 4.
This commit is contained in:
114
internal/server/ports.go
Normal file
114
internal/server/ports.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mgit.msbls.de/m/mcables/internal/db"
|
||||
)
|
||||
|
||||
type portCreate struct {
|
||||
TypeID int64 `json:"type_id"`
|
||||
Label string `json:"label,omitempty"`
|
||||
XOffset float64 `json:"x_offset"`
|
||||
YOffset float64 `json:"y_offset"`
|
||||
}
|
||||
|
||||
type portPatch struct {
|
||||
TypeID *int64 `json:"type_id,omitempty"`
|
||||
Label *string `json:"label,omitempty"`
|
||||
XOffset *float64 `json:"x_offset,omitempty"`
|
||||
YOffset *float64 `json:"y_offset,omitempty"`
|
||||
}
|
||||
|
||||
func (h *handlers) listPortsForDevice(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
|
||||
}
|
||||
ps, err := h.store.ListPortsForDevice(pid, id)
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, ps)
|
||||
}
|
||||
|
||||
func (h *handlers) createPort(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 portCreate
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
p, err := h.store.CreatePort(pid, id, db.PortCreate{
|
||||
TypeID: body.TypeID, Label: body.Label,
|
||||
XOffset: body.XOffset, YOffset: body.YOffset,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, p)
|
||||
}
|
||||
|
||||
func (h *handlers) patchPort(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 portPatch
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
p, err := h.store.UpdatePort(pid, id, db.PortUpdate{
|
||||
TypeID: body.TypeID, Label: body.Label,
|
||||
XOffset: body.XOffset, YOffset: body.YOffset,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, p)
|
||||
}
|
||||
|
||||
func (h *handlers) deletePort(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.DeletePort(pid, id); err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -51,6 +51,12 @@ func New(store *db.Store, frontend fs.FS) http.Handler {
|
||||
mux.HandleFunc("PATCH /api/projects/{pid}/io-markers/{id}", h.patchIOMarker)
|
||||
mux.HandleFunc("DELETE /api/projects/{pid}/io-markers/{id}", h.deleteIOMarker)
|
||||
|
||||
// Ports — slice 7 lets m add/edit/remove instance ports on a device.
|
||||
mux.HandleFunc("GET /api/projects/{pid}/devices/{id}/ports", h.listPortsForDevice)
|
||||
mux.HandleFunc("POST /api/projects/{pid}/devices/{id}/ports", h.createPort)
|
||||
mux.HandleFunc("PATCH /api/projects/{pid}/ports/{id}", h.patchPort)
|
||||
mux.HandleFunc("DELETE /api/projects/{pid}/ports/{id}", h.deletePort)
|
||||
|
||||
// Device-type catalog. Built-ins are read-only; project-custom rows
|
||||
// support full CRUD scoped to the project.
|
||||
mux.HandleFunc("GET /api/device-types", h.listBuiltInDeviceTypes)
|
||||
|
||||
Reference in New Issue
Block a user