moto/internal/organize/user.go

58 lines
1.5 KiB
Go

package organize
import (
"context"
"fmt"
"git.nobla.cn/golang/moto/common/db"
"git.nobla.cn/golang/moto/models"
"git.nobla.cn/golang/rest/types"
"gorm.io/gorm"
)
func UserTypes(ctx context.Context) []*types.TypeValue {
result, err := db.TryCache(ctx, fmt.Sprintf("user:types"), func(tx *gorm.DB) (any, error) {
values := make([]*models.User, 0)
err := tx.Order("uid ASC").Find(&values).Error
if err != nil {
return nil, err
}
data := make([]*types.TypeValue, 0, len(values))
for _, row := range values {
data = append(data, &types.TypeValue{
Label: fmt.Sprintf("%s(%s)", row.Username, row.UID),
Value: row.UID,
})
}
return data, nil
}, db.WithDepend("SELECT max(`updated_at`) FROM `users`"))
if err == nil {
return result.([]*types.TypeValue)
}
return nil
}
func UserTags(ctx context.Context) []*types.TypeValue {
result, err := db.TryCache(ctx, fmt.Sprintf("users:tags"), func(tx *gorm.DB) (any, error) {
values := make([]*models.User, 0)
if errTx := tx.Select("DISTINCT(`tag`) AS `tag`").Find(&values).Error; errTx == nil {
vs := make([]*types.TypeValue, 0, len(values))
for _, row := range values {
if row.Tag != "" {
vs = append(vs, &types.TypeValue{
Label: row.Tag,
Value: row.Tag,
})
}
}
return vs, nil
} else {
return nil, errTx
}
}, db.WithDepend("SELECT max(`updated_at`) FROM `users`"))
if err == nil {
return result.([]*types.TypeValue)
} else {
return []*types.TypeValue{}
}
}