first commit
This commit is contained in:
107
app/blog/services/category_service.go
Normal file
107
app/blog/services/category_service.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gobeyhan/database"
|
||||
"gobeyhan/database/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CategoryService struct{}
|
||||
|
||||
func NewCategoryService() *CategoryService {
|
||||
return &CategoryService{}
|
||||
}
|
||||
|
||||
// GetAllCategories retrieves all categories, optionally filtering by active status
|
||||
func (s *CategoryService) GetAllCategories(activeOnly bool) ([]models.Category, error) {
|
||||
var categories []models.Category
|
||||
query := database.DB.Preload("Parent").Preload("Children")
|
||||
|
||||
if activeOnly {
|
||||
query = query.Where("is_active = ?", true)
|
||||
}
|
||||
|
||||
err := query.Order("`order` ASC, created_at DESC").Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// GetCategoryByID retrieves a category by ID with parent and children relationships
|
||||
func (s *CategoryService) GetCategoryByID(id uint64) (*models.Category, error) {
|
||||
var category models.Category
|
||||
err := database.DB.
|
||||
Preload("Parent").
|
||||
Preload("Children").
|
||||
First(&category, id).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &category, nil
|
||||
}
|
||||
|
||||
// GetCategoryBySlug retrieves a category by slug
|
||||
func (s *CategoryService) GetCategoryBySlug(slug string) (*models.Category, error) {
|
||||
var category models.Category
|
||||
err := database.DB.
|
||||
Preload("Parent").
|
||||
Preload("Children").
|
||||
Where("slug = ?", slug).
|
||||
First(&category).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &category, nil
|
||||
}
|
||||
|
||||
// CreateCategory creates a new category
|
||||
func (s *CategoryService) CreateCategory(category *models.Category) error {
|
||||
return database.DB.Create(category).Error
|
||||
}
|
||||
|
||||
// UpdateCategory updates an existing category
|
||||
func (s *CategoryService) UpdateCategory(id uint64, updates map[string]interface{}) error {
|
||||
result := database.DB.Model(&models.Category{}).Where("id = ?", id).Updates(updates)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteCategory deletes a category by ID
|
||||
func (s *CategoryService) DeleteCategory(id uint64) error {
|
||||
result := database.DB.Delete(&models.Category{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCategoriesByParent retrieves child categories of a parent
|
||||
func (s *CategoryService) GetCategoriesByParent(parentID uint64, activeOnly bool) ([]models.Category, error) {
|
||||
var categories []models.Category
|
||||
query := database.DB.Where("parent_id = ?", parentID)
|
||||
|
||||
if activeOnly {
|
||||
query = query.Where("is_active = ?", true)
|
||||
}
|
||||
|
||||
err := query.Order("`order` ASC, created_at DESC").Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
Reference in New Issue
Block a user