Initialize work order management system with database schema, API handlers, web client, and Docker configuration.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"workorders/internal/model"
|
||||
)
|
||||
|
||||
type ResourceHandler struct{ db *sqlx.DB }
|
||||
|
||||
func NewResourceHandler(db *sqlx.DB) *ResourceHandler { return &ResourceHandler{db: db} }
|
||||
|
||||
func (h *ResourceHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
woID, err := intParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
var rows []model.AssignedResource
|
||||
err = h.db.Select(&rows, `
|
||||
SELECT r.id, r.wo_id, r.resource_type, r.resource_id, r.quantity, r.notes, r.assigned_at,
|
||||
ISNULL(p.name, ISNULL(v.unit_number, ISNULL(e.name, m.name))) AS name
|
||||
FROM wo_resources r
|
||||
LEFT JOIN resource_people p ON r.resource_type='person' AND r.resource_id=p.id
|
||||
LEFT JOIN resource_vehicles v ON r.resource_type='vehicle' AND r.resource_id=v.id
|
||||
LEFT JOIN resource_equipment e ON r.resource_type='equipment' AND r.resource_id=e.id
|
||||
LEFT JOIN resource_materials m ON r.resource_type='material' AND r.resource_id=m.id
|
||||
WHERE r.wo_id=@p1`, woID)
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
respond(w, http.StatusOK, rows)
|
||||
}
|
||||
|
||||
func (h *ResourceHandler) Assign(w http.ResponseWriter, r *http.Request) {
|
||||
woID, err := intParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
ResourceType string `json:"resource_type"`
|
||||
ResourceID int `json:"resource_id"`
|
||||
Quantity *float64 `json:"quantity"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
if err := decode(r, &body); err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid body")
|
||||
return
|
||||
}
|
||||
var rid int
|
||||
err = h.db.QueryRow(`
|
||||
INSERT INTO wo_resources (wo_id,resource_type,resource_id,quantity,notes)
|
||||
OUTPUT INSERTED.id VALUES (@p1,@p2,@p3,@p4,@p5)`,
|
||||
woID, body.ResourceType, body.ResourceID, body.Quantity, body.Notes,
|
||||
).Scan(&rid)
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
respond(w, http.StatusCreated, map[string]any{"id": rid})
|
||||
}
|
||||
|
||||
func (h *ResourceHandler) Remove(w http.ResponseWriter, r *http.Request) {
|
||||
rid, err := intParam(r, "rid")
|
||||
if err != nil {
|
||||
respondError(w, http.StatusBadRequest, "invalid rid")
|
||||
return
|
||||
}
|
||||
h.db.Exec(`DELETE FROM wo_resources WHERE id=@p1`, rid)
|
||||
respond(w, http.StatusOK, map[string]any{"deleted": rid})
|
||||
}
|
||||
Reference in New Issue
Block a user