Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
125 lines
4.5 KiB
Go
125 lines
4.5 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.RelatedCategory").Preload("Folders").Preload("Articles").First(&category, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &category, nil
|
|
}
|
|
|
|
func (r *CategoryRepo) GetByPortalAndID(ctx context.Context, portalID, id uint) (*model.Category, error) {
|
|
var category model.Category
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Folders").Preload("Articles").
|
|
Where("portal_id = ?", portalID).
|
|
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)
|
|
query := db.Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Articles").Order("position ASC")
|
|
if limit > 0 {
|
|
query = query.Offset(offset).Limit(limit)
|
|
}
|
|
if err := query.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)
|
|
query := db.Preload("Parent").Preload("RelatedCategories.RelatedCategory").Preload("Articles").Order("position ASC")
|
|
if limit > 0 {
|
|
query = query.Offset(offset).Limit(limit)
|
|
}
|
|
if err := query.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
|
|
}
|
|
|
|
func (r *CategoryRepo) UpdatePositionsForPortal(ctx context.Context, portalID uint, positions map[uint]int) error {
|
|
for id, pos := range positions {
|
|
if err := r.db.WithContext(ctx).Model(&model.Category{}).Where("id = ? AND portal_id = ?", id, portalID).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).
|
|
Preload("Articles").
|
|
Where("slug = ? AND portal_id = ? AND locale = ?", slug, portalID, locale).
|
|
First(&category).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &category, nil
|
|
}
|