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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user