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

Transactions query #762

Merged
merged 5 commits into from
Jun 7, 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
20 changes: 20 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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/requestQuery"
Expand Down Expand Up @@ -1421,6 +1423,24 @@ components:
schema:
type: string
format: date
limitQuery:
name: limit
description: 取得する最大個数
required: false
in: query
schema:
type: integer
minimum: 0
default: 100
offsetQuery:
name: offset
description: スキップする個数
required: false
in: query
schema:
type: integer
minimum: 0
default: 0
tagQuery:
name: tag
description: タグ(複数の場合カンマ区切り)
Expand Down
2 changes: 2 additions & 0 deletions model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type TransactionQuery struct {
Target *string
Since *time.Time
Until *time.Time
Limit int
Offset int
Tag *string
Group *string
Request *uuid.UUID
Expand Down
2 changes: 2 additions & 0 deletions model/transaction_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (repo *EntRepository) GetTransactions(ctx context.Context, query Transactio
Where(transaction.CreatedAtLT(*query.Until))
}

transactionsq = transactionsq.Limit(query.Limit).Offset(query.Offset)

if query.Tag != nil {
transactionsq = transactionsq.
Where(transaction.HasTagWith(
Expand Down
30 changes: 30 additions & 0 deletions router/transaction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package router

import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -70,6 +72,32 @@ func (h Handlers) GetTransactions(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 limit 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 @@ -95,6 +123,8 @@ func (h Handlers) GetTransactions(c echo.Context) error {
Target: target,
Since: since,
Until: until,
Limit: limit,
Offset: offset,
Tag: tag,
Group: group,
Request: request,
Expand Down
20 changes: 16 additions & 4 deletions router/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func TestHandlers_GetTransactions(t *testing.T) {
Target: nil,
Since: nil,
Until: nil,
Limit: 100,
Offset: 0,
Tag: nil,
Group: nil,
}).
Expand Down Expand Up @@ -206,7 +208,9 @@ func TestHandlers_GetTransactions(t *testing.T) {
h.Repository.MockTransactionRepository.
EXPECT().
GetTransactions(c.Request().Context(), model.TransactionQuery{
Sort: &sortQuery,
Sort: &sortQuery,
Limit: 100,
Offset: 0,
}).
Return(txs, nil)

Expand Down Expand Up @@ -319,7 +323,9 @@ func TestHandlers_GetTransactions(t *testing.T) {
h.Repository.MockTransactionRepository.
EXPECT().
GetTransactions(c.Request().Context(), model.TransactionQuery{
Sort: &sortQuery,
Sort: &sortQuery,
Limit: 100,
Offset: 0,
}).
Return(txs, nil)

Expand Down Expand Up @@ -435,6 +441,8 @@ func TestHandlers_GetTransactions(t *testing.T) {
EXPECT().
GetTransactions(c.Request().Context(), model.TransactionQuery{
Target: &targetQuery,
Limit: 100,
Offset: 0,
}).
Return(txs, nil)

Expand Down Expand Up @@ -532,8 +540,10 @@ func TestHandlers_GetTransactions(t *testing.T) {
h.Repository.MockTransactionRepository.
EXPECT().
GetTransactions(c.Request().Context(), model.TransactionQuery{
Since: &since,
Until: &until,
Since: &since,
Until: &until,
Limit: 100,
Offset: 0,
}).
Return(txs, nil)

Expand Down Expand Up @@ -575,6 +585,8 @@ func TestHandlers_GetTransactions(t *testing.T) {
assert.Equal(t, string(resBody), strings.TrimRight(rec.Body.String(), "\n"))
}
})

// TODO: SuccessWithLimit, SuccessWithOffset
}

func TestHandlers_PostTransaction(t *testing.T) {
Expand Down