106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"carrotskin/internal/model"
|
||
|
|
"carrotskin/internal/repository"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 通用错误
|
||
|
|
var (
|
||
|
|
ErrProfileNotFound = errors.New("档案不存在")
|
||
|
|
ErrProfileNoPermission = errors.New("无权操作此档案")
|
||
|
|
ErrTextureNotFound = errors.New("材质不存在")
|
||
|
|
ErrTextureNoPermission = errors.New("无权操作此材质")
|
||
|
|
ErrUserNotFound = errors.New("用户不存在")
|
||
|
|
)
|
||
|
|
|
||
|
|
// NormalizePagination 规范化分页参数
|
||
|
|
func NormalizePagination(page, pageSize int) (int, int) {
|
||
|
|
if page < 1 {
|
||
|
|
page = 1
|
||
|
|
}
|
||
|
|
if pageSize < 1 {
|
||
|
|
pageSize = 20
|
||
|
|
}
|
||
|
|
if pageSize > 100 {
|
||
|
|
pageSize = 100
|
||
|
|
}
|
||
|
|
return page, pageSize
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetProfileWithPermissionCheck 获取档案并验证权限
|
||
|
|
// 返回档案,如果不存在或无权限则返回相应错误
|
||
|
|
func GetProfileWithPermissionCheck(uuid string, userID int64) (*model.Profile, error) {
|
||
|
|
profile, err := repository.FindProfileByUUID(uuid)
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
|
|
return nil, ErrProfileNotFound
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("查询档案失败: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if profile.UserID != userID {
|
||
|
|
return nil, ErrProfileNoPermission
|
||
|
|
}
|
||
|
|
|
||
|
|
return profile, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetTextureWithPermissionCheck 获取材质并验证权限
|
||
|
|
// 返回材质,如果不存在或无权限则返回相应错误
|
||
|
|
func GetTextureWithPermissionCheck(textureID, userID int64) (*model.Texture, error) {
|
||
|
|
texture, err := repository.FindTextureByID(textureID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if texture == nil {
|
||
|
|
return nil, ErrTextureNotFound
|
||
|
|
}
|
||
|
|
|
||
|
|
if texture.UploaderID != userID {
|
||
|
|
return nil, ErrTextureNoPermission
|
||
|
|
}
|
||
|
|
|
||
|
|
return texture, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// EnsureTextureExists 确保材质存在
|
||
|
|
func EnsureTextureExists(textureID int64) (*model.Texture, error) {
|
||
|
|
texture, err := repository.FindTextureByID(textureID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if texture == nil {
|
||
|
|
return nil, ErrTextureNotFound
|
||
|
|
}
|
||
|
|
if texture.Status == -1 {
|
||
|
|
return nil, errors.New("材质已删除")
|
||
|
|
}
|
||
|
|
return texture, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// EnsureUserExists 确保用户存在
|
||
|
|
func EnsureUserExists(userID int64) (*model.User, error) {
|
||
|
|
user, err := repository.FindUserByID(userID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if user == nil {
|
||
|
|
return nil, ErrUserNotFound
|
||
|
|
}
|
||
|
|
return user, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// WrapError 包装错误,添加上下文信息
|
||
|
|
func WrapError(err error, message string) error {
|
||
|
|
if err == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
return fmt.Errorf("%s: %w", message, err)
|
||
|
|
}
|
||
|
|
|