Skip to content

Commit 78e0178

Browse files
committed
refactor: reimplement the user storage quota calc
Signed-off-by: saltbo <saltbo@foxmail.com>
1 parent c06eed7 commit 78e0178

File tree

19 files changed

+583
-72
lines changed

19 files changed

+583
-72
lines changed

hack/gen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
})
1313

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

1717
// Generate the code
1818
g.Execute()

internal/app/api/recyclebin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (rs *RecycleBinResource) delete(c *gin.Context) {
6767
}
6868

6969
func (rs *RecycleBinResource) clean(c *gin.Context) {
70-
if err := rs.rbf.Clean(c, ginutil.QueryInt64(c, "sid")); err != nil {
70+
if err := rs.rbf.Clean(c, ginutil.QueryInt64(c, "sid"), authed.UidGet(c)); err != nil {
7171
ginutil.JSONServerError(c, err)
7272
return
7373
}

internal/app/dao/dao.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ type DBQueryFactory struct {
3939
}
4040

4141
func NewDBQueryFactory() *DBQueryFactory {
42-
return &DBQueryFactory{}
43-
}
44-
45-
func (D *DBQueryFactory) Q() *query.Query {
4642
if !viper.IsSet("installed") {
4743
return nil
4844
}
@@ -51,5 +47,9 @@ func (D *DBQueryFactory) Q() *query.Query {
5147
log.Fatalln(err)
5248
}
5349

50+
return &DBQueryFactory{}
51+
}
52+
53+
func (D *DBQueryFactory) Q() *query.Query {
5454
return query.Use(gdb)
5555
}

internal/app/dao/user.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/saltbo/gopkg/strutil"
9+
"github.com/saltbo/zpan/internal/app/entity"
910
"gorm.io/gorm"
1011
"gorm.io/gorm/clause"
1112

@@ -85,8 +86,8 @@ func (u *User) Create(user *model.User, storageMax uint64) (*model.User, error)
8586
Uid: user.Id,
8687
Nickname: user.Email[:strings.Index(user.Email, "@")],
8788
}
88-
user.Storage = model.UserStorage{
89-
Max: model.UserStorageDefaultSize,
89+
user.Storage = entity.UserStorage{
90+
Max: entity.UserStorageDefaultSize,
9091
}
9192
if storageMax > 0 {
9293
user.Storage.Max = storageMax
@@ -144,5 +145,5 @@ func (u *User) UpdateProfile(uid int64, up *model.UserProfile) error {
144145
}
145146

146147
func (u *User) UpdateStorage(uid int64, quota uint64) error {
147-
return gdb.Model(model.UserStorage{}).Where("uid=?", uid).Update("max", quota).Error
148+
return gdb.Model(entity.UserStorage{}).Where("uid=?", uid).Update("max", quota).Error
148149
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package model
1+
package entity
22

33
import (
44
"time"
@@ -21,12 +21,12 @@ type UserStorage struct {
2121
Deleted gorm.DeletedAt `json:"-"`
2222
}
2323

24-
func (UserStorage) TableName() string {
24+
func (us *UserStorage) TableName() string {
2525
return "zp_storage_quota"
2626
}
2727

28-
func (sq *UserStorage) Overflowed(addonSize int64) bool {
29-
if sq.Used+uint64(addonSize) >= sq.Max {
28+
func (us *UserStorage) Overflowed(addonSize int64) bool {
29+
if us.Used+uint64(addonSize) >= us.Max {
3030
return true
3131
}
3232

internal/app/model/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func Tables() []interface{} {
88
new(User),
99
new(UserKey),
1010
new(UserProfile),
11-
new(UserStorage),
11+
new(entity.UserStorage),
1212
new(entity.Storage),
1313
new(entity.Matter),
1414
new(Share),

internal/app/model/user.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"time"
77

8+
"github.com/saltbo/zpan/internal/app/entity"
89
"gorm.io/gorm"
910
)
1011

@@ -45,20 +46,20 @@ func NewUserCreateOption() UserCreateOption {
4546
}
4647

4748
type User struct {
48-
Id int64 `json:"id"`
49-
Email string `json:"email" gorm:"size:32;unique_index;not null"`
50-
Username string `json:"username" gorm:"size:20;unique_index;not null"`
51-
Password string `json:"-" gorm:"size:32;not null"`
52-
Status uint8 `json:"-" gorm:"size:1;not null"`
53-
StatusTxt string `json:"status" gorm:"-"`
54-
Roles string `json:"-" gorm:"size:64;not null"`
55-
RoleTxt string `json:"role" gorm:"-"`
56-
Ticket string `json:"ticket" gorm:"size:6;unique_index;not null"`
57-
Profile UserProfile `json:"profile,omitempty" gorm:"foreignKey:Uid"`
58-
Storage UserStorage `json:"storage,omitempty" gorm:"foreignKey:Uid"`
59-
Created time.Time `json:"created" gorm:"autoCreateTime;not null"`
60-
Updated time.Time `json:"updated" gorm:"autoUpdateTime;not null"`
61-
Deleted gorm.DeletedAt `json:"-"`
49+
Id int64 `json:"id"`
50+
Email string `json:"email" gorm:"size:32;unique_index;not null"`
51+
Username string `json:"username" gorm:"size:20;unique_index;not null"`
52+
Password string `json:"-" gorm:"size:32;not null"`
53+
Status uint8 `json:"-" gorm:"size:1;not null"`
54+
StatusTxt string `json:"status" gorm:"-"`
55+
Roles string `json:"-" gorm:"size:64;not null"`
56+
RoleTxt string `json:"role" gorm:"-"`
57+
Ticket string `json:"ticket" gorm:"size:6;unique_index;not null"`
58+
Profile UserProfile `json:"profile,omitempty" gorm:"foreignKey:Uid"`
59+
Storage entity.UserStorage `json:"storage,omitempty" gorm:"foreignKey:Uid"`
60+
Created time.Time `json:"created" gorm:"autoCreateTime;not null"`
61+
Updated time.Time `json:"updated" gorm:"autoUpdateTime;not null"`
62+
Deleted gorm.DeletedAt `json:"-"`
6263

6364
Token string `json:"-" gorm:"-"`
6465
}

internal/app/repo/query/gen.go

Lines changed: 33 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)