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 5 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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/ncw/swift v1.0.53 h1:luHjjTNtekIEvHg5KdAFIBaH7bWfNkefwFnpDffSIks=
github.com/ncw/swift v1.0.53/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
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