feat: add users page
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"quyun/app/requests"
|
||||
"quyun/app/service/testx"
|
||||
"quyun/database"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
@@ -195,19 +196,19 @@ func (s *MediasTestSuite) Test_ConvertFileTypeByMimeType() {
|
||||
Convey("image", func() {
|
||||
mimeType := "image/jpeg"
|
||||
fileType := Medias.ConvertFileTypeByMimeType(mimeType)
|
||||
So(fileType, ShouldEqual, MediaTypeImage)
|
||||
So(fileType, ShouldEqual, fields.MediaAssetTypeImage)
|
||||
})
|
||||
|
||||
Convey("video", func() {
|
||||
mimeType := "video/mp4"
|
||||
fileType := Medias.ConvertFileTypeByMimeType(mimeType)
|
||||
So(fileType, ShouldEqual, MediaTypeVideo)
|
||||
So(fileType, ShouldEqual, fields.MediaAssetTypeVideo)
|
||||
})
|
||||
|
||||
Convey("invalid mime type", func() {
|
||||
mimeType := "invalid/type"
|
||||
fileType := Medias.ConvertFileTypeByMimeType(mimeType)
|
||||
So(fileType, ShouldEqual, MediaTypeUnknown)
|
||||
So(fileType, ShouldEqual, fields.MediaAssetTypeUnknown)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func (m *postsModel) Update(ctx context.Context, id int64, model *model.Posts) e
|
||||
model.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.UPDATE(tbl.MutableColumns).MODEL(model).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
stmt := tbl.UPDATE(tbl.MutableColumns.Except(tbl.CreatedAt, tbl.DeletedAt)).MODEL(model).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
|
||||
@@ -2,7 +2,9 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
@@ -75,3 +77,123 @@ func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, e
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
// BuildConditionWithKey builds the WHERE clause for user queries
|
||||
func (m *usersModel) BuildConditionWithKey(key *string) BoolExpression {
|
||||
tbl := table.Users
|
||||
|
||||
cond := tbl.DeletedAt.IS_NULL()
|
||||
|
||||
if key == nil || *key == "" {
|
||||
return cond
|
||||
}
|
||||
|
||||
cond = cond.AND(
|
||||
tbl.Username.LIKE(String("%" + *key + "%")),
|
||||
)
|
||||
|
||||
return cond
|
||||
}
|
||||
|
||||
// countByCondition counts users matching the given condition
|
||||
func (m *usersModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
||||
var cnt struct {
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
tbl := table.Users
|
||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
err := stmt.QueryContext(ctx, db, &cnt)
|
||||
if err != nil {
|
||||
m.log.Errorf("error counting users: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
// List returns a paginated list of users
|
||||
func (m *usersModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var users []model.Users = make([]model.Users, 0)
|
||||
err := stmt.QueryContext(ctx, db, &users)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying users: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := m.countByCondition(ctx, cond)
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting user count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: users,
|
||||
Total: count,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Create creates a new user
|
||||
func (m *usersModel) Create(ctx context.Context, model *model.Users) error {
|
||||
model.CreatedAt = time.Now()
|
||||
model.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Users
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
if err != nil {
|
||||
m.log.Errorf("error creating user: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update updates an existing user
|
||||
func (m *usersModel) Update(ctx context.Context, id int64, model *model.Users) error {
|
||||
model.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Users
|
||||
stmt := tbl.UPDATE(tbl.MutableColumns.Except(tbl.CreatedAt, tbl.DeletedAt)).MODEL(model).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
if err != nil {
|
||||
m.log.Errorf("error updating user: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByID soft deletes a user by ID
|
||||
func (m *usersModel) DeleteByID(ctx context.Context, id int64) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.DeletedAt).
|
||||
SET(TimestampT(time.Now())).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log.Errorf("error deleting user: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,12 +6,16 @@ import (
|
||||
|
||||
"quyun/app/service/testx"
|
||||
"quyun/database"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
"github.com/samber/lo"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
|
||||
// . "github.com/go-jet/jet/v2/postgres"
|
||||
gonanoid "github.com/matoous/go-nanoid/v2"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
@@ -36,8 +40,25 @@ func Test_Users(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *UsersTestSuite) Test_Demo() {
|
||||
Convey("Test_Demo", s.T(), func() {
|
||||
func (s *UsersTestSuite) Test_BatchInsert() {
|
||||
Convey("Test_BatchInsert", s.T(), func() {
|
||||
database.Truncate(context.Background(), db, table.Users.TableName())
|
||||
|
||||
count := 100
|
||||
for i := 0; i < count; i++ {
|
||||
// generate openid use nanoid
|
||||
openID, err := gonanoid.New(10)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
user := &model.Users{
|
||||
Status: fields.UserStatusOk,
|
||||
OpenID: openID,
|
||||
Username: "Username_" + openID,
|
||||
Avatar: lo.ToPtr("Avatar_" + openID),
|
||||
}
|
||||
err = Users.Create(context.Background(), user)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user