This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
transfers: read various filter params in http request #474
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package route | ||
|
||
import ( | ||
"math" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
maxLimit int64 = 1000 | ||
) | ||
|
||
// ReadOffset returns the "offset" query param from a request or zero if it's missing. | ||
func ReadOffset(r *http.Request) int64 { | ||
return readIntQueryParam(r, "offset", math.MaxInt64) | ||
} | ||
|
||
// ReadLimit returns the "limit" query param from a request or zero if it's missing. | ||
func ReadLimit(r *http.Request) int64 { | ||
return readIntQueryParam(r, "limit", maxLimit) | ||
} | ||
|
||
func readIntQueryParam(r *http.Request, key string, max int64) int64 { | ||
if v := r.URL.Query().Get(key); v != "" { | ||
limit, _ := strconv.ParseInt(v, 10, 32) | ||
if limit > max { | ||
return max | ||
} | ||
return limit | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package route | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestReadLimit(t *testing.T) { | ||
req := makeRequest(t, "http://localhost:8082/transfers?limit=27") | ||
if limit := ReadLimit(req); limit != 27 { | ||
t.Errorf("limit=%d", limit) | ||
} | ||
|
||
req = makeRequest(t, "http://localhost:8082/transfers?limit=2700") | ||
if limit := ReadLimit(req); limit != 1000 { | ||
t.Errorf("limit=%d", limit) | ||
} | ||
|
||
req = makeRequest(t, "http://localhost:8082/transfers") | ||
if limit := ReadLimit(req); limit != 0 { | ||
t.Errorf("limit=%d", limit) | ||
} | ||
} | ||
|
||
func TestReadOffset(t *testing.T) { | ||
req := makeRequest(t, "http://localhost:8082/transfers?offset=27") | ||
if offset := ReadOffset(req); offset != 27 { | ||
t.Errorf("offset=%d", offset) | ||
} | ||
|
||
req = makeRequest(t, "http://localhost:8082/transfers") | ||
if offset := ReadOffset(req); offset != 0 { | ||
t.Errorf("offset=%d", offset) | ||
} | ||
} | ||
|
||
func makeRequest(t *testing.T, in string) *http.Request { | ||
u, _ := url.Parse(in) | ||
return &http.Request{ | ||
URL: u, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import ( | |
) | ||
|
||
type Repository interface { | ||
getUserTransfers(userID id.User) ([]*model.Transfer, error) | ||
getUserTransfers(userID id.User, params transferFilterParams) ([]*model.Transfer, error) | ||
getUserTransfer(id id.Transfer, userID id.User) (*model.Transfer, error) | ||
UpdateTransferStatus(id id.Transfer, status model.TransferStatus) error | ||
|
||
|
@@ -60,15 +60,17 @@ func (r *SQLRepo) Close() error { | |
return r.db.Close() | ||
} | ||
|
||
func (r *SQLRepo) getUserTransfers(userID id.User) ([]*model.Transfer, error) { | ||
query := `select transfer_id from transfers where user_id = ? and deleted_at is null` | ||
func (r *SQLRepo) getUserTransfers(userID id.User, params transferFilterParams) ([]*model.Transfer, error) { | ||
query := `select transfer_id from transfers | ||
where user_id = ? and created_at >= ? and created_at <= ? and deleted_at is null | ||
order by created_at desc limit ? offset ?;` | ||
stmt, err := r.db.Prepare(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer stmt.Close() | ||
|
||
rows, err := stmt.Query(userID) | ||
rows, err := stmt.Query(userID, params.StartDate, params.EndDate, params.Limit, params.Offset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before we query is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's EndDate: time.Now().Add(24 * time.Hour), In the future we need to support #447 (scheduled transfers) where I think this endpoint should order by scheduled_at or created_at (coalesce the two) because transfers without scheduled_at are to be uploaded/posted on the current day. |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package util | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
YYMMDDTimeFormat = "2006-01-02" | ||
) | ||
|
||
// FirstParsedTime attempts to parse v with all provided formats in order. | ||
// The first parsed time.Time that doesn't error is returned. | ||
func FirstParsedTime(v string, formats ...string) time.Time { | ||
for i := range formats { | ||
if tt, err := time.Parse(formats[i], v); err == nil { | ||
return tt | ||
} | ||
} | ||
return time.Time{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2020 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFirstParsedTime(t *testing.T) { | ||
tt := FirstParsedTime("2020-04-07") | ||
if !tt.IsZero() { | ||
t.Errorf("expected zero, got %v", tt) | ||
} | ||
|
||
tt = FirstParsedTime("2020-04-07", YYMMDDTimeFormat) | ||
if v := tt.Format(YYMMDDTimeFormat); v != "2020-04-07" { | ||
t.Errorf("got %v", v) | ||
} | ||
|
||
tt = FirstParsedTime(time.Now().Format(time.RFC3339), YYMMDDTimeFormat) | ||
if !tt.IsZero() { | ||
t.Errorf("expected zero, got %v", tt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have an index for
created_at
thatsdesc
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that itsignore me lol doesn't apply here as I was thinking more about it,X or equal to
Is it fine to get duplicates if they query this by sequential dates?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no index on
created_at desc
for transfers. We should add one... Thanks for the catch