feat(http): device-type endpoints + type_id on device create/patch
- GET /api/device-types — built-ins only (read-only). - GET /api/projects/:pid/device-types — built-ins + project-custom merged. - POST/PATCH/DELETE /api/projects/:pid/device-types — project-custom only. Mutating a built-in row returns 403 via the new ErrForbidden → 403 map in writeError. - devicePatch / deviceCreate JSON shapes accept type_id (tri-state for PATCH via the existing parseFrameRef helper applied to type_id too). - POST /api/projects/:pid/devices with type_id seeds ports in one tx server-side; response carries the device row + the snapshot will then carry the new ports.
This commit is contained in:
147
internal/server/device_types.go
Normal file
147
internal/server/device_types.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mgit.msbls.de/m/mcables/internal/db"
|
||||
)
|
||||
|
||||
type deviceTypePortBody struct {
|
||||
CableTypeID int64 `json:"cable_type_id"`
|
||||
LabelPrefix string `json:"label_prefix,omitempty"`
|
||||
Count int `json:"count"`
|
||||
Edge string `json:"edge,omitempty"`
|
||||
SortOrder int `json:"sort_order,omitempty"`
|
||||
}
|
||||
|
||||
type deviceTypeCreate struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Icon string `json:"icon,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Ports []deviceTypePortBody `json:"ports,omitempty"`
|
||||
}
|
||||
|
||||
type deviceTypePatch struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
Kind *string `json:"kind,omitempty"`
|
||||
Icon *string `json:"icon,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
Ports *[]deviceTypePortBody `json:"ports,omitempty"`
|
||||
}
|
||||
|
||||
func portsToStore(body []deviceTypePortBody) []db.DeviceTypePortCreate {
|
||||
out := make([]db.DeviceTypePortCreate, len(body))
|
||||
for i, p := range body {
|
||||
c := p.Count
|
||||
if c <= 0 {
|
||||
c = 1
|
||||
}
|
||||
out[i] = db.DeviceTypePortCreate{
|
||||
CableTypeID: p.CableTypeID,
|
||||
LabelPrefix: p.LabelPrefix,
|
||||
Count: c,
|
||||
Edge: p.Edge,
|
||||
SortOrder: p.SortOrder,
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// GET /api/device-types — built-in catalog only, read-only.
|
||||
func (h *handlers) listBuiltInDeviceTypes(w http.ResponseWriter, _ *http.Request) {
|
||||
dts, err := h.store.ListBuiltInDeviceTypes()
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, dts)
|
||||
}
|
||||
|
||||
// GET /api/projects/:pid/device-types — built-ins + project-custom merged.
|
||||
func (h *handlers) listDeviceTypes(w http.ResponseWriter, r *http.Request) {
|
||||
pid, ok := parseInt64Path(r, "pid")
|
||||
if !ok {
|
||||
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
|
||||
return
|
||||
}
|
||||
dts, err := h.store.ListDeviceTypesForProject(pid)
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, dts)
|
||||
}
|
||||
|
||||
func (h *handlers) createDeviceType(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 deviceTypeCreate
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
dt, err := h.store.CreateDeviceType(pid, db.DeviceTypeCreate{
|
||||
Name: body.Name, Kind: body.Kind, Icon: body.Icon,
|
||||
Description: body.Description, Ports: portsToStore(body.Ports),
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, dt)
|
||||
}
|
||||
|
||||
func (h *handlers) patchDeviceType(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 deviceTypePatch
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
|
||||
return
|
||||
}
|
||||
u := db.DeviceTypeUpdate{
|
||||
Name: body.Name, Kind: body.Kind, Icon: body.Icon, Description: body.Description,
|
||||
}
|
||||
if body.Ports != nil {
|
||||
converted := portsToStore(*body.Ports)
|
||||
u.Ports = &converted
|
||||
}
|
||||
dt, err := h.store.UpdateDeviceType(pid, id, u)
|
||||
if err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, dt)
|
||||
}
|
||||
|
||||
func (h *handlers) deleteDeviceType(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.DeleteDeviceType(pid, id); err != nil {
|
||||
writeError(w, err, nil)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -110,6 +110,7 @@ func (h *handlers) deleteFrame(w http.ResponseWriter, r *http.Request) {
|
||||
type deviceCreate struct {
|
||||
Name string `json:"name"`
|
||||
FrameID *int64 `json:"frame_id,omitempty"`
|
||||
TypeID *int64 `json:"type_id,omitempty"`
|
||||
Color string `json:"color,omitempty"`
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
@@ -117,13 +118,14 @@ type deviceCreate struct {
|
||||
Height float64 `json:"height"`
|
||||
}
|
||||
|
||||
// devicePatch uses a raw `json.RawMessage` for frame_id so we can tell
|
||||
// "key absent" (leave alone) from "key present and null" (set to NULL)
|
||||
// from "key present with an int" (move to that frame). Standard encoding
|
||||
// of nullable fields in JSON PATCH.
|
||||
// devicePatch uses a raw `json.RawMessage` for frame_id + type_id so we
|
||||
// can tell "key absent" (leave alone) from "key present and null"
|
||||
// (set to NULL) from "key present with an int" (move to that target).
|
||||
// Standard encoding of nullable fields in JSON PATCH.
|
||||
type devicePatch struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
FrameID json.RawMessage `json:"frame_id,omitempty"`
|
||||
TypeID json.RawMessage `json:"type_id,omitempty"`
|
||||
Color *string `json:"color,omitempty"`
|
||||
X *float64 `json:"x,omitempty"`
|
||||
Y *float64 `json:"y,omitempty"`
|
||||
@@ -173,7 +175,8 @@ func (h *handlers) createDevice(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
d, err := h.store.CreateDevice(pid, db.DeviceCreate{
|
||||
Name: body.Name, FrameID: body.FrameID, Color: body.Color,
|
||||
Name: body.Name, FrameID: body.FrameID, TypeID: body.TypeID,
|
||||
Color: body.Color,
|
||||
X: body.X, Y: body.Y, Width: body.Width, Height: body.Height,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -204,8 +207,13 @@ func (h *handlers) patchDevice(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), "frame_id must be an integer or null")
|
||||
return
|
||||
}
|
||||
typeRef, err := parseFrameRef(body.TypeID)
|
||||
if err != nil {
|
||||
writeError(w, errors.Join(db.ErrInvalidInput, err), "type_id must be an integer or null")
|
||||
return
|
||||
}
|
||||
d, err := h.store.UpdateDevice(pid, id, db.DeviceUpdate{
|
||||
Name: body.Name, FrameID: ref, Color: body.Color,
|
||||
Name: body.Name, FrameID: ref, TypeID: typeRef, Color: body.Color,
|
||||
X: body.X, Y: body.Y, Width: body.Width, Height: body.Height,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -42,6 +42,8 @@ func writeError(w http.ResponseWriter, err error, details any) {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody{Error: err.Error(), Details: details})
|
||||
case errors.Is(err, db.ErrInvalidInput):
|
||||
writeJSON(w, http.StatusBadRequest, errorBody{Error: err.Error(), Details: details})
|
||||
case errors.Is(err, db.ErrForbidden):
|
||||
writeJSON(w, http.StatusForbidden, errorBody{Error: err.Error(), Details: details})
|
||||
default:
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody{Error: err.Error(), Details: details})
|
||||
}
|
||||
|
||||
@@ -51,6 +51,14 @@ 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)
|
||||
|
||||
// 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)
|
||||
mux.HandleFunc("GET /api/projects/{pid}/device-types", h.listDeviceTypes)
|
||||
mux.HandleFunc("POST /api/projects/{pid}/device-types", h.createDeviceType)
|
||||
mux.HandleFunc("PATCH /api/projects/{pid}/device-types/{id}", h.patchDeviceType)
|
||||
mux.HandleFunc("DELETE /api/projects/{pid}/device-types/{id}", h.deleteDeviceType)
|
||||
|
||||
// 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