59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// FolderRepo provides data access for Folder.
|
|
type FolderRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewFolderRepo(db *gorm.DB) *FolderRepo {
|
|
return &FolderRepo{db: db}
|
|
}
|
|
|
|
func (r *FolderRepo) Create(ctx context.Context, folder *model.Folder) error {
|
|
return r.db.WithContext(ctx).Create(folder).Error
|
|
}
|
|
|
|
func (r *FolderRepo) GetByID(ctx context.Context, id uint) (*model.Folder, error) {
|
|
var folder model.Folder
|
|
if err := r.db.WithContext(ctx).First(&folder, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &folder, nil
|
|
}
|
|
|
|
func (r *FolderRepo) Update(ctx context.Context, folder *model.Folder) error {
|
|
return r.db.WithContext(ctx).Save(folder).Error
|
|
}
|
|
|
|
func (r *FolderRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.Folder{}, id).Error
|
|
}
|
|
|
|
func (r *FolderRepo) FindByPortalID(ctx context.Context, portalID uint, offset, limit int) ([]model.Folder, int64, error) {
|
|
var folders []model.Folder
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.Folder{}).Where("portal_id = ?", portalID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&folders).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return folders, count, nil
|
|
}
|
|
|
|
func (r *FolderRepo) FindByCategoryID(ctx context.Context, categoryID uint, offset, limit int) ([]model.Folder, int64, error) {
|
|
var folders []model.Folder
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.Folder{}).Where("category_id = ?", categoryID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&folders).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return folders, count, nil
|
|
} |