package handler import ( "encoding/json" "errors" "net/http" "strconv" "git.g3e.fr/H6N/account/internal/store" "github.com/jackc/pgx/v5" ) type InstrumentHandler struct { store *store.Store } func NewInstrumentHandler(s *store.Store) *InstrumentHandler { return &InstrumentHandler{store: s} } func (h *InstrumentHandler) RegisterRoutes(mux *http.ServeMux) { mux.HandleFunc("GET /instruments", h.list) mux.HandleFunc("POST /instruments", h.create) mux.HandleFunc("GET /instruments/{id}", h.get) mux.HandleFunc("PUT /instruments/{id}", h.update) mux.HandleFunc("DELETE /instruments/{id}", h.delete) } func (h *InstrumentHandler) list(w http.ResponseWriter, r *http.Request) { instruments, err := h.store.ListInstruments(r.Context()) if err != nil { writeError(w, http.StatusInternalServerError, "failed to list instruments") return } writeJSON(w, http.StatusOK, instruments) } func (h *InstrumentHandler) get(w http.ResponseWriter, r *http.Request) { id, err := parseID(r) if err != nil { writeError(w, http.StatusBadRequest, "invalid id") return } instrument, err := h.store.GetInstrument(r.Context(), id) if errors.Is(err, pgx.ErrNoRows) { writeError(w, http.StatusNotFound, "instrument not found") return } if err != nil { writeError(w, http.StatusInternalServerError, "failed to get instrument") return } writeJSON(w, http.StatusOK, instrument) } func (h *InstrumentHandler) create(w http.ResponseWriter, r *http.Request) { var p store.CreateInstrumentParams if err := json.NewDecoder(r.Body).Decode(&p); err != nil { writeError(w, http.StatusBadRequest, "invalid request body") return } if p.Type == "" || p.Code == "" || p.Name == "" { writeError(w, http.StatusBadRequest, "type, code and name are required") return } if p.DeviseCotation == "" { p.DeviseCotation = "EUR" } instrument, err := h.store.CreateInstrument(r.Context(), p) if err != nil { writeError(w, http.StatusInternalServerError, "failed to create instrument") return } writeJSON(w, http.StatusCreated, instrument) } func (h *InstrumentHandler) update(w http.ResponseWriter, r *http.Request) { id, err := parseID(r) if err != nil { writeError(w, http.StatusBadRequest, "invalid id") return } var p store.CreateInstrumentParams if err := json.NewDecoder(r.Body).Decode(&p); err != nil { writeError(w, http.StatusBadRequest, "invalid request body") return } instrument, err := h.store.UpdateInstrument(r.Context(), id, p) if errors.Is(err, pgx.ErrNoRows) { writeError(w, http.StatusNotFound, "instrument not found") return } if err != nil { writeError(w, http.StatusInternalServerError, "failed to update instrument") return } writeJSON(w, http.StatusOK, instrument) } func (h *InstrumentHandler) delete(w http.ResponseWriter, r *http.Request) { id, err := parseID(r) if err != nil { writeError(w, http.StatusBadRequest, "invalid id") return } if err := h.store.DeleteInstrument(r.Context(), id); err != nil { writeError(w, http.StatusInternalServerError, "failed to delete instrument") return } w.WriteHeader(http.StatusNoContent) } func parseID(r *http.Request) (int32, error) { v, err := strconv.Atoi(r.PathValue("id")) return int32(v), err }