feat: categories endpoints
This commit is contained in:
@@ -57,6 +57,7 @@ func (s *APIServer) registerRoutes(r *chi.Mux) {
|
||||
|
||||
r.Route("/assets", s.setupAssetRoutes())
|
||||
r.Route("/shelves", s.setupShelfRoutes())
|
||||
r.Route("/categories", s.setupCategoryRoutes())
|
||||
}
|
||||
|
||||
func (s *APIServer) handleIndex(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
118
internal/api/categories.go
Normal file
118
internal/api/categories.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.brettb.xyz/goinv/server/internal/types"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (s *APIServer) setupCategoryRoutes() func(chi.Router) {
|
||||
return func(r chi.Router) {
|
||||
r.Get("/", makeHandler(s.getCategories))
|
||||
r.Post("/", makeHandler(s.createCategory))
|
||||
|
||||
r.Route("/{categoryID}", func(r chi.Router) {
|
||||
r.Use(s.CategoryCtx)
|
||||
r.Get("/", makeHandler(s.getCategory))
|
||||
r.Delete("/", makeHandler(s.deleteCategory))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *APIServer) CategoryCtx(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
categoryIdStr := chi.URLParam(r, "categoryID")
|
||||
categoryId, err := strconv.ParseUint(categoryIdStr, 10, 64)
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
shelf, err := s.db.GetCategoryByID(categoryId)
|
||||
if err != nil {
|
||||
render.Render(w, r, errNotFound)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), "category", shelf)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getCategories(w http.ResponseWriter, r *http.Request) error {
|
||||
categories, err := s.db.GetCategories(0, 10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
total, err := s.db.TotalCategories()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.MultipleCategoryResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Categories: categories,
|
||||
Total: total,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) getCategory(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
category, ok := ctx.Value("category").(*types.Category)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.CategoryResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Category: category,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) deleteCategory(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
category, ok := ctx.Value("category").(*types.Category)
|
||||
if !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
if ok, _ := s.db.DeleteCategoryByID(category.ID); !ok {
|
||||
return render.Render(w, r, errUnprocessable)
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.CategoryResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Category: category,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *APIServer) createCategory(w http.ResponseWriter, r *http.Request) error {
|
||||
data := &types.CreateCategoryRequest{}
|
||||
if err := render.Bind(r, data); err != nil {
|
||||
return render.Render(w, r, errBadRequest(err))
|
||||
}
|
||||
|
||||
category := &types.Category{
|
||||
Name: data.Name,
|
||||
}
|
||||
|
||||
err := s.db.CreateCategory(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return render.Render(w, r, &types.CategoryResponse{
|
||||
Response: &types.Response{
|
||||
HTTPStatusCode: http.StatusOK,
|
||||
},
|
||||
Category: category,
|
||||
})
|
||||
}
|
||||
@@ -129,3 +129,37 @@ func (s *DataStore) DeleteShelfByID(id uint64) (bool, error) {
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) GetCategoryByID(id uint64) (*types.Category, error) {
|
||||
var result types.Category
|
||||
tx := s.db.Model(&types.Category{}).Where("id = ?", id).First(&result)
|
||||
if tx.Error != nil {
|
||||
return nil, fmt.Errorf("category %d not found", id)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) GetCategories(offset, limit int) ([]*types.Category, error) {
|
||||
var categories []*types.Category
|
||||
s.db.Offset(offset).Limit(limit).Find(&categories)
|
||||
if len(categories) == 0 {
|
||||
return nil, fmt.Errorf("no categories found")
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) TotalCategories() (int64, error) {
|
||||
var count int64
|
||||
if tx := s.db.Find(&types.Category{}).Count(&count); tx.Error != nil {
|
||||
return 0, tx.Error
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *DataStore) DeleteCategoryByID(id uint64) (bool, error) {
|
||||
tx := s.db.Delete(&types.Category{}, id)
|
||||
if tx.Error != nil {
|
||||
return false, fmt.Errorf("unable to delete: %s", tx.Error.Error())
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -23,6 +24,23 @@ type Category struct {
|
||||
Requests
|
||||
*/
|
||||
|
||||
type CreateCategoryRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (c CreateCategoryRequest) Bind(r *http.Request) error { return nil }
|
||||
|
||||
/*
|
||||
Responses
|
||||
*/
|
||||
|
||||
type CategoryResponse struct {
|
||||
*Response
|
||||
Category *Category `json:"category"`
|
||||
}
|
||||
|
||||
type MultipleCategoryResponse struct {
|
||||
*Response
|
||||
Categories []*Category `json:"categories"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user