feat: initial building endpoints

This commit is contained in:
2024-02-12 15:06:14 -06:00
parent b2c8c2915f
commit 915149e97b
6 changed files with 114 additions and 4 deletions

View File

@@ -58,6 +58,7 @@ func (s *APIServer) registerRoutes(r *chi.Mux) {
r.Route("/assets", s.setupAssetRoutes())
r.Route("/shelves", s.setupShelfRoutes())
r.Route("/categories", s.setupCategoryRoutes())
r.Route("/buildings", s.setupBuildingRoutes())
}
func (s *APIServer) handleIndex(w http.ResponseWriter, r *http.Request) error {

75
internal/api/buildings.go Normal file
View File

@@ -0,0 +1,75 @@
package api
import (
"context"
"net/http"
"strconv"
"git.brettb.xyz/goinv/server/internal/types"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func (s *APIServer) setupBuildingRoutes() func(chi.Router) {
return func(r chi.Router) {
r.Get("/", makeHandler(s.getShelves))
r.Post("/", makeHandler(s.createShelf))
r.Route("/{buildingId}", func(r chi.Router) {
r.Use(s.BuildingCtx)
})
}
}
func (s *APIServer) BuildingCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buildingIdStr := chi.URLParam(r, "buildingId")
buildingId, err := strconv.ParseUint(buildingIdStr, 10, 64)
if err != nil {
render.Render(w, r, errNotFound)
return
}
building, err := s.db.GetBuildingByID(buildingId)
if err != nil {
render.Render(w, r, errNotFound)
return
}
ctx := context.WithValue(r.Context(), "building", building)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (s *APIServer) getBuildings(w http.ResponseWriter, r *http.Request) error {
buildings, err := s.db.GetBuildings(0, 10)
if err != nil {
return err
}
total, err := s.db.TotalBuildings()
if err != nil {
return err
}
return render.Render(w, r, &types.MultipleBuildingResponse{
Response: &types.Response{
HTTPStatusCode: http.StatusOK,
},
Buildings: buildings,
Total: total,
})
}
func (s *APIServer) getBuilding(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
building, ok := ctx.Value("building").(*types.Building)
if !ok {
return render.Render(w, r, errUnprocessable)
}
return render.Render(w, r, &types.BuildingResponse{
Response: &types.Response{
HTTPStatusCode: http.StatusOK,
},
Building: building,
})
}

View File

@@ -2,7 +2,6 @@ package api
import (
"context"
"log"
"net/http"
"strconv"
@@ -30,13 +29,11 @@ func (s *APIServer) ShelfCtx(next http.Handler) http.Handler {
shelfIdStr := chi.URLParam(r, "shelfId")
shelfId, err := strconv.ParseUint(shelfIdStr, 10, 64)
if err != nil {
log.Println("Test")
render.Render(w, r, errNotFound)
return
}
shelf, err := s.db.GetShelfByID(shelfId)
if err != nil {
log.Printf("Test 2: %v\n", err)
render.Render(w, r, errNotFound)
return
}