Feat: use transactions to manipulate user's used storage

This commit is contained in:
HFO4
2022-02-27 14:24:17 +08:00
parent 285e80ba76
commit 521c5c8dc4
11 changed files with 56 additions and 81 deletions

View File

@@ -39,12 +39,23 @@ func init() {
}
// Create 创建文件记录
func (file *File) Create() (uint, error) {
if err := DB.Create(file).Error; err != nil {
func (file *File) Create() error {
tx := DB.Begin()
if err := tx.Create(file).Error; err != nil {
util.Log().Warning("无法插入文件记录, %s", err)
return 0, err
tx.Rollback()
return err
}
return file.ID, nil
user := &User{}
user.ID = file.UserID
if err := user.ChangeStorage(tx, "+", file.Size); err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
// AfterFind 找到文件后的钩子