feat: add post page

This commit is contained in:
yanghao05
2025-04-09 16:40:34 +08:00
parent aa8077937f
commit 6151f4e244
8 changed files with 457 additions and 91 deletions

View File

@@ -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