moto/internal/organize/user.go

58 lines
1.5 KiB
Go
Raw Normal View History

2024-12-12 17:39:04 +08:00
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"
)
2024-12-13 10:37:09 +08:00
func UserTypes(ctx context.Context) []*types.TypeValue {
result, err := db.TryCache(ctx, fmt.Sprintf("user:types"), func(tx *gorm.DB) (any, error) {
2024-12-12 17:39:04 +08:00
values := make([]*models.User, 0)
2024-12-13 10:37:09 +08:00
err := tx.Order("uid ASC").Find(&values).Error
2024-12-12 17:39:04 +08:00
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
2024-12-13 10:37:09 +08:00
}, db.WithDepend("SELECT max(`updated_at`) FROM `users`"))
2024-12-12 17:39:04 +08:00
if err == nil {
return result.([]*types.TypeValue)
}
return nil
}
2024-12-13 10:37:09 +08:00
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{}
}
}