Added api/classifications endpoint, requiring a new query
This commit is contained in:
1 parent
378ac59f41
commit
0e27d2b2f3
9 files changed
+186
-6
No files matched your search
+5
-2
@@ -20,7 +20,8 @@ type API struct {
|
||||
type Option func(*API)
|
||||
|
||||
// WithStore makes the API serve the opportunities in opportunityStore under
|
||||
// /opportunities. Without it there are no opportunity routes.
|
||||
// /opportunities, and their classifications under /classifications. Without
|
||||
// it there are neither.
|
||||
func WithStore(opportunityStore *store.Store) Option {
|
||||
return func(a *API) {
|
||||
a.store = opportunityStore
|
||||
@@ -46,10 +47,12 @@ func New(opts ...Option) *API {
|
||||
}
|
||||
|
||||
// Routes registers the API's routes on router (mounted under /api): its
|
||||
// description (see openapi.go), and the opportunities if it has a store.
|
||||
// description (see openapi.go), and the opportunities and classifications if
|
||||
// it has a store.
|
||||
func (a *API) Routes(router chi.Router) {
|
||||
docsRoutes(router)
|
||||
if a.store != nil {
|
||||
newOpportunityRoutes(a.store, a.events).Routes(router)
|
||||
newClassificationRoutes(a.store).Routes(router)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/types"
|
||||
)
|
||||
|
||||
// The classifications the opportunities have are listed once each, by code.
|
||||
func TestListClassifications(t *testing.T) {
|
||||
url := opportunitiesServer(t, 3)
|
||||
|
||||
var classifications []types.ClassificationCode
|
||||
status := request(t, "GET", url+"/classifications", &classifications)
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("status %d", status)
|
||||
}
|
||||
if len(classifications) != 2 ||
|
||||
classifications[0].Code != "03000000" ||
|
||||
classifications[1].Code != "72000000" ||
|
||||
classifications[0].ID == 0 {
|
||||
t.Errorf("got %+v; want 03000000 then 72000000, with ids", classifications)
|
||||
}
|
||||
}
|
||||
|
||||
// With no opportunities, the list is [] in JSON, not null.
|
||||
func TestListClassificationsEmpty(t *testing.T) {
|
||||
url := opportunitiesServer(t, 0)
|
||||
res, err := http.Get(url + "/classifications")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var raw json.RawMessage
|
||||
json.NewDecoder(res.Body).Decode(&raw)
|
||||
if string(raw) != "[]" {
|
||||
t.Errorf("body = %s; want []", raw)
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
// Package api implements the REST API mounted under /api. New assembles it
|
||||
// (api.go): the opportunities, read-only apart from their marks
|
||||
// (opportunities.go), and the API's own OpenAPI 3.1 description and docs
|
||||
// viewer (openapi.go). New resources are added in New and Routes, so the
|
||||
// server only mounts the API.
|
||||
// (opportunities.go), their classifications (classifications.go), and the
|
||||
// API's own OpenAPI 3.1 description and docs viewer (openapi.go). New
|
||||
// resources are added in New and Routes, so the server only mounts the API.
|
||||
//
|
||||
// The description is generated from the swag annotations on this package's
|
||||
// handlers by go generate (swag v2, see the README's dev tools), into
|
||||
|
||||
@@ -42,6 +42,15 @@ func opportunitiesServer(t *testing.T, count int, opts ...Option) string {
|
||||
UUIDType: "ocid",
|
||||
Title: fmt.Sprintf("Opportunity %d", i),
|
||||
Source: "FindATender",
|
||||
// Two codes, shared: ocds-1, ocds-3... have 72000000, the
|
||||
// others 03000000.
|
||||
Classifications: []types.ClassificationCode{
|
||||
{
|
||||
IsCPV: true,
|
||||
Code: []string{"03000000", "72000000"}[i%2],
|
||||
Classification: []string{"Agriculture", "IT services"}[i%2],
|
||||
},
|
||||
},
|
||||
Releases: []types.OpportunityRelease{
|
||||
{
|
||||
ReleaseID: "r1",
|
||||
|
||||
Reference in new issue
Block a user