Skip to content

Commit

Permalink
Merge pull request #806 from traPtitech/feat/add_limit_offset_to_requ…
Browse files Browse the repository at this point in the history
…ests_parameters

requestsにLimitとOffsetパラメータを追加
  • Loading branch information
reiroop authored Dec 29, 2024
2 parents c3dbb3b + 3035835 commit 27d5da6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions model/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions model/request_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 35 additions & 0 deletions router/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down
35 changes: 28 additions & 7 deletions router/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion router/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 27d5da6

Please sign in to comment.