From 94fce0d9731cce200134eea6736168679cbf6ae5 Mon Sep 17 00:00:00 2001 From: L-time Date: Fri, 7 Jun 2024 11:30:34 +0800 Subject: [PATCH] fixed --- internal/index/mysql_repository_test.go | 2 +- internal/subject/mysql_repository_test.go | 4 ++++ web/handler/episode_comment.go | 20 ++++---------------- web/handler/index/comment.go | 19 +++---------------- web/handler/index/index.go | 4 ++-- web/handler/subject/comment.go | 15 ++++----------- web/handler/subject/subject.go | 3 +-- web/routes.go | 4 ++-- 8 files changed, 21 insertions(+), 50 deletions(-) diff --git a/internal/index/mysql_repository_test.go b/internal/index/mysql_repository_test.go index d0320d943..7c39285a6 100644 --- a/internal/index/mysql_repository_test.go +++ b/internal/index/mysql_repository_test.go @@ -76,7 +76,7 @@ func TestMysqlRepo_NewIndex(t *testing.T) { index := &model.Index{ ID: 0, Title: "test", - Description: "Test Field", + Description: "Test Index", CreatorID: 382951, CreatedAt: now, UpdatedAt: now, diff --git a/internal/subject/mysql_repository_test.go b/internal/subject/mysql_repository_test.go index c6a767a99..83c044249 100644 --- a/internal/subject/mysql_repository_test.go +++ b/internal/subject/mysql_repository_test.go @@ -160,3 +160,7 @@ func TestMysqlRepo_GetCharacterRelated(t *testing.T) { require.True(t, found, "character 1 should be related to subject 8") } + +func Test_mysqlRepo_GetAllPost(t *testing.T) { + +} diff --git a/web/handler/episode_comment.go b/web/handler/episode_comment.go index 86ab6afb8..a8afb2bd2 100644 --- a/web/handler/episode_comment.go +++ b/web/handler/episode_comment.go @@ -3,7 +3,6 @@ package handler import ( "errors" "net/http" - "strconv" "time" "github.com/labstack/echo/v4" @@ -90,24 +89,13 @@ func (h Handler) GetEpisodeComments(c echo.Context) error { return errgo.Wrap(err, "failed to find subject of episode") } - var offset, limit int - offsetStr := c.QueryParam("offset") - limitStr := c.QueryParam("limit") - if offsetStr == "" { - offset = 0 // 默认为0 - } else { - offset, err = strconv.Atoi(offsetStr) - } - if limitStr == "" { - limit = 25 // 默认25 - } else { - limit, err = strconv.Atoi(limitStr) - } + pq, err := req.GetPageQuery(c, req.DefaultPageLimit, req.DefaultMaxPageLimit) if err != nil { - return res.BadRequest(err.Error()) + return res.BadRequest("cannot get offset and limit") } + var r []model.EpisodeComment - r, err = h.episode.GetAllComment(c.Request().Context(), id, offset, limit) + r, err = h.episode.GetAllComment(c.Request().Context(), id, pq.Offset, pq.Limit) if err != nil { return res.NotFound("cannot get episode comments") } diff --git a/web/handler/index/comment.go b/web/handler/index/comment.go index ffc4954d8..829721908 100644 --- a/web/handler/index/comment.go +++ b/web/handler/index/comment.go @@ -2,7 +2,6 @@ package index import ( "net/http" - "strconv" "time" "github.com/labstack/echo/v4" @@ -63,24 +62,12 @@ func (h Handler) GetComments(c echo.Context) error { if !ok || r.NSFW && !user.AllowNSFW() { return res.NotFound("index not found") } - var offset, limit int - offsetStr := c.QueryParam("offset") - limitStr := c.QueryParam("limit") - if offsetStr == "" { - offset = 0 // 默认为0 - } else { - offset, err = strconv.Atoi(offsetStr) - } - if limitStr == "" { - limit = 25 // 默认25 - } else { - limit, err = strconv.Atoi(limitStr) - } + pq, err := req.GetPageQuery(c, req.DefaultPageLimit, req.DefaultMaxPageLimit) if err != nil { - return res.BadRequest(err.Error()) + return res.BadRequest("cannot get offset and limit") } var result []model.IndexComment - result, err = h.i.GetIndexComments(c.Request().Context(), id, offset, limit) + result, err = h.i.GetIndexComments(c.Request().Context(), id, pq.Offset, pq.Limit) if err != nil { return res.NotFound("comment not found") diff --git a/web/handler/index/index.go b/web/handler/index/index.go index 08d17ca83..10e0669de 100644 --- a/web/handler/index/index.go +++ b/web/handler/index/index.go @@ -124,7 +124,7 @@ func (h Handler) getIndexSubjects( ) error { count, err := h.i.CountSubjects(c.Request().Context(), id, subjectType) if err != nil { - return errgo.Wrap(err, "Field.CountSubjects") + return errgo.Wrap(err, "Index.CountSubjects") } if count == 0 { @@ -182,7 +182,7 @@ func (h Handler) NewIndex(c echo.Context) error { } ctx := c.Request().Context() if err := h.i.New(ctx, i); err != nil { - return errgo.Wrap(err, "failed to crea te a new index") + return errgo.Wrap(err, "failed to create a new index") } u, err := h.u.GetByID(ctx, i.CreatorID) if err != nil { diff --git a/web/handler/subject/comment.go b/web/handler/subject/comment.go index e46773f83..0daf5b29c 100644 --- a/web/handler/subject/comment.go +++ b/web/handler/subject/comment.go @@ -69,19 +69,12 @@ func (h Subject) GetComments(c echo.Context) error { return errgo.Wrap(err, "failed to get subject") } - var offset, limit int - - offsetStr := c.QueryParam("offset") - limitStr := c.QueryParam("limit") - - if offsetStr == "" { - offset = 0 // 默认为0 - } - if limitStr == "" { - limit = 25 // 默认25 + pq, err := req.GetPageQuery(c, req.DefaultPageLimit, req.DefaultMaxPageLimit) + if err != nil { + return res.BadRequest("cannot get offset and limit") } - result, err := h.subject.GetAllPost(c.Request().Context(), s.ID, offset, limit) + result, err := h.subject.GetAllPost(c.Request().Context(), s.ID, pq.Offset, pq.Limit) if err != nil { return res.BadRequest("cannot found comment") } diff --git a/web/handler/subject/subject.go b/web/handler/subject/subject.go index 742439b72..65ec05844 100644 --- a/web/handler/subject/subject.go +++ b/web/handler/subject/subject.go @@ -21,7 +21,6 @@ import ( "github.com/bangumi/server/internal/episode" "github.com/bangumi/server/internal/person" "github.com/bangumi/server/internal/subject" - "github.com/bangumi/server/web/mw" ) type Subject struct { @@ -59,6 +58,6 @@ func (h *Subject) Routes(g *echo.Group) { // comment g.GET("/subjects/:id/post", h.GetComments) g.GET("/subjects/:id/post/:post_id", h.GetComment) - g.PUT("/subjects/:id/post", h.AddComment, mw.NeedLogin) + // g.PUT("/subjects/:id/post", h.AddComment, mw.NeedLogin) // g.DELETE("/subjects/:id/post/:post_id", h.RemoveComment, mw.NeedLogin) } diff --git a/web/routes.go b/web/routes.go index 0c1a0a7a3..a930601b6 100644 --- a/web/routes.go +++ b/web/routes.go @@ -82,7 +82,7 @@ func AddRouters( v0.GET("/episodes", h.ListEpisode) v0.GET("episodes/:id/comment", h.GetEpisodeComments) - v0.PUT("episodes/:id/comment", h.PostEpisodeComment, mw.NeedLogin) + // v0.PUT("episodes/:id/comment", h.PostEpisodeComment, mw.NeedLogin) v0.GET("episodes/:id/comment/:comment_id", h.GetEpisodeComment) // echo 中间件从前往后运行按顺序 @@ -122,7 +122,7 @@ func AddRouters( v0.GET("/indices/:id/comment", i.GetComments, mw.NeedLogin) v0.GET("/indices/:id/comment/:comment_id", i.GetComment, mw.NeedLogin) - v0.PUT("/indices/:id/comment", i.AddComment, mw.NeedLogin) + // v0.PUT("/indices/:id/comment", i.AddComment, mw.NeedLogin) // v0.DELETE("/indices/:id/comment/:comment_id", i.RemoveComment, mw.NeedLogin) }