feat: update

This commit is contained in:
yanghao05
2025-04-11 16:09:06 +08:00
parent 79972e963c
commit 58f1e78054
5 changed files with 95 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ import (
"quyun/database/schemas/public/model"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
type ListQuery struct {
@@ -16,7 +17,7 @@ type ListQuery struct {
type posts struct{}
// List posts
// @Router /v1/posts [get]
// @Router /posts [get]
// @Bind pagination query
// @Bind query query
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
@@ -25,8 +26,17 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
}
// Show
// @Router /v1/posts/:id [get]
// @Router /show/:id [get]
// @Bind id path
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*model.Posts, error) {
return models.Posts.GetByID(ctx.Context(), id)
}
// Mine posts
// @Router /mine [get]
// @Bind pagination query
// @Bind query query
func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
return models.Users.PostList(ctx.Context(), 1, pagination, query.Keyword)
}

View File

@@ -28,15 +28,21 @@ func (r *Routes) Name() string {
func (r *Routes) Register(router fiber.Router) {
// 注册路由组: posts
router.Get("/v1/posts", DataFunc2(
router.Get("/", DataFunc2(
r.posts.List,
Query[requests.Pagination]("pagination"),
Query[ListQuery]("query"),
))
router.Get("/v1/posts/:id", DataFunc1(
router.Get("/show/:id", DataFunc1(
r.posts.Show,
PathParam[int64]("id"),
))
router.Get("/mine", DataFunc2(
r.posts.Mine,
Query[requests.Pagination]("pagination"),
Query[ListQuery]("query"),
))
}