Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requestsにLimitとOffsetパラメータを追加 #806

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -157,6 +158,38 @@
}
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

Check warning on line 175 in router/request.go

View check run for this annotation

Codecov / codecov/patch

router/request.go#L163-L175

Added lines #L163 - L175 were not covered by tests
}
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

Check warning on line 191 in router/request.go

View check run for this annotation

Codecov / codecov/patch

router/request.go#L179-L191

Added lines #L179 - L191 were not covered by tests
}
var tag *string
if c.QueryParam("tag") != "" {
t := c.QueryParam("tag")
Expand All @@ -182,6 +215,8 @@
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 @@
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))

Check warning on line 97 in router/transaction.go

View check run for this annotation

Codecov / codecov/patch

router/transaction.go#L97

Added line #L97 was not covered by tests
return echo.NewHTTPError(http.StatusBadRequest, err)
}
if offsetI < 0 {
Expand Down
Loading