Skip to content

Commit

Permalink
refactor: reimplement the user storage quota calc
Browse files Browse the repository at this point in the history
Signed-off-by: saltbo <saltbo@foxmail.com>
  • Loading branch information
saltbo committed Aug 5, 2023
1 parent c06eed7 commit 78e0178
Show file tree
Hide file tree
Showing 19 changed files with 583 additions and 72 deletions.
2 changes: 1 addition & 1 deletion hack/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
})

// Generate basic type-safe DAO API for struct `model.User` following conventions
g.ApplyBasic(entity.Storage{}, entity.Matter{}, entity.RecycleBin{})
g.ApplyBasic(entity.Storage{}, entity.Matter{}, entity.RecycleBin{}, entity.UserStorage{})

// Generate the code
g.Execute()
Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/recyclebin.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (rs *RecycleBinResource) delete(c *gin.Context) {
}

func (rs *RecycleBinResource) clean(c *gin.Context) {
if err := rs.rbf.Clean(c, ginutil.QueryInt64(c, "sid")); err != nil {
if err := rs.rbf.Clean(c, ginutil.QueryInt64(c, "sid"), authed.UidGet(c)); err != nil {
ginutil.JSONServerError(c, err)
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/app/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ type DBQueryFactory struct {
}

func NewDBQueryFactory() *DBQueryFactory {
return &DBQueryFactory{}
}

func (D *DBQueryFactory) Q() *query.Query {
if !viper.IsSet("installed") {
return nil
}
Expand All @@ -51,5 +47,9 @@ func (D *DBQueryFactory) Q() *query.Query {
log.Fatalln(err)
}

return &DBQueryFactory{}
}

func (D *DBQueryFactory) Q() *query.Query {
return query.Use(gdb)
}
7 changes: 4 additions & 3 deletions internal/app/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/saltbo/gopkg/strutil"
"github.com/saltbo/zpan/internal/app/entity"
"gorm.io/gorm"
"gorm.io/gorm/clause"

Expand Down Expand Up @@ -85,8 +86,8 @@ func (u *User) Create(user *model.User, storageMax uint64) (*model.User, error)
Uid: user.Id,
Nickname: user.Email[:strings.Index(user.Email, "@")],
}
user.Storage = model.UserStorage{
Max: model.UserStorageDefaultSize,
user.Storage = entity.UserStorage{
Max: entity.UserStorageDefaultSize,
}
if storageMax > 0 {
user.Storage.Max = storageMax
Expand Down Expand Up @@ -144,5 +145,5 @@ func (u *User) UpdateProfile(uid int64, up *model.UserProfile) error {
}

func (u *User) UpdateStorage(uid int64, quota uint64) error {
return gdb.Model(model.UserStorage{}).Where("uid=?", uid).Update("max", quota).Error
return gdb.Model(entity.UserStorage{}).Where("uid=?", uid).Update("max", quota).Error
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package entity

import (
"time"
Expand All @@ -21,12 +21,12 @@ type UserStorage struct {
Deleted gorm.DeletedAt `json:"-"`
}

func (UserStorage) TableName() string {
func (us *UserStorage) TableName() string {
return "zp_storage_quota"
}

func (sq *UserStorage) Overflowed(addonSize int64) bool {
if sq.Used+uint64(addonSize) >= sq.Max {
func (us *UserStorage) Overflowed(addonSize int64) bool {
if us.Used+uint64(addonSize) >= us.Max {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/model/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func Tables() []interface{} {
new(User),
new(UserKey),
new(UserProfile),
new(UserStorage),
new(entity.UserStorage),
new(entity.Storage),
new(entity.Matter),
new(Share),
Expand Down
29 changes: 15 additions & 14 deletions internal/app/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/saltbo/zpan/internal/app/entity"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -45,20 +46,20 @@ func NewUserCreateOption() UserCreateOption {
}

type User struct {
Id int64 `json:"id"`
Email string `json:"email" gorm:"size:32;unique_index;not null"`
Username string `json:"username" gorm:"size:20;unique_index;not null"`
Password string `json:"-" gorm:"size:32;not null"`
Status uint8 `json:"-" gorm:"size:1;not null"`
StatusTxt string `json:"status" gorm:"-"`
Roles string `json:"-" gorm:"size:64;not null"`
RoleTxt string `json:"role" gorm:"-"`
Ticket string `json:"ticket" gorm:"size:6;unique_index;not null"`
Profile UserProfile `json:"profile,omitempty" gorm:"foreignKey:Uid"`
Storage UserStorage `json:"storage,omitempty" gorm:"foreignKey:Uid"`
Created time.Time `json:"created" gorm:"autoCreateTime;not null"`
Updated time.Time `json:"updated" gorm:"autoUpdateTime;not null"`
Deleted gorm.DeletedAt `json:"-"`
Id int64 `json:"id"`
Email string `json:"email" gorm:"size:32;unique_index;not null"`
Username string `json:"username" gorm:"size:20;unique_index;not null"`
Password string `json:"-" gorm:"size:32;not null"`
Status uint8 `json:"-" gorm:"size:1;not null"`
StatusTxt string `json:"status" gorm:"-"`
Roles string `json:"-" gorm:"size:64;not null"`
RoleTxt string `json:"role" gorm:"-"`
Ticket string `json:"ticket" gorm:"size:6;unique_index;not null"`
Profile UserProfile `json:"profile,omitempty" gorm:"foreignKey:Uid"`
Storage entity.UserStorage `json:"storage,omitempty" gorm:"foreignKey:Uid"`
Created time.Time `json:"created" gorm:"autoCreateTime;not null"`
Updated time.Time `json:"updated" gorm:"autoUpdateTime;not null"`
Deleted gorm.DeletedAt `json:"-"`

Token string `json:"-" gorm:"-"`
}
Expand Down
58 changes: 33 additions & 25 deletions internal/app/repo/query/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 78e0178

Please sign in to comment.