refactor: update users api

This commit is contained in:
ning 2024-06-17 17:09:33 +08:00
parent 5c1d4077e2
commit 0a2e23c285
3 changed files with 7 additions and 4 deletions

View File

@ -46,6 +46,7 @@ func (rt *Router) userGets(c *gin.Context) {
order := ginx.QueryStr(c, "order", "username")
desc := ginx.QueryBool(c, "desc", false)
rt.UserCache.UpdateUsersLastActiveTime()
total, err := models.UserTotal(rt.Ctx, query, stime, etime)
ginx.Dangerous(err)

View File

@ -215,13 +215,13 @@ func (uc *UserCacheType) loopUpdateLastActiveTime() {
duration := 5 * time.Minute
for {
time.Sleep(duration)
if err := uc.updateUsersLastActiveTime(); err != nil {
if err := uc.UpdateUsersLastActiveTime(); err != nil {
logger.Warningf("failed to update users' last active time: %v", err)
}
}
}
func (uc *UserCacheType) updateUsersLastActiveTime() error {
func (uc *UserCacheType) UpdateUsersLastActiveTime() error {
// read the full list of users from the database
users, err := models.UserGetAll(uc.ctx)
if err != nil {

View File

@ -205,8 +205,10 @@ func (u *User) Add(ctx *ctx.Context) error {
}
func (u *User) Update(ctx *ctx.Context, selectField interface{}, selectFields ...interface{}) error {
if err := u.Verify(); err != nil {
return err
if u.Belong == "" {
if err := u.Verify(); err != nil {
return err
}
}
return DB(ctx).Model(u).Select(selectField, selectFields...).Updates(u).Error