109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
|
|
"filestoragesystem/internal/model"
|
|
)
|
|
|
|
// UserRepo 用户数据访问
|
|
type UserRepo struct{ DB *gorm.DB }
|
|
|
|
func (r *UserRepo) Create(u *model.User) error { return r.DB.Create(u).Error }
|
|
|
|
func (r *UserRepo) FindByID(id uint) (*model.User, error) {
|
|
var u model.User
|
|
err := r.DB.Preload("Role.Permissions").First(&u, id).Error
|
|
return &u, err
|
|
}
|
|
|
|
func (r *UserRepo) FindByUsernameOrEmail(account string) (*model.User, error) {
|
|
var u model.User
|
|
err := r.DB.Preload("Role.Permissions").
|
|
Where("username = ? OR email = ?", account, account).First(&u).Error
|
|
return &u, err
|
|
}
|
|
|
|
func (r *UserRepo) FindByUsername(username string) (*model.User, error) {
|
|
var u model.User
|
|
err := r.DB.Where("username = ?", username).First(&u).Error
|
|
return &u, err
|
|
}
|
|
|
|
func (r *UserRepo) FindByEmail(email string) (*model.User, error) {
|
|
var u model.User
|
|
err := r.DB.Where("email = ?", email).First(&u).Error
|
|
return &u, err
|
|
}
|
|
|
|
// List 分页查询用户,keyword匹配用户名/邮箱,roleID/status可选
|
|
func (r *UserRepo) List(keyword, roleID, status string, page, pageSize int) ([]model.User, int64, error) {
|
|
q := r.DB.Model(&model.User{})
|
|
if keyword != "" {
|
|
like := "%" + keyword + "%"
|
|
q = q.Where("username LIKE ? OR email LIKE ?", like, like)
|
|
}
|
|
if roleID != "" {
|
|
q = q.Where("role_id = ?", roleID)
|
|
}
|
|
if status != "" {
|
|
q = q.Where("status = ?", status)
|
|
}
|
|
var total int64
|
|
if err := q.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var users []model.User
|
|
err := q.Preload("Role").Order("id DESC").
|
|
Offset((page - 1) * pageSize).Limit(pageSize).Find(&users).Error
|
|
return users, total, err
|
|
}
|
|
|
|
func (r *UserRepo) Update(u *model.User) error { return r.DB.Save(u).Error }
|
|
|
|
func (r *UserRepo) UpdateFields(id uint, fields map[string]interface{}) error {
|
|
return r.DB.Model(&model.User{}).Where("id = ?", id).Updates(fields).Error
|
|
}
|
|
|
|
func (r *UserRepo) Delete(id uint) error { return r.DB.Delete(&model.User{}, id).Error }
|
|
|
|
// UpdateStorageUsed 增减用户已用存储(delta可正可负)
|
|
func (r *UserRepo) UpdateStorageUsed(id uint, delta int64) error {
|
|
return r.DB.Model(&model.User{}).Where("id = ?", id).
|
|
UpdateColumn("storage_used", gorm.Expr("storage_used + ?", delta)).Error
|
|
}
|
|
|
|
// ---- API Key ----
|
|
|
|
// APIKeyRepo API密钥数据访问
|
|
type APIKeyRepo struct{ DB *gorm.DB }
|
|
|
|
func (r *APIKeyRepo) Create(k *model.APIKey) error { return r.DB.Create(k).Error }
|
|
|
|
func (r *APIKeyRepo) FindByID(id uint) (*model.APIKey, error) {
|
|
var k model.APIKey
|
|
err := r.DB.First(&k, id).Error
|
|
return &k, err
|
|
}
|
|
|
|
func (r *APIKeyRepo) FindByAccessKey(accessKey string) (*model.APIKey, error) {
|
|
var k model.APIKey
|
|
err := r.DB.Where("access_key = ?", accessKey).First(&k).Error
|
|
return &k, err
|
|
}
|
|
|
|
func (r *APIKeyRepo) ListByUser(userID uint) ([]model.APIKey, error) {
|
|
var keys []model.APIKey
|
|
err := r.DB.Where("user_id = ?", userID).Order("id DESC").Find(&keys).Error
|
|
return keys, err
|
|
}
|
|
|
|
func (r *APIKeyRepo) Delete(id, userID uint) error {
|
|
return r.DB.Where("id = ? AND user_id = ?", id, userID).Delete(&model.APIKey{}).Error
|
|
}
|
|
|
|
func (r *APIKeyRepo) Touch(id uint) error {
|
|
return r.DB.Model(&model.APIKey{}).Where("id = ?", id).
|
|
UpdateColumn("last_used_at", gorm.Expr("CURRENT_TIMESTAMP")).Error
|
|
}
|