Skip to content

Commit

Permalink
feat(cmfx/modules/member): 添加 Level 和 Type 相关的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Jan 2, 2025
1 parent 8721d07 commit 92c67b4
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 25 deletions.
24 changes: 23 additions & 1 deletion cmfx/modules/member/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,30 @@ func Load(mod *cmfx.Module, conf *Config, up *upload.Module, adminMod *admin.Mod
Get("/member/levels", m.getLevels, adminAPI(func(o *openapi.Operation) {
o.Tag("member").Desc(web.Phrase("get member level list api"), nil).Response200([]tag.TagPO{})
})).
Patch("/member/levels/{id:digit}", m.adminPatchLevel, adminAPI(func(o *openapi.Operation) {
o.Tag("member").
Desc(web.Phrase("patch member level api"), nil).
Body(tagInfo{}, false, nil, nil).
PathID("id:digit", web.Phrase("the ID of member level"))
})).
Post("/member/levels", m.adminPostLevel, adminAPI(func(o *openapi.Operation) {
o.Tag("member").
Desc(web.Phrase("add member level api"), nil).
Body(tagInfo{}, false, nil, nil)
})).
Get("/member/types", m.getTypes, adminAPI(func(o *openapi.Operation) {
o.Tag("member").Desc(web.Phrase("get member type list api"), nil).Response200([]tag.TagPO{})
})).
Patch("/member/types/{id:digit}", m.adminPatchType, adminAPI(func(o *openapi.Operation) {
o.Tag("member").
Desc(web.Phrase("patch member type api"), nil).
Body(tagInfo{}, false, nil, nil).
PathID("id:digit", web.Phrase("the ID of member type"))
})).
Post("/member/types", m.adminPostType, adminAPI(func(o *openapi.Operation) {
o.Tag("member").
Desc(web.Phrase("add member type api"), nil).
Body(tagInfo{}, false, nil, nil)
}))

// member 接口
Expand All @@ -106,7 +128,7 @@ func Load(mod *cmfx.Module, conf *Config, up *upload.Module, adminMod *admin.Mod
Body(memberInfoTO{}, false, nil, nil).
ResponseEmpty("204")
})).
Get("/invited", m.adminGetMemberInvited, adminAPI(func(o *openapi.Operation) {
Get("/invited", m.memberGetMemberInvited, adminAPI(func(o *openapi.Operation) {
o.Tag("member").Desc(web.Phrase("get member invited api"), nil).
QueryObject(&invitedQuery{}, nil).
Response200(query.Page[InvitedMember]{})
Expand Down
5 changes: 0 additions & 5 deletions cmfx/modules/member/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import (

var _ web.Filter = &invitedQuery{}

func newModule(suite *test.Suite) *Module {
mod := suite.NewModule("mem")
return Load(mod, defaultConfig(suite.Assertion()), uploadtest.NewModule(suite, "mem_upload"), admintest.NewModule(suite))
}

func TestModule_NewMember(t *testing.T) {
a := assert.New(t, false)
s := test.NewSuite(a)
Expand Down
84 changes: 82 additions & 2 deletions cmfx/modules/member/route_admins.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2024 caixw
// SPDX-FileCopyrightText: 2024-2025 caixw
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -211,13 +211,22 @@ func (mem *adminInfoTO) Filter(v *web.FilterContext) {
return t.After(time.Now())
}), locales.InvalidValue))

inviter := filter.NewBuilder(filter.V(validator.ZeroOr(func(id int64) bool {
u, err := mem.m.UserModule().GetUser(id)
if err != nil {
v.Context().Server().Logs().ERROR().Error(err)
return false
}
return u.ID > 0
}), locales.InvalidValue))

v.Add(filters.NotEmpty("username", &mem.Username)).
Add(user.StateFilter("state", &mem.State)).
Add(filters.NotEmpty("password", &mem.Password)).
Add(birthday("birthday", &mem.Birthday)).
Add(filter.NewBuilder(filter.V(validator.ZeroOr(types.SexValidator), locales.InvalidValue))("sex", &mem.Sex)).
Add(filters.Avatar("avatar", &mem.Avatar)).
Add(filters.Avatar("avatar", &mem.Avatar)) // TODO intviter
Add(inviter("inviter", &mem.Inviter))
}

func (mem *adminInfoTO) toInfo() *RegisterInfo {
Expand Down Expand Up @@ -289,10 +298,81 @@ func (m *Module) getLevels(ctx *web.Context) web.Responser {
return web.OK(l)
}

type tagInfo struct {
XMLName struct{} `json:"-" cbor:"-" yaml:"-" xml:"info"`
Title string `json:"title" cbor:"title" yaml:"title" xml:"title"`
}

func (t *tagInfo) Filter(ctx *web.FilterContext) {
ctx.Add(filters.NotEmpty("title", &t.Title))
}

func (m *Module) adminPatchLevel(ctx *web.Context) web.Responser {
id, resp := ctx.PathID("id", cmfx.NotFoundInvalidPath)
if resp != nil {
return resp
}

data := &tagInfo{}
if resp = ctx.Read(true, data, cmfx.BadRequestInvalidBody); resp != nil {
return resp
}

if err := m.levels.Set(id, data.Title); err != nil {
return ctx.Error(err, "")
}

return web.NoContent()
}

func (m *Module) adminPostLevel(ctx *web.Context) web.Responser {
data := &tagInfo{}
if resp := ctx.Read(true, data, cmfx.BadRequestInvalidBody); resp != nil {
return resp
}

if err := m.levels.Add(data.Title); err != nil {
return ctx.Error(err, "")
}

return web.Created(nil, "")
}

func (m *Module) getTypes(ctx *web.Context) web.Responser {
l, err := m.types.Get()
if err != nil {
return ctx.Error(err, "")
}
return web.OK(l)
}

func (m *Module) adminPatchType(ctx *web.Context) web.Responser {
id, resp := ctx.PathID("id", cmfx.NotFoundInvalidPath)
if resp != nil {
return resp
}

data := &tagInfo{}
if resp = ctx.Read(true, data, cmfx.BadRequestInvalidBody); resp != nil {
return resp
}

if err := m.types.Set(id, data.Title); err != nil {
return ctx.Error(err, "")
}

return web.NoContent()
}

func (m *Module) adminPostType(ctx *web.Context) web.Responser {
data := &tagInfo{}
if resp := ctx.Read(true, data, cmfx.BadRequestInvalidBody); resp != nil {
return resp
}

if err := m.types.Add(data.Title); err != nil {
return ctx.Error(err, "")
}

return web.Created(nil, "")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/issue9/webfilter v0.9.0
github.com/issue9/webuse/v7 v7.0.0-20241230041047-b527a7c27188
github.com/mattn/go-sqlite3 v1.14.24
github.com/shirou/gopsutil/v4 v4.24.11
github.com/shirou/gopsutil/v4 v4.24.12
golang.org/x/crypto v0.31.0
golang.org/x/text v0.21.0
)
Expand Down
20 changes: 4 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,10 @@ github.com/issue9/version v1.0.8 h1:IsNdDYdV8UGDGwwgp8H4RszJE0Ko26HjWg9pZzyOivs=
github.com/issue9/version v1.0.8/go.mod h1:w8bQwODBOG5+iaS3qIJbElxxpp3Uo4x5F39qKBqwpdc=
github.com/issue9/watermark v1.2.4 h1:iu3xIhn0PyCltpEagRA4rEq/o6CNpK6rvzigZCj2Ank=
github.com/issue9/watermark v1.2.4/go.mod h1:Fa7Rky0U7BECCWDblX3sZ1JFwB5vFK55FF9HKTyYyew=
github.com/issue9/web v0.100.8 h1:m7ZytUduZlYOfVwQlNMwKJrd/VQz8Xa0KKLZGZeagfk=
github.com/issue9/web v0.100.8/go.mod h1:Jl+09NuNBPKBPe727dr32hNzVnexKEU5PcOyGAgYqrc=
github.com/issue9/web v0.100.9-0.20241230035511-e33ba17825d9 h1:r+QDaF8PHRsDzd9dVnji25+JXSDK4HtnfSg+3eVOIdM=
github.com/issue9/web v0.100.9-0.20241230035511-e33ba17825d9/go.mod h1:Jl+09NuNBPKBPe727dr32hNzVnexKEU5PcOyGAgYqrc=
github.com/issue9/web v0.100.9-0.20241230042041-062fbc92386d h1:dOYgx8JipJxfGhnxNthVBdkQtpt7qxyzmJhENUOMkv0=
github.com/issue9/web v0.100.9-0.20241230042041-062fbc92386d/go.mod h1:Jl+09NuNBPKBPe727dr32hNzVnexKEU5PcOyGAgYqrc=
github.com/issue9/webfilter v0.9.0 h1:UbSPPGhmQ2kr/XY/P9uNyRwugW5+FqRdcPUkHUFF1sE=
github.com/issue9/webfilter v0.9.0/go.mod h1:YepSF2nHnC0GWriKetPKPYnQghuLJmHieaQ5JJNDffE=
github.com/issue9/webuse/v7 v7.0.0-20241220091456-beb06bc648ed h1:j3OCbSM7K12E5d5hDC9FpY44Exl/szyB1RB/yj4vT9w=
github.com/issue9/webuse/v7 v7.0.0-20241220091456-beb06bc648ed/go.mod h1:m//ZcCvElJkAp9pigNGLvuhnMSmm3ynIXBBq8O1JJoI=
github.com/issue9/webuse/v7 v7.0.0-20241229151521-62b60879d6ed h1:xOJBNKAp1+t4LHUxGSHUbgaXkAEPB6AgHLlm0fBlNV0=
github.com/issue9/webuse/v7 v7.0.0-20241229151521-62b60879d6ed/go.mod h1:i75yenoNY0QBgxkQgiq0ScjLPGHdRVQApZkUjQPoljQ=
github.com/issue9/webuse/v7 v7.0.0-20241230023955-0afafd0d17cd h1:jL5F5H5Pcx3zja7SSK2z7sJDRAY/iDSRqNuK/oJy6Hg=
github.com/issue9/webuse/v7 v7.0.0-20241230023955-0afafd0d17cd/go.mod h1:i75yenoNY0QBgxkQgiq0ScjLPGHdRVQApZkUjQPoljQ=
github.com/issue9/webuse/v7 v7.0.0-20241230025552-b3fdc0344268 h1:0JTswRQ12UN/ApAgkU6T8dT4zyPZb5Ad0eUfdhOdNsA=
github.com/issue9/webuse/v7 v7.0.0-20241230025552-b3fdc0344268/go.mod h1:i75yenoNY0QBgxkQgiq0ScjLPGHdRVQApZkUjQPoljQ=
github.com/issue9/webuse/v7 v7.0.0-20241230041047-b527a7c27188 h1:y1z+zQDSjvgNwacDKpmqO6CJhxPQ58RaLl3F4R5AQnQ=
github.com/issue9/webuse/v7 v7.0.0-20241230041047-b527a7c27188/go.mod h1:i75yenoNY0QBgxkQgiq0ScjLPGHdRVQApZkUjQPoljQ=
github.com/jellydator/ttlcache/v3 v3.3.0 h1:BdoC9cE81qXfrxeb9eoJi9dWrdhSuwXMAnHTbnBm4Wc=
Expand Down Expand Up @@ -126,12 +114,12 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
github.com/shirou/gopsutil/v4 v4.24.11/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8=
github.com/shirou/gopsutil/v4 v4.24.12 h1:qvePBOk20e0IKA1QXrIIU+jmk+zEiYVVx06WjBRlZo4=
github.com/shirou/gopsutil/v4 v4.24.12/go.mod h1:DCtMPAad2XceTeIAbGyVfycbYQNBGk2P8cvDi7/VN9o=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
Expand Down

0 comments on commit 92c67b4

Please sign in to comment.