feat: update page
This commit is contained in:
@@ -78,3 +78,57 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *model.Posts) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete posts
|
||||
// @Router /v1/admin/posts/:id [delete]
|
||||
// @Bind id path
|
||||
func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if post == nil {
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
if err := models.Posts.DeleteByID(ctx.Context(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PostItem struct {
|
||||
*model.Posts
|
||||
Medias []*models.MediaItem `json:"medias"`
|
||||
}
|
||||
|
||||
// Show posts by id
|
||||
// @Router /v1/admin/posts/:id [get]
|
||||
// @Bind id path
|
||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
medias, err := models.Medias.GetByIds(ctx.Context(), lo.Map(post.Assets.Data, func(asset fields.MediaAsset, _ int) int64 {
|
||||
return asset.Media
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PostItem{
|
||||
Posts: post,
|
||||
Medias: lo.Map(medias, func(media *model.Medias, _ int) *models.MediaItem {
|
||||
return &models.MediaItem{
|
||||
ID: media.ID,
|
||||
Name: media.Name,
|
||||
UploadTime: media.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
FileSize: media.Size,
|
||||
MimeType: media.MimeType,
|
||||
FileType: models.Medias.ConvertFileTypeByMimeType(media.MimeType),
|
||||
ThumbnailUrl: "",
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -56,6 +56,16 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
Body[model.Posts]("form"),
|
||||
))
|
||||
|
||||
router.Delete("/v1/admin/posts/:id", Func1(
|
||||
r.posts.Delete,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
|
||||
router.Get("/v1/admin/posts/:id", DataFunc1(
|
||||
r.posts.Show,
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
|
||||
// 注册路由组: uploads
|
||||
router.Post("/v1/admin/uploads/:md5/chunks/:idx", Func3(
|
||||
r.uploads.Chunks,
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
@@ -29,9 +28,7 @@ func (m *postsModel) Prepare() error {
|
||||
func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
|
||||
tbl := table.Posts
|
||||
|
||||
cond := tbl.DeletedAt.IS_NULL().AND(
|
||||
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
|
||||
)
|
||||
cond := tbl.DeletedAt.IS_NULL()
|
||||
|
||||
if key == nil || *key == "" {
|
||||
return cond
|
||||
@@ -58,8 +55,6 @@ func (m *postsModel) GetByID(ctx context.Context, id int64) (*model.Posts, error
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)).AND(
|
||||
tbl.DeletedAt.IS_NULL(),
|
||||
).AND(
|
||||
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
|
||||
),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
@@ -211,3 +206,21 @@ func (m *postsModel) Buy(ctx context.Context, userId, postId int64) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByID soft delete item
|
||||
func (m *postsModel) DeleteByID(ctx context.Context, id int64) error {
|
||||
tbl := table.Posts
|
||||
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 post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user