diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 03abfc0d..d29e7c52 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -149,6 +149,8 @@ paths: - $ref: "#/components/parameters/targetQuery" - $ref: "#/components/parameters/sinceQuery" - $ref: "#/components/parameters/untilQuery" + - $ref: "#/components/parameters/limitQuery" + - $ref: "#/components/parameters/offsetQuery" - $ref: "#/components/parameters/tagQuery" - $ref: "#/components/parameters/groupQuery" - $ref: "#/components/parameters/createdByQuery" diff --git a/model/request.go b/model/request.go index e8476231..94460dc2 100644 --- a/model/request.go +++ b/model/request.go @@ -64,6 +64,8 @@ type RequestQuery struct { Status *string Since *time.Time Until *time.Time + Limit int + Offset int Tag *string Group *string CreatedBy *uuid.UUID diff --git a/model/request_impl.go b/model/request_impl.go index 33ae869e..9efc5174 100644 --- a/model/request_impl.go +++ b/model/request_impl.go @@ -102,6 +102,8 @@ func (repo *EntRepository) GetRequests( ) } + requestsq = requestsq.Limit(query.Limit).Offset(query.Offset) + if query.Group != nil && *query.Group != "" { requestsq = requestsq. Where( diff --git a/router/request.go b/router/request.go index 19d2bdbe..56a3dda3 100644 --- a/router/request.go +++ b/router/request.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "strconv" "time" "github.com/google/uuid" @@ -157,6 +158,38 @@ func (h Handlers) GetRequests(c echo.Context) error { } until = &u } + limit := 100 + if limitQuery := c.QueryParam("limit"); limitQuery != "" { + limitI, err := strconv.Atoi(limitQuery) + if err != nil { + h.Logger.Info("could not parse limit as integer", zap.Error(err)) + return echo.NewHTTPError(http.StatusBadRequest, err) + } + if limitI < 0 { + h.Logger.Info("received negative limit", zap.Int("limit", limitI)) + return echo.NewHTTPError( + http.StatusBadRequest, + fmt.Errorf("negative limit(=%d) is invalid", limitI), + ) + } + limit = limitI + } + offset := 0 + if offsetQuery := c.QueryParam("offset"); offsetQuery != "" { + offsetI, err := strconv.Atoi(offsetQuery) + if err != nil { + h.Logger.Info("could not parse offset as integer", zap.Error(err)) + return echo.NewHTTPError(http.StatusBadRequest, err) + } + if offsetI < 0 { + h.Logger.Info("received negative offset", zap.Int("offset", offsetI)) + return echo.NewHTTPError( + http.StatusBadRequest, + fmt.Errorf("negative offset(=%d) is invalid", offsetI), + ) + } + offset = offsetI + } var tag *string if c.QueryParam("tag") != "" { t := c.QueryParam("tag") @@ -182,6 +215,8 @@ func (h Handlers) GetRequests(c echo.Context) error { Status: ss, Since: since, Until: until, + Limit: limit, + Offset: offset, Tag: tag, Group: group, CreatedBy: cratedBy, diff --git a/router/request_test.go b/router/request_test.go index 03c7ddf7..ac6d3778 100644 --- a/router/request_test.go +++ b/router/request_test.go @@ -67,7 +67,10 @@ func TestHandlers_GetRequests(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - GetRequests(c.Request().Context(), model.RequestQuery{}). + GetRequests(c.Request().Context(), model.RequestQuery{ + Limit: 100, + Offset: 0, + }). Return(requests, nil) res := []*RequestResponse{ @@ -122,7 +125,10 @@ func TestHandlers_GetRequests(t *testing.T) { assert.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - GetRequests(c.Request().Context(), model.RequestQuery{}). + GetRequests(c.Request().Context(), model.RequestQuery{ + Limit: 100, + Offset: 0, + }). Return(requests, nil) res := []*RequestResponse{} @@ -167,6 +173,8 @@ func TestHandlers_GetRequests(t *testing.T) { EXPECT(). GetRequests(c.Request().Context(), model.RequestQuery{ Status: &status, + Limit: 100, + Offset: 0, }). Return(requests, nil) @@ -228,7 +236,9 @@ func TestHandlers_GetRequests(t *testing.T) { h.Repository.MockRequestRepository. EXPECT(). GetRequests(c.Request().Context(), model.RequestQuery{ - Until: &date2, + Until: &date2, + Limit: 100, + Offset: 0, }). Return(requests, nil) @@ -290,7 +300,9 @@ func TestHandlers_GetRequests(t *testing.T) { h.Repository.MockRequestRepository. EXPECT(). GetRequests(c.Request().Context(), model.RequestQuery{ - Since: &date2, + Since: &date2, + Limit: 100, + Offset: 0, }). Return(requests, nil) @@ -364,7 +376,9 @@ func TestHandlers_GetRequests(t *testing.T) { h.Repository.MockRequestRepository. EXPECT(). GetRequests(c.Request().Context(), model.RequestQuery{ - Tag: &tag1.Name, + Tag: &tag1.Name, + Limit: 100, + Offset: 0, }). Return(requests, nil) @@ -420,7 +434,11 @@ func TestHandlers_GetRequests(t *testing.T) { require.NoError(t, err) h.Repository.MockRequestRepository. EXPECT(). - GetRequests(c.Request().Context(), model.RequestQuery{CreatedBy: &request.CreatedBy}). + GetRequests(c.Request().Context(), model.RequestQuery{ + Limit: 100, + Offset: 0, + CreatedBy: &request.CreatedBy}, + ). Return(modelRequests, nil) err = h.Handlers.GetRequests(c) if !assert.NoError(t, err) { @@ -483,7 +501,10 @@ func TestHandlers_GetRequests(t *testing.T) { resErr := errors.New("Failed to get requests.") h.Repository.MockRequestRepository. EXPECT(). - GetRequests(c.Request().Context(), model.RequestQuery{}). + GetRequests(c.Request().Context(), model.RequestQuery{ + Limit: 100, + Offset: 0, + }). Return(nil, resErr) err = h.Handlers.GetRequests(c) diff --git a/router/transaction.go b/router/transaction.go index ec374eb4..9e1def8b 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -94,7 +94,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { if offsetQuery := c.QueryParam("offset"); offsetQuery != "" { offsetI, err := strconv.Atoi(offsetQuery) if err != nil { - h.Logger.Info("could not parse limit as integer", zap.Error(err)) + h.Logger.Info("could not parse offset as integer", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if offsetI < 0 {