api
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
// Package usersvc 是后台管理小程序端用户的业务逻辑。
|
||||
package usersvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"portal-mini-go/internal/model"
|
||||
"portal-mini-go/internal/pkg/errors"
|
||||
"portal-mini-go/internal/repository/user"
|
||||
)
|
||||
|
||||
// Service 用户管理业务服务,依赖用户仓储。
|
||||
type Service struct {
|
||||
repo *user.Repository
|
||||
}
|
||||
|
||||
func New(repo *user.Repository) *Service { return &Service{repo: repo} }
|
||||
|
||||
// ListReq 列表查询条件。
|
||||
type ListReq struct {
|
||||
MiniID uint64
|
||||
Keyword string
|
||||
Page int
|
||||
Size int
|
||||
}
|
||||
|
||||
// List 分页查询用户,缺省第 1 页、每页 20 条。
|
||||
func (s *Service) List(ctx context.Context, req ListReq) ([]model.User, int64, error) {
|
||||
page, size := req.Page, req.Size
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if size <= 0 {
|
||||
size = 20
|
||||
}
|
||||
return s.repo.AdminList(ctx, user.AdminListFilter{
|
||||
MiniID: req.MiniID,
|
||||
Keyword: req.Keyword,
|
||||
Page: page,
|
||||
Size: size,
|
||||
})
|
||||
}
|
||||
|
||||
// GetByID 按 ID 查询用户详情。
|
||||
func (s *Service) GetByID(ctx context.Context, id uint64) (*model.User, error) {
|
||||
return s.repo.AdminGet(ctx, id)
|
||||
}
|
||||
|
||||
// UpdateReq 编辑用户资料请求;指针区分“未提供”与“置空”。
|
||||
type UpdateReq struct {
|
||||
Nickname *string
|
||||
Avatar *string
|
||||
Status *int8
|
||||
}
|
||||
|
||||
// Update 编辑用户资料与状态,返回更新后的用户。
|
||||
func (s *Service) Update(ctx context.Context, id uint64, req UpdateReq) (*model.User, error) {
|
||||
if req.Status != nil {
|
||||
if *req.Status != model.UserStatusEnabled && *req.Status != model.UserStatusDisabled {
|
||||
return nil, errors.New(40000, "非法的用户状态")
|
||||
}
|
||||
}
|
||||
if err := s.repo.AdminUpdate(ctx, id, req.Nickname, req.Avatar, req.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminGet(ctx, id)
|
||||
}
|
||||
|
||||
// SetStatus 禁用 / 启用用户,返回更新后的用户。
|
||||
func (s *Service) SetStatus(ctx context.Context, id uint64, status int8) (*model.User, error) {
|
||||
if status != model.UserStatusEnabled && status != model.UserStatusDisabled {
|
||||
return nil, errors.New(40000, "非法的用户状态")
|
||||
}
|
||||
if err := s.repo.AdminUpdate(ctx, id, nil, nil, &status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminGet(ctx, id)
|
||||
}
|
||||
Reference in New Issue
Block a user