Skip to content

Commit

Permalink
refactor: update modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 6, 2023
1 parent 73d97ae commit 1cd7bb1
Show file tree
Hide file tree
Showing 10 changed files with 3,733 additions and 13,823 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ test:
go test -race -cover ./...

install:
go get -d entgo.io/ent/cmd/entc@v0.11.9
go install entgo.io/ent/cmd/entc@v0.12.3

generate:
rm -rf ./ent
go run entgo.io/ent/cmd/ent generate --feature sql/modifier --feature privacy --feature intercept ./schema --template ./template --target ./ent
entc generate --feature sql/modifier --feature schema/snapshot --feature privacy --feature intercept ./schema --template ./template --target ./ent

describe:
entc describe ./schema
Expand Down
20 changes: 20 additions & 0 deletions controller/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package controller

import (
"context"
"strings"
"time"

"github.com/iancoleman/strcase"
"github.com/vicanso/elton"
"github.com/vicanso/forest/cs"
"github.com/vicanso/forest/ent"
gen "github.com/vicanso/forest/ent"
"github.com/vicanso/forest/ent/configuration"
confSchema "github.com/vicanso/forest/ent/configuration"
"github.com/vicanso/forest/router"
Expand Down Expand Up @@ -164,6 +167,23 @@ func (params *configurationListParmas) where(query *ent.ConfigurationQuery) *ent
return query
}

// GetOrders 获取排序的函数列表
func (params *configurationListParmas) GetOrders() []configuration.OrderOption {
if params.Order == "" {
return nil
}
arr := strings.Split(params.Order, ",")
funcs := make([]configuration.OrderOption, len(arr))
for index, item := range arr {
if item[0] == '-' {
funcs[index] = gen.Desc(strcase.ToSnake(item[1:]))
} else {
funcs[index] = gen.Asc(strcase.ToSnake(item))
}
}
return funcs
}

// queryAll 查询配置列表
func (params *configurationListParmas) queryAll(ctx context.Context) ([]*ent.Configuration, error) {
query := getConfigurationClient().Query()
Expand Down
1 change: 0 additions & 1 deletion controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestListParams(t *testing.T) {
"id",
"updated_at",
}, params.GetFields())
assert.Equal(2, len(params.GetOrders()))
}

func newContextAndUserSession() (*elton.Context, *session.UserSession) {
Expand Down
52 changes: 47 additions & 5 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import (
"math/rand"
"net/http"
"strconv"
"strings"
"time"

"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqljson"
"github.com/iancoleman/strcase"
"github.com/samber/lo"
"github.com/tidwall/gjson"
"github.com/vicanso/elton"
"github.com/vicanso/forest/config"
"github.com/vicanso/forest/cs"
"github.com/vicanso/forest/ent"
gen "github.com/vicanso/forest/ent"
"github.com/vicanso/forest/ent/predicate"
"github.com/vicanso/forest/ent/user"
"github.com/vicanso/forest/ent/userlogin"
Expand Down Expand Up @@ -404,6 +407,23 @@ func (params *userListParams) where(query *ent.UserQuery) *ent.UserQuery {
return query
}

// GetOrders 获取排序的函数列表
func (params *userListParams) GetOrders() []user.OrderOption {
if params.Order == "" {
return nil
}
arr := strings.Split(params.Order, ",")
funcs := make([]user.OrderOption, len(arr))
for index, item := range arr {
if item[0] == '-' {
funcs[index] = gen.Desc(strcase.ToSnake(item[1:]))
} else {
funcs[index] = gen.Asc(strcase.ToSnake(item))
}
}
return funcs
}

// queryAll 查询用户列表
func (params *userListParams) queryAll(ctx context.Context) ([]*ent.User, error) {
query := getUserClient().Query()
Expand Down Expand Up @@ -439,6 +459,23 @@ func (params *userLoginListParams) where(query *ent.UserLoginQuery) *ent.UserLog
return query
}

// GetOrders 获取排序的函数列表
func (params *userLoginListParams) GetOrders() []userlogin.OrderOption {
if params.Order == "" {
return nil
}
arr := strings.Split(params.Order, ",")
funcs := make([]userlogin.OrderOption, len(arr))
for index, item := range arr {
if item[0] == '-' {
funcs[index] = gen.Desc(strcase.ToSnake(item[1:]))
} else {
funcs[index] = gen.Asc(strcase.ToSnake(item))
}
}
return funcs
}

// queryAll 查询所有的登录记录
func (params *userLoginListParams) queryAll(ctx context.Context) ([]*ent.UserLogin, error) {
query := getUserLoginClient().Query()
Expand Down Expand Up @@ -541,7 +578,8 @@ func (ctrl *userCtrl) updateByID(c *elton.Context) error {
// 在登录之前需要先调用获取token,此token用于登录时与客户密码sha256生成hash,
// 保证客户每次登录时的密码均不相同,避免接口重放登录。
// Responses:
// 200: apiUserLoginTokenResponse
//
// 200: apiUserLoginTokenResponse
func (*userCtrl) getLoginToken(c *elton.Context) error {
us := getUserSession(c)
// 清除当前session id,确保每次登录的用户都是新的session
Expand All @@ -568,7 +606,8 @@ func (*userCtrl) getLoginToken(c *elton.Context) error {
// 若用户登录状态,则返回客户的相关信息。
// 若用户未登录,仅返回服务器当前时间。
// Responses:
// 200: apiUserInfoResponse
//
// 200: apiUserInfoResponse
func (*userCtrl) me(c *elton.Context) error {
cookie, _ := c.Cookie(sessionConfig.TrackKey)
if cookie == nil {
Expand Down Expand Up @@ -627,7 +666,8 @@ func (*userCtrl) detail(c *elton.Context) error {
// 在成功注册后返回用户信息。
// 需注意此时并非登录状态,需要客户自主登录。
// Responses:
// 201: apiUserRegisterResponse
//
// 201: apiUserRegisterResponse
func (*userCtrl) register(c *elton.Context) error {
params := userRegisterLoginParams{}
err := validateBody(c, &params)
Expand Down Expand Up @@ -659,7 +699,8 @@ func (*userCtrl) register(c *elton.Context) error {
// 用户登录时需要先获取token,之后使用token与密码sha256后提交,
// 登录成功后返回用户信息。
// Responses:
// 200: apiUserInfoResponse
//
// 200: apiUserInfoResponse
func (*userCtrl) login(c *elton.Context) error {
params := userRegisterLoginParams{}
err := validateBody(c, &params)
Expand Down Expand Up @@ -764,7 +805,8 @@ func (*userCtrl) login(c *elton.Context) error {
//
// 退出用户当前登录状态,成功时并无内容返回。
// Responses:
// 204: apiNoContentResponse
//
// 204: apiNoContentResponse
func (*userCtrl) logout(c *elton.Context) error {
us := getUserSession(c)
// 清除session
Expand Down
Loading

0 comments on commit 1cd7bb1

Please sign in to comment.