feat: 初始提交云印享付费打印服务后端
This commit is contained in:
67
cloudprint-backend/internal/repository/file_repository.go
Normal file
67
cloudprint-backend/internal/repository/file_repository.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FileRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewFileRepository(db *gorm.DB) *FileRepository {
|
||||
return &FileRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *FileRepository) Create(ctx context.Context, file *model.File) error {
|
||||
return r.db.WithContext(ctx).Create(file).Error
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetByID(ctx context.Context, id int32) (*model.File, error) {
|
||||
var file model.File
|
||||
err := r.db.WithContext(ctx).First(&file, id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &file, nil
|
||||
}
|
||||
|
||||
func (r *FileRepository) Delete(ctx context.Context, id int32) error {
|
||||
return r.db.WithContext(ctx).Delete(&model.File{}, id).Error
|
||||
}
|
||||
|
||||
func (r *FileRepository) GetListByUploader(ctx context.Context, uploaderType string, uploaderID int32, page, pageSize int) ([]*model.File, int64, error) {
|
||||
var files []*model.File
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.File{})
|
||||
if uploaderType != "" {
|
||||
query = query.Where("uploader_type = ?", uploaderType)
|
||||
}
|
||||
if uploaderID > 0 {
|
||||
query = query.Where("uploader_id = ?", uploaderID)
|
||||
}
|
||||
|
||||
err := query.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err = query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&files).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return files, total, nil
|
||||
}
|
||||
|
||||
func (r *FileRepository) Update(ctx context.Context, file *model.File) error {
|
||||
return r.db.WithContext(ctx).Save(file).Error
|
||||
}
|
||||
Reference in New Issue
Block a user