feat: initial building endpoints
This commit is contained in:
@@ -58,6 +58,7 @@ func (s *APIServer) registerRoutes(r *chi.Mux) {
|
|||||||
r.Route("/assets", s.setupAssetRoutes())
|
r.Route("/assets", s.setupAssetRoutes())
|
||||||
r.Route("/shelves", s.setupShelfRoutes())
|
r.Route("/shelves", s.setupShelfRoutes())
|
||||||
r.Route("/categories", s.setupCategoryRoutes())
|
r.Route("/categories", s.setupCategoryRoutes())
|
||||||
|
r.Route("/buildings", s.setupBuildingRoutes())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIServer) handleIndex(w http.ResponseWriter, r *http.Request) error {
|
func (s *APIServer) handleIndex(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
|||||||
75
internal/api/buildings.go
Normal file
75
internal/api/buildings.go
Normal 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,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -30,13 +29,11 @@ func (s *APIServer) ShelfCtx(next http.Handler) http.Handler {
|
|||||||
shelfIdStr := chi.URLParam(r, "shelfId")
|
shelfIdStr := chi.URLParam(r, "shelfId")
|
||||||
shelfId, err := strconv.ParseUint(shelfIdStr, 10, 64)
|
shelfId, err := strconv.ParseUint(shelfIdStr, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Test")
|
|
||||||
render.Render(w, r, errNotFound)
|
render.Render(w, r, errNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
shelf, err := s.db.GetShelfByID(shelfId)
|
shelf, err := s.db.GetShelfByID(shelfId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Test 2: %v\n", err)
|
|
||||||
render.Render(w, r, errNotFound)
|
render.Render(w, r, errNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,3 +177,29 @@ func (s *DataStore) DeleteCategoryByID(id uint64) (bool, error) {
|
|||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DataStore) GetBuildings(offset, limit uint64) ([]*types.Building, error) {
|
||||||
|
var buildings []*types.Building
|
||||||
|
s.db.Order("id asc").Offset(int(offset)).Limit(int(limit)).Find(&buildings)
|
||||||
|
if len(buildings) == 0 {
|
||||||
|
return nil, fmt.Errorf("no buildings found")
|
||||||
|
}
|
||||||
|
return buildings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DataStore) TotalBuildings() (int64, error) {
|
||||||
|
var count int64
|
||||||
|
if tx := s.db.Find(&types.Building{}).Count(&count); tx.Error != nil {
|
||||||
|
return 0, tx.Error
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DataStore) GetBuildingByID(id uint64) (*types.Building, error) {
|
||||||
|
var result types.Building
|
||||||
|
tx := s.db.Model(&types.Building{}).Where("id = ?", id).First(&result)
|
||||||
|
if tx.Error != nil {
|
||||||
|
return nil, fmt.Errorf("building %d not found", id)
|
||||||
|
}
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,3 +33,14 @@ func (c CreateBuildingRequest) Bind(r *http.Request) error { return nil }
|
|||||||
/*
|
/*
|
||||||
Responses
|
Responses
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
type BuildingResponse struct {
|
||||||
|
*Response
|
||||||
|
Building *Building `json:"building"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MultipleBuildingResponse struct {
|
||||||
|
*Response
|
||||||
|
Buildings []*Building `json:"buildings"`
|
||||||
|
Total int64 `json:"total"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type ShelfLocation struct {
|
|||||||
RoomNumber string `json:"room_number,omitempty"`
|
RoomNumber string `json:"room_number,omitempty"`
|
||||||
Description string `json:"description,omitempty"`
|
Description string `json:"description,omitempty"`
|
||||||
BuildingID uint64 `json:"-"`
|
BuildingID uint64 `json:"-"`
|
||||||
Building Building `json:"building",omitempty`
|
Building Building `json:"building,omitempty"`
|
||||||
Assets []Asset `json:"assets,omitempty"`
|
Assets []Asset `json:"assets,omitempty"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
|||||||
Reference in New Issue
Block a user