feat: 初始提交云印享付费打印服务后端
This commit is contained in:
108
cloudprint-backend/internal/repository/library_repository.go
Normal file
108
cloudprint-backend/internal/repository/library_repository.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LibraryRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewLibraryRepository(db *gorm.DB) *LibraryRepository {
|
||||
return &LibraryRepository{db: db}
|
||||
}
|
||||
|
||||
// Category operations
|
||||
func (r *LibraryRepository) CreateCategory(ctx context.Context, category *model.LibraryCategory) error {
|
||||
return r.db.WithContext(ctx).Create(category).Error
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetCategoryByID(ctx context.Context, id int32) (*model.LibraryCategory, error) {
|
||||
var category model.LibraryCategory
|
||||
err := r.db.WithContext(ctx).First(&category, id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &category, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetAllCategories(ctx context.Context) ([]*model.LibraryCategory, error) {
|
||||
var categories []*model.LibraryCategory
|
||||
err := r.db.WithContext(ctx).Order("sort_order ASC, id ASC").Find(&categories).Error
|
||||
return categories, err
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) UpdateCategory(ctx context.Context, category *model.LibraryCategory) error {
|
||||
return r.db.WithContext(ctx).Save(category).Error
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) DeleteCategory(ctx context.Context, id int32) error {
|
||||
return r.db.WithContext(ctx).Delete(&model.LibraryCategory{}, id).Error
|
||||
}
|
||||
|
||||
// LibraryFile operations
|
||||
func (r *LibraryRepository) CreateFile(ctx context.Context, file *model.LibraryFile) error {
|
||||
return r.db.WithContext(ctx).Create(file).Error
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetFileByID(ctx context.Context, id int32) (*model.LibraryFile, error) {
|
||||
var file model.LibraryFile
|
||||
err := r.db.WithContext(ctx).Preload("File").First(&file, id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &file, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetFilesByCategory(ctx context.Context, categoryID int32, page, pageSize int) ([]*model.LibraryFile, int64, error) {
|
||||
var files []*model.LibraryFile
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.LibraryFile{}).Where("category_id = ?", categoryID)
|
||||
err := query.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err = query.Preload("File").Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&files).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return files, total, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) DeleteFile(ctx context.Context, id int32) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// Get file to delete underlying file record
|
||||
var libFile model.LibraryFile
|
||||
if err := tx.First(&libFile, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Delete library file
|
||||
if err := tx.Delete(&libFile).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Delete underlying file
|
||||
if err := tx.Delete(&model.File{}, libFile.FileID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) IncrementDownloadCount(ctx context.Context, id int32) error {
|
||||
return r.db.WithContext(ctx).Model(&model.LibraryFile{}).Where("id = ?", id).
|
||||
Update("download_count", gorm.Expr("download_count + 1")).Error
|
||||
}
|
||||
Reference in New Issue
Block a user