diff --git a/backend/app/http/admin/posts.go b/backend/app/http/admin/posts.go index 0961043..3b4af7c 100644 --- a/backend/app/http/admin/posts.go +++ b/backend/app/http/admin/posts.go @@ -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 +} diff --git a/backend/app/http/admin/routes.gen.go b/backend/app/http/admin/routes.gen.go index 88b62ab..374e374 100644 --- a/backend/app/http/admin/routes.gen.go +++ b/backend/app/http/admin/routes.gen.go @@ -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, diff --git a/backend/app/models/posts.go b/backend/app/models/posts.go index 5fb1e8c..0eacc46 100644 --- a/backend/app/models/posts.go +++ b/backend/app/models/posts.go @@ -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 +} diff --git a/backend/test.http b/backend/test.http index 3c317d8..a02f82c 100644 --- a/backend/test.http +++ b/backend/test.http @@ -43,4 +43,7 @@ Content-Type: application/json ### get posts with keyword GET {{host}}/v1/admin/posts?page=1&limit=10&keyword=99123 HTTP/1.1 -Content-Type: application/json \ No newline at end of file +Content-Type: application/json + +### delete posts +DELETE {{host}}/v1/admin/posts/103 HTTP/1.1 \ No newline at end of file diff --git a/frontend/admin/src/api/posts_item.json b/frontend/admin/src/api/posts_item.json new file mode 100644 index 0000000..4c2c1f3 --- /dev/null +++ b/frontend/admin/src/api/posts_item.json @@ -0,0 +1,25 @@ +{ + "id": 102, + "created_at": "2025-04-09T20:25:00.118963Z", + "updated_at": "2025-04-09T20:25:00.118963Z", + "deleted_at": null, + "status": 0, + "title": "adsfafd", + "description": "afda", + "content": "", + "price": 123123, + "discount": 100, + "views": 0, + "likes": 0, + "tags": null, + "assets": [ + { + "type": "unknown", + "media": 47 + }, + { + "type": "document", + "media": 48 + } + ] +} \ No newline at end of file diff --git a/frontend/admin/src/pages/PostEditPage.vue b/frontend/admin/src/pages/PostEditPage.vue index 207acc8..093cdf7 100644 --- a/frontend/admin/src/pages/PostEditPage.vue +++ b/frontend/admin/src/pages/PostEditPage.vue @@ -1,4 +1,5 @@