feat: categories endpoints

This commit is contained in:
2024-01-20 21:59:06 -06:00
parent de2c08ad09
commit a8ee837dee
4 changed files with 172 additions and 1 deletions

View File

@@ -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
}