feat: add post page
This commit is contained in:
@@ -64,15 +64,14 @@ func (m *mediasModel) countByCondition(ctx context.Context, expr BoolExpression)
|
||||
}
|
||||
|
||||
func (m *mediasModel) List(ctx context.Context, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||
limit := pagination.GetLimit()
|
||||
offset := pagination.GetOffset()
|
||||
pagination.Format()
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(limit).
|
||||
OFFSET(offset)
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var medias []model.Medias
|
||||
|
||||
@@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
@@ -72,6 +73,9 @@ func (m *postsModel) GetByID(ctx context.Context, id int64) (*model.Posts, error
|
||||
|
||||
// Create
|
||||
func (m *postsModel) Create(ctx context.Context, model *model.Posts) error {
|
||||
model.CreatedAt = time.Now()
|
||||
model.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
@@ -86,6 +90,8 @@ func (m *postsModel) Create(ctx context.Context, model *model.Posts) error {
|
||||
|
||||
// Update
|
||||
func (m *postsModel) Update(ctx context.Context, id int64, model *model.Posts) error {
|
||||
model.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.UPDATE(tbl.MutableColumns).SET(model).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
@@ -118,16 +124,15 @@ func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression)
|
||||
}
|
||||
|
||||
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
||||
limit := pagination.GetLimit()
|
||||
offset := pagination.GetOffset()
|
||||
pagination.Format()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(limit).
|
||||
OFFSET(offset)
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var posts []model.Posts
|
||||
|
||||
@@ -2,10 +2,14 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"quyun/app/service/testx"
|
||||
"quyun/database"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@@ -36,8 +40,37 @@ func Test_Posts(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *PostsTestSuite) Test_Demo() {
|
||||
func (s *PostsTestSuite) Test_BatchInsert() {
|
||||
Convey("Test_Demo", s.T(), func() {
|
||||
database.Truncate(context.Background(), db, table.Posts.TableName())
|
||||
|
||||
count := 100
|
||||
for i := 0; i < count; i++ {
|
||||
model := model.Posts{
|
||||
Status: fields.PostStatusPublished,
|
||||
Title: fmt.Sprintf("test-title-%d", i),
|
||||
Description: fmt.Sprintf("test-description-%d", i),
|
||||
Content: fmt.Sprintf("test-content-%d", i),
|
||||
Price: rand.Int63n(10000),
|
||||
Discount: int16(rand.Intn(100)),
|
||||
Views: rand.Int63n(10000),
|
||||
Likes: rand.Int63n(10000),
|
||||
Tags: fields.ToJson([]string{"tag1", "tag2", "tag3"}),
|
||||
Assets: fields.ToJson([]fields.MediaAsset{
|
||||
{
|
||||
Type: fields.MediaAssetTypeAudio,
|
||||
Media: rand.Int63n(10000),
|
||||
},
|
||||
{
|
||||
Type: fields.MediaAssetTypeVideo,
|
||||
Media: rand.Int63n(10000),
|
||||
},
|
||||
}),
|
||||
}
|
||||
|
||||
if err := Posts.Create(context.Background(), &model); err != nil {
|
||||
s.T().Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,29 +9,19 @@ type Pager struct {
|
||||
}
|
||||
|
||||
type Pagination struct {
|
||||
Page int64 `json:"page" form:"page" query:"page"`
|
||||
Limit int64 `json:"limit" form:"limit" query:"limit"`
|
||||
Page int64 `json:"page" form:"page" query:"page"`
|
||||
Limit int64 `json:"limit" form:"limit" query:"limit"`
|
||||
Offset int64 `json:"-"`
|
||||
}
|
||||
|
||||
func (filter *Pagination) GetOffset() int64 {
|
||||
return (filter.Page - 1) * filter.Limit
|
||||
}
|
||||
|
||||
func (filter *Pagination) GetLimit() int64 {
|
||||
if filter.Limit <= 0 {
|
||||
return 10
|
||||
}
|
||||
return filter.Limit
|
||||
}
|
||||
|
||||
func (filter *Pagination) Format() *Pagination {
|
||||
func (filter *Pagination) Format() {
|
||||
if filter.Page <= 0 {
|
||||
filter.Page = 1
|
||||
}
|
||||
|
||||
if !lo.Contains([]int64{10, 20, 50, 100}, filter.Limit) {
|
||||
if !lo.Contains([]int64{10, 25, 50, 100}, filter.Limit) {
|
||||
filter.Limit = 10
|
||||
}
|
||||
|
||||
return filter
|
||||
filter.Offset = (filter.Page - 1) * filter.Limit
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user