package handlers import ( "net/http" "time" "github.com/jmoiron/sqlx" "workorders/internal/model" ) type StepHandler struct{ db *sqlx.DB } func NewStepHandler(db *sqlx.DB) *StepHandler { return &StepHandler{db: db} } func (h *StepHandler) List(w http.ResponseWriter, r *http.Request) { woID, err := intParam(r, "id") if err != nil { respondError(w, http.StatusBadRequest, "invalid id") return } var steps []model.Step if err := h.db.Select(&steps, `SELECT * FROM wo_steps WHERE wo_id=@p1 ORDER BY step_order`, woID); err != nil { respondError(w, http.StatusInternalServerError, err.Error()) return } respond(w, http.StatusOK, steps) } func (h *StepHandler) Create(w http.ResponseWriter, r *http.Request) { woID, err := intParam(r, "id") if err != nil { respondError(w, http.StatusBadRequest, "invalid id") return } var body model.Step if err := decode(r, &body); err != nil { respondError(w, http.StatusBadRequest, "invalid body") return } var maxOrder int h.db.QueryRow(`SELECT ISNULL(MAX(step_order),0) FROM wo_steps WHERE wo_id=@p1`, woID).Scan(&maxOrder) if body.StepType == "" { body.StepType = "work_step" } var sid int err = h.db.QueryRow(` INSERT INTO wo_steps (wo_id,step_order,title,description,required,step_type,type_config) OUTPUT INSERTED.id VALUES (@p1,@p2,@p3,@p4,@p5,@p6,@p7)`, woID, maxOrder+1, body.Title, body.Description, body.Required, body.StepType, body.TypeConfig, ).Scan(&sid) if err != nil { respondError(w, http.StatusInternalServerError, err.Error()) return } var step model.Step h.db.Get(&step, `SELECT * FROM wo_steps WHERE id=@p1`, sid) respond(w, http.StatusCreated, step) } func (h *StepHandler) Update(w http.ResponseWriter, r *http.Request) { sid, err := intParam(r, "sid") if err != nil { respondError(w, http.StatusBadRequest, "invalid sid") return } var body model.Step if err := decode(r, &body); err != nil { respondError(w, http.StatusBadRequest, "invalid body") return } h.db.Exec(`UPDATE wo_steps SET title=@p1,description=@p2,required=@p3 WHERE id=@p4`, body.Title, body.Description, body.Required, sid) var step model.Step h.db.Get(&step, `SELECT * FROM wo_steps WHERE id=@p1`, sid) respond(w, http.StatusOK, step) } func (h *StepHandler) Complete(w http.ResponseWriter, r *http.Request) { sid, err := intParam(r, "sid") if err != nil { respondError(w, http.StatusBadRequest, "invalid sid") return } user := userFromCtx(r) var body struct { Notes string `json:"notes"` } decode(r, &body) now := time.Now().UTC() h.db.Exec(`UPDATE wo_steps SET completed=1,completed_by=@p1,completed_at=@p2,notes=@p3 WHERE id=@p4`, user.Email, now, body.Notes, sid) var step model.Step h.db.Get(&step, `SELECT * FROM wo_steps WHERE id=@p1`, sid) respond(w, http.StatusOK, step) } func (h *StepHandler) Uncomplete(w http.ResponseWriter, r *http.Request) { sid, err := intParam(r, "sid") if err != nil { respondError(w, http.StatusBadRequest, "invalid sid") return } h.db.Exec(`UPDATE wo_steps SET completed=0,completed_by=NULL,completed_at=NULL,notes='' WHERE id=@p1`, sid) var step model.Step h.db.Get(&step, `SELECT * FROM wo_steps WHERE id=@p1`, sid) respond(w, http.StatusOK, step) } func (h *StepHandler) Delete(w http.ResponseWriter, r *http.Request) { sid, err := intParam(r, "sid") if err != nil { respondError(w, http.StatusBadRequest, "invalid sid") return } h.db.Exec(`DELETE FROM wo_steps WHERE id=@p1`, sid) respond(w, http.StatusOK, map[string]any{"deleted": sid}) }