37 lines
826 B
Go
37 lines
826 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"workorders/internal/model"
|
|
)
|
|
|
|
func respond(w http.ResponseWriter, code int, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
json.NewEncoder(w).Encode(map[string]any{"data": data})
|
|
}
|
|
|
|
func respondError(w http.ResponseWriter, code int, msg string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
fmt.Fprintf(w, `{"error":%q}`, msg)
|
|
}
|
|
|
|
func intParam(r *http.Request, key string) (int, error) {
|
|
return strconv.Atoi(chi.URLParam(r, key))
|
|
}
|
|
|
|
func decode(r *http.Request, v any) error {
|
|
return json.NewDecoder(r.Body).Decode(v)
|
|
}
|
|
|
|
func userFromCtx(r *http.Request) model.UserClaims {
|
|
u, _ := r.Context().Value(model.CtxUserKey).(model.UserClaims)
|
|
return u
|
|
}
|