Files
gochat/internal/repository/category_repo.go
T
2026-06-04 15:44:48 +08:00

93 lines
3.4 KiB
Go

package repository
import (
"context"
"github.com/gochat/gochat/internal/model"
"gorm.io/gorm"
)
// CategoryRepo provides data access for Category.
type CategoryRepo struct {
db *gorm.DB
}
func NewCategoryRepo(db *gorm.DB) *CategoryRepo {
return &CategoryRepo{db: db}
}
func (r *CategoryRepo) Create(ctx context.Context, category *model.Category) error {
return r.db.WithContext(ctx).Create(category).Error
}
func (r *CategoryRepo) GetByID(ctx context.Context, id uint) (*model.Category, error) {
var category model.Category
if err := r.db.WithContext(ctx).Preload("Parent").Preload("RelatedCategories").Preload("Folders").First(&category, id).Error; err != nil {
return nil, err
}
return &category, nil
}
func (r *CategoryRepo) Update(ctx context.Context, category *model.Category) error {
return r.db.WithContext(ctx).Save(category).Error
}
func (r *CategoryRepo) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&model.Category{}, id).Error
}
// FindByPortalID returns categories for a portal with pagination, ordered by position.
func (r *CategoryRepo) FindByPortalID(ctx context.Context, portalID uint, offset, limit int) ([]model.Category, int64, error) {
var categories []model.Category
var count int64
db := r.db.WithContext(ctx).Model(&model.Category{}).Where("portal_id = ?", portalID)
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Order("position ASC").Find(&categories).Error; err != nil {
return nil, 0, err
}
return categories, count, nil
}
// FindByPortalIDAndLocale returns categories for a portal filtered by locale.
func (r *CategoryRepo) FindByPortalIDAndLocale(ctx context.Context, portalID uint, locale string, offset, limit int) ([]model.Category, int64, error) {
var categories []model.Category
var count int64
db := r.db.WithContext(ctx).Model(&model.Category{}).Where("portal_id = ? AND locale = ?", portalID, locale)
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Order("position ASC").Find(&categories).Error; err != nil {
return nil, 0, err
}
return categories, count, nil
}
// FindByPortalIDAndParentID returns sub-categories for a parent.
func (r *CategoryRepo) FindByPortalIDAndParentID(ctx context.Context, portalID uint, parentID uint, offset, limit int) ([]model.Category, int64, error) {
var categories []model.Category
var count int64
db := r.db.WithContext(ctx).Model(&model.Category{}).Where("portal_id = ? AND parent_id = ?", portalID, parentID)
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Order("position ASC").Find(&categories).Error; err != nil {
return nil, 0, err
}
return categories, count, nil
}
// UpdatePositions batch-updates category positions for ordering.
// Implements Category.update_positions from M9 spec.
func (r *CategoryRepo) UpdatePositions(ctx context.Context, positions map[uint]int) error {
for id, pos := range positions {
if err := r.db.WithContext(ctx).Model(&model.Category{}).Where("id = ?", id).Update("position", pos).Error; err != nil {
return err
}
}
return nil
}
// FindBySlugAndPortalIDAndLocale returns a category by slug, portal, and locale.
func (r *CategoryRepo) FindBySlugAndPortalIDAndLocale(ctx context.Context, slug string, portalID uint, locale string) (*model.Category, error) {
var category model.Category
if err := r.db.WithContext(ctx).Where("slug = ? AND portal_id = ? AND locale = ?", slug, portalID, locale).First(&category).Error; err != nil {
return nil, err
}
return &category, nil
}