From 784b869b4a42793bb3601d78e8044c6288d82aac Mon Sep 17 00:00:00 2001 From: rei Date: Sat, 9 Nov 2024 02:04:44 +0900 Subject: [PATCH 1/5] =?UTF-8?q?requests=E3=81=ABLimit=E3=81=A8Offset?= =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=82=BF=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit requests_testは未対応 --- docs/swagger.yaml | 2 ++ go.sum | 12 ++++++++++++ model/request.go | 2 ++ model/request_impl.go | 2 ++ router/request.go | 29 +++++++++++++++++++++++++++++ router/transaction.go | 2 +- 6 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 5035e029..fe1e2f9b 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" responses: diff --git a/go.sum b/go.sum index 8a4cc276..6644a7e5 100644 --- a/go.sum +++ b/go.sum @@ -51,18 +51,26 @@ 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= github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -85,6 +93,8 @@ golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= @@ -93,6 +103,8 @@ golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/model/request.go b/model/request.go index 13d424d1..d38de723 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 } diff --git a/model/request_impl.go b/model/request_impl.go index acc17d54..c4ed15d9 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 5ac87943..65fb980d 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,32 @@ 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") @@ -173,6 +200,8 @@ func (h Handlers) GetRequests(c echo.Context) error { Status: ss, Since: since, Until: until, + Limit: limit, + Offset: offset, Tag: tag, Group: group, } diff --git a/router/transaction.go b/router/transaction.go index 8e6e26b5..0028830c 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -91,7 +91,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 { From ef85c50a9c70fc236221979e57697f10b1ff591c Mon Sep 17 00:00:00 2001 From: rei Date: Sat, 28 Dec 2024 21:32:56 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20RequestQuery=20=E3=81=AE=20CreatedBy?= =?UTF-8?q?=20=E3=81=8C=E5=89=8A=E9=99=A4=E3=81=95=E3=82=8C=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=9F=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/request.go | 19 ++++++++++--------- router/request.go | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/model/request.go b/model/request.go index d38de723..94460dc2 100644 --- a/model/request.go +++ b/model/request.go @@ -59,13 +59,14 @@ type RequestDetail struct { } type RequestQuery struct { - Sort *string - Target *uuid.UUID - Status *string - Since *time.Time - Until *time.Time - Limit int - Offset int - Tag *string - Group *string + Sort *string + Target *uuid.UUID + Status *string + Since *time.Time + Until *time.Time + Limit int + Offset int + Tag *string + Group *string + CreatedBy *uuid.UUID } diff --git a/router/request.go b/router/request.go index c9f3d79a..fc722b64 100644 --- a/router/request.go +++ b/router/request.go @@ -204,15 +204,16 @@ func (h Handlers) GetRequests(c echo.Context) error { cratedBy = &u } query := model.RequestQuery{ - Sort: sort, - Target: target, - Status: ss, - Since: since, - Until: until, - Limit: limit, - Offset: offset, - Tag: tag, - Group: group, + Sort: sort, + Target: target, + Status: ss, + Since: since, + Until: until, + Limit: limit, + Offset: offset, + Tag: tag, + Group: group, + CreatedBy: cratedBy, } modelrequests, err := h.Repository.GetRequests(ctx, query) From 8bc772fc1e678a1cde92b4b6b143956904a2bfcf Mon Sep 17 00:00:00 2001 From: rei Date: Sat, 28 Dec 2024 21:46:17 +0900 Subject: [PATCH 3/5] =?UTF-8?q?style:=20=E8=A1=8C=E3=81=AE=E9=95=B7?= =?UTF-8?q?=E3=81=95=E3=81=AE=E5=88=B6=E9=99=90=E3=82=92=E6=BA=80=E3=81=9F?= =?UTF-8?q?=E3=81=99=E3=82=88=E3=81=86=E3=81=AB=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=9E=E3=83=83=E3=83=88=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/request.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/router/request.go b/router/request.go index fc722b64..56a3dda3 100644 --- a/router/request.go +++ b/router/request.go @@ -167,7 +167,10 @@ func (h Handlers) GetRequests(c echo.Context) error { } 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)) + return echo.NewHTTPError( + http.StatusBadRequest, + fmt.Errorf("negative limit(=%d) is invalid", limitI), + ) } limit = limitI } @@ -180,7 +183,10 @@ func (h Handlers) GetRequests(c echo.Context) error { } 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)) + return echo.NewHTTPError( + http.StatusBadRequest, + fmt.Errorf("negative offset(=%d) is invalid", offsetI), + ) } offset = offsetI } From ada68305e0d17c044a3c98fb3b803cb10440a218 Mon Sep 17 00:00:00 2001 From: rei Date: Sat, 28 Dec 2024 23:10:25 +0900 Subject: [PATCH 4/5] =?UTF-8?q?test:=20TestHandlers=5FGetRequests=20?= =?UTF-8?q?=E3=81=AE=20RequestQuery=20=E3=81=AB=20Limit=20=E3=81=A8=20Offs?= =?UTF-8?q?et=20=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/request_test.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) 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) From 30358354a92be3865a2ef64e31a677f837201c83 Mon Sep 17 00:00:00 2001 From: rei Date: Sun, 29 Dec 2024 15:58:10 +0900 Subject: [PATCH 5/5] chore: run go mod tidy --- go.sum | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go.sum b/go.sum index b1c7b640..430c6b0f 100644 --- a/go.sum +++ b/go.sum @@ -45,16 +45,12 @@ 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=