Skip to content

Commit

Permalink
fix: move the user storage used calc #78
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Jul 12, 2021
1 parent 9630885 commit 1c57452
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
34 changes: 17 additions & 17 deletions internal/app/dao/matter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,7 @@ func (ms *Matter) Create(matter *model.Matter) error {
return fmt.Errorf("parent dir not exist")
}

fc := func(tx *gorm.DB) error {
if err := tx.Create(matter).Error; err != nil {
return err
}

// update the service
expr := gorm.Expr("used+?", matter.Size)
if err := tx.Model(&model.UserStorage{}).Where("uid=?", matter.Uid).Update("used", expr).Error; err != nil {
return err
}

return nil
}
return gdb.Transaction(fc)
return gdb.Create(matter).Error
}

func (ms *Matter) Find(alias string) (*model.Matter, error) {
Expand All @@ -104,9 +91,22 @@ func (ms *Matter) FindUserMatter(uid int64, alias string) (*model.Matter, error)
return m, nil
}

func (ms *Matter) Uploaded(alias string) error {
m := gdb.Model(&model.Matter{})
return m.Where("alias=?", alias).Update("uploaded_at", time.Now()).Error
func (ms *Matter) Uploaded(matter *model.Matter) error {
fc := func(tx *gorm.DB) error {
if err := tx.First(matter).Update("uploaded_at", time.Now()).Error; err != nil {
return err
}

// update the storage used of the user
expr := gorm.Expr("used+?", matter.Size)
if err := tx.Model(&model.UserStorage{}).Where("uid=?", matter.Uid).Update("used", expr).Error; err != nil {
return err
}

return nil
}

return gdb.Transaction(fc)
}

func (ms *Matter) Rename(alias, name string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/fakefs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (f *File) UploadDone(uid int64, alias string) (*model.Matter, error) {
return nil, err
}

if err := f.dMatter.Uploaded(alias); err != nil {
if err := f.dMatter.Uploaded(m); err != nil {
return nil, err
}

Expand Down

0 comments on commit 1c57452

Please sign in to comment.