Files

55 lines
1.5 KiB
Go

package api
import (
"net/http"
"github.com/go-chi/chi/v5"
"git.avsdev.uk/Go/job-search/internal/httpx"
"git.avsdev.uk/Go/job-search/internal/store"
)
// classificationRoutes wires HTTP handlers to the classification codes in
// the opportunity store. They're read-only.
type classificationRoutes struct {
store *store.Store
}
// newClassificationRoutes returns the classification handlers for
// opportunityStore.
func newClassificationRoutes(
opportunityStore *store.Store,
) *classificationRoutes {
return &classificationRoutes{
store: opportunityStore,
}
}
// Routes registers the classification routes on router (mounted under
// /api).
func (h *classificationRoutes) Routes(router chi.Router) {
router.Get("/classifications", h.list)
}
// list serves GET /classifications: every classification code that at least
// one opportunity has, ordered by code.
//
// @Summary List classifications
// @Description Every classification code (CPV and others) that at least one
// @Description stored opportunity has, once each, ordered by code: the
// @Description codes worth filtering opportunities by.
// @Tags classifications
// @Produce json
// @Success 200 {array} types.ClassificationCode
// @Failure 500 {object} httpx.Problem
// @Router /classifications [get]
func (h *classificationRoutes) list(w http.ResponseWriter, r *http.Request) {
classifications, err := h.store.ListClassifications(r.Context())
if err != nil {
httpx.WriteProblem(w, r, http.StatusInternalServerError, "")
return
}
httpx.WriteJSON(w, http.StatusOK, classifications)
}