feat: add user_list
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
)
|
||||
|
||||
type TenantFilter struct {
|
||||
@@ -35,3 +36,7 @@ func (form *TenantExpireUpdateForm) ParseDuration() (time.Duration, error) {
|
||||
}
|
||||
return duration, nil
|
||||
}
|
||||
|
||||
type TenantStatusUpdateForm struct {
|
||||
Status consts.TenantStatus `json:"status" validate:"required,oneof=normal disabled"`
|
||||
}
|
||||
|
||||
20
backend/app/http/super/dto/user.go
Normal file
20
backend/app/http/super/dto/user.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
)
|
||||
|
||||
type UserPageFilter struct {
|
||||
requests.Pagination
|
||||
requests.SortQueryFilter
|
||||
|
||||
Username *string `query:"username"`
|
||||
TenantID *int64 `query:"tenant_id"`
|
||||
}
|
||||
|
||||
type UserItem struct {
|
||||
*models.User
|
||||
|
||||
StatusDescription string `json:"status_description,omitempty"`
|
||||
}
|
||||
@@ -26,13 +26,13 @@ func Provide(opts ...opt.Option) error {
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
authController *authController,
|
||||
staticController *staticController,
|
||||
tenant *tenant,
|
||||
user *user,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
authController: authController,
|
||||
staticController: staticController,
|
||||
tenant: tenant,
|
||||
authController: authController,
|
||||
tenant: tenant,
|
||||
user: user,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
@@ -56,5 +56,12 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*user, error) {
|
||||
obj := &user{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -5,13 +5,12 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/super/dto"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
. "go.ipao.vip/atom/fen"
|
||||
"quyun/v2/app/http/super/dto"
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
@@ -21,9 +20,9 @@ import (
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
// Controller instances
|
||||
authController *authController
|
||||
staticController *staticController
|
||||
tenant *tenant
|
||||
authController *authController
|
||||
tenant *tenant
|
||||
user *user
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
@@ -59,8 +58,18 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
PathParam[int64]("tenantID"),
|
||||
Body[dto.TenantExpireUpdateForm]("form"),
|
||||
))
|
||||
|
||||
router.Get("/super/*", Func(r.staticController.static))
|
||||
r.log.Debugf("Registering route: Patch /super/v1/tenants/:tenantID/status -> tenant.updateStatus")
|
||||
router.Patch("/super/v1/tenants/:tenantID/status", Func2(
|
||||
r.tenant.updateStatus,
|
||||
PathParam[int64]("tenantID"),
|
||||
Body[dto.TenantStatusUpdateForm]("form"),
|
||||
))
|
||||
// Register routes for controller: user
|
||||
r.log.Debugf("Registering route: Get /super/v1/users -> user.list")
|
||||
router.Get("/super/v1/users", DataFunc1(
|
||||
r.user.list,
|
||||
Query[dto.UserPageFilter]("filter"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type staticController struct{}
|
||||
|
||||
// Static
|
||||
//
|
||||
// @Tags Super
|
||||
// @Router /super/*
|
||||
func (ctl *staticController) static(ctx fiber.Ctx) error {
|
||||
root := "/home/rogee/Projects/quyun_v2/frontend/superadmin/dist/"
|
||||
param := ctx.Params("*")
|
||||
file := filepath.Join(root, param)
|
||||
// // Static
|
||||
// //
|
||||
// // @Tags Super
|
||||
// // @Router /super/*
|
||||
// func (ctl *staticController) static(ctx fiber.Ctx) error {
|
||||
// root := "/home/rogee/Projects/quyun_v2/frontend/superadmin/dist/"
|
||||
// param := ctx.Params("*")
|
||||
// file := filepath.Join(root, param)
|
||||
|
||||
// if file not exits use index.html
|
||||
if _, err := os.Stat(file); os.IsNotExist(err) {
|
||||
file = filepath.Join(root, "index.html")
|
||||
}
|
||||
// // if file not exits use index.html
|
||||
// if _, err := os.Stat(file); os.IsNotExist(err) {
|
||||
// file = filepath.Join(root, "index.html")
|
||||
// }
|
||||
|
||||
return ctx.SendFile(file)
|
||||
}
|
||||
// return ctx.SendFile(file)
|
||||
// }
|
||||
|
||||
@@ -47,3 +47,19 @@ func (*tenant) updateExpire(ctx fiber.Ctx, tenantID int64, form *dto.TenantExpir
|
||||
|
||||
return services.Tenant.AddExpireDuration(ctx, tenantID, duration)
|
||||
}
|
||||
|
||||
// updateStatus
|
||||
//
|
||||
// @Summary 更新租户状态
|
||||
// @Tags Super
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantID path int64 true "TenantID"
|
||||
// @Param form body dto.TenantStatusUpdateForm true "Form"
|
||||
//
|
||||
// @Router /super/v1/tenants/:tenantID/status [patch]
|
||||
// @Bind tenantID path
|
||||
// @Bind form body
|
||||
func (*tenant) updateStatus(ctx fiber.Ctx, tenantID int64, form *dto.TenantStatusUpdateForm) error {
|
||||
return services.Tenant.UpdateStatus(ctx, tenantID, form.Status)
|
||||
}
|
||||
|
||||
28
backend/app/http/super/user.go
Normal file
28
backend/app/http/super/user.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
_ "quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type user struct{}
|
||||
|
||||
// list
|
||||
//
|
||||
// @Summary 租户列表
|
||||
// @Tags Super
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param filter query dto.UserPageFilter true "Filter"
|
||||
// @Success 200 {object} requests.Pager{items=dto.UserItem}
|
||||
//
|
||||
// @Router /super/v1/users [get]
|
||||
// @Bind filter query
|
||||
func (*user) list(ctx fiber.Ctx, filter *dto.UserPageFilter) (*requests.Pager, error) {
|
||||
return services.User.Page(ctx, filter)
|
||||
}
|
||||
@@ -195,3 +195,21 @@ func (t *tenant) AddExpireDuration(ctx context.Context, tenantID int64, duration
|
||||
}
|
||||
return m.Save(ctx)
|
||||
}
|
||||
|
||||
// UpdateStatus
|
||||
func (t *tenant) UpdateStatus(ctx context.Context, tenantID int64, status consts.TenantStatus) error {
|
||||
logrus.WithField("tenant_id", tenantID).WithField("status", status).Info("update tenant status")
|
||||
|
||||
m, err := t.FindByID(ctx, tenantID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Status = status
|
||||
_, err = m.Update(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
@@ -52,16 +54,8 @@ func (t *user) SetStatus(ctx context.Context, userID int64, status consts.UserSt
|
||||
return m.Save(ctx)
|
||||
}
|
||||
|
||||
type UserPageFilter struct {
|
||||
requests.Pagination
|
||||
requests.SortQueryFilter
|
||||
|
||||
Username *string `query:"username"`
|
||||
TenantID *int64 `query:"tenant_id"`
|
||||
}
|
||||
|
||||
// Page
|
||||
func (t *user) Page(ctx context.Context, filter *UserPageFilter) (*requests.Pager, error) {
|
||||
func (t *user) Page(ctx context.Context, filter *dto.UserPageFilter) (*requests.Pager, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
conds := []gen.Condition{}
|
||||
@@ -76,11 +70,18 @@ func (t *user) Page(ctx context.Context, filter *UserPageFilter) (*requests.Page
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
items, total, err := query.Where(conds...).Order(tbl.ID.Desc()).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
users, total, err := query.Where(conds...).Order(tbl.ID.Desc()).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := lo.Map(users, func(model *models.User, _ int) *dto.UserItem {
|
||||
return &dto.UserItem{
|
||||
User: model,
|
||||
StatusDescription: model.Status.Description(),
|
||||
}
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
@@ -115,7 +116,7 @@ func (t *UserTestSuite) Test_Page() {
|
||||
err := m.Create(t.T().Context())
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
pager, err := User.Page(t.T().Context(), &UserPageFilter{
|
||||
pager, err := User.Page(t.T().Context(), &dto.UserPageFilter{
|
||||
Username: &username,
|
||||
})
|
||||
|
||||
@@ -192,7 +193,7 @@ func (t *UserTestSuite) Test_Page() {
|
||||
So(err, ShouldBeNil)
|
||||
}
|
||||
|
||||
pager, err := User.Page(t.T().Context(), &UserPageFilter{
|
||||
pager, err := User.Page(t.T().Context(), &dto.UserPageFilter{
|
||||
TenantID: lo.ToPtr(int64(1)),
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user