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

ページング処理の修正 #20

Merged
merged 1 commit into from Apr 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions graph/item.go
Expand Up @@ -97,16 +97,14 @@ func (g *Graph) GetItemsInPeriod(ctx context.Context, input model.InputItemsInPe
t := g.Client.Time

cursor := repository.ItemsInPeriodCursor{
Date: time.Now(),
CreatedAt: time.Now(),
ID: "",
ID: "",
UserID: "",
}
cursorDate := strings.Split(*input.After, "/")
if len(cursorDate) > 1 {
cursor = repository.ItemsInPeriodCursor{
Date: t.ParseInLocationTimezone(cursorDate[0]),
CreatedAt: t.ParseInLocationTimezone(cursorDate[1]),
ID: cursorDate[2],
UserID: cursorDate[0],
ID: cursorDate[1],
}
}

Expand All @@ -123,7 +121,7 @@ func (g *Graph) GetItemsInPeriod(ctx context.Context, input model.InputItemsInPe

ibpes = append(ibpes, &model.ItemsInPeriodEdge{
Node: items[index],
Cursor: i.Date.Format("2006-01-02T15:04:05+09:00") + "/" + i.CreatedAt.Format("2006-01-02T15:04:05+09:00") + "/" + i.ID,
Cursor: i.UserID + "/" + i.ID,
})
}

Expand Down
6 changes: 4 additions & 2 deletions graph/item_test.go
Expand Up @@ -83,6 +83,7 @@ func TestGetItemsInPeriod(t *testing.T) {
ID: "test1",
CategoryID: 1,
Title: "test-title",
UserID: "test-user",
Date: date,
CreatedAt: date,
UpdatedAt: date,
Expand All @@ -100,11 +101,12 @@ func TestGetItemsInPeriod(t *testing.T) {
after := ""

iipe := []*model.ItemsInPeriodEdge{{
Cursor: "2019-01-01T00:00:00+09:00/2019-01-01T00:00:00+09:00/test1",
Cursor: "test-user/test1",
Node: &model.Item{
ID: "test1",
CategoryID: 1,
Title: "test-title",
UserID: "test-user",
Date: date,
CreatedAt: date,
UpdatedAt: date,
Expand All @@ -113,7 +115,7 @@ func TestGetItemsInPeriod(t *testing.T) {

result := &model.ItemsInPeriod{
PageInfo: &model.PageInfo{
EndCursor: "2019-01-01T00:00:00+09:00/2019-01-01T00:00:00+09:00/test1",
EndCursor: "test-user/test1",
HasNextPage: false,
},
Edges: iipe,
Expand Down
24 changes: 16 additions & 8 deletions repository/item.go
Expand Up @@ -2,7 +2,6 @@ package repository

import (
"context"
"log"
"time"

"cloud.google.com/go/firestore"
Expand Down Expand Up @@ -112,19 +111,22 @@ func (re *ItemRepository) GetItemsInDate(ctx context.Context, f *firestore.Clien
}

type ItemsInPeriodCursor struct {
Date time.Time
CreatedAt time.Time
ID string
ID string
UserID string
}

// GetItemsInPeriod 期間でアイテムを取得する
func (re *ItemRepository) GetItemsInPeriod(ctx context.Context, f *firestore.Client, userID string, startDate time.Time, endDate time.Time, first int, cursor ItemsInPeriodCursor) ([]*model.Item, error) {
var items []*model.Item
query := getItemCollection(f, userID).Where("Date", ">=", startDate).Where("Date", "<=", endDate).OrderBy("Date", firestore.Asc).OrderBy("CreatedAt", firestore.Asc).OrderBy("ID", firestore.Asc)

log.Println(cursor.ID)
if cursor.ID != "" {
query = query.StartAfter(cursor.Date, cursor.CreatedAt, cursor.ID)
ds, err := getItemCollection(f, cursor.UserID).Doc(cursor.ID).Get(ctx)
if err != nil {
return nil, err
}

query = query.StartAfter(ds)
}

matchItem := query.Limit(first).Documents(ctx)
Expand All @@ -147,10 +149,16 @@ func (re *ItemRepository) GetItemsInPeriod(ctx context.Context, f *firestore.Cli
// GetItemUserMultipleInPeriod 期間でアイテムを取得する
func (re *ItemRepository) GetItemUserMultipleInPeriod(ctx context.Context, f *firestore.Client, userID []string, startDate time.Time, endDate time.Time, first int, cursor ItemsInPeriodCursor) ([]*model.Item, error) {
var items []*model.Item
query := f.CollectionGroup("items").Where("UserID", "in", userID).Where("Date", ">=", startDate).Where("Date", "<=", endDate).OrderBy("Date", firestore.Asc).OrderBy("CreatedAt", firestore.Asc).OrderBy("ID", firestore.Asc)

query := f.CollectionGroup("items").Where("UserID", "in", userID).Where("Date", ">=", startDate).Where("Date", "<=", endDate).OrderBy("Date", firestore.Asc).OrderBy("CreatedAt", firestore.Asc)

if cursor.ID != "" {
query = query.StartAfter(cursor.Date, cursor.CreatedAt, cursor.ID)
ds, err := getItemCollection(f, cursor.UserID).Doc(cursor.ID).Get(ctx)
if err != nil {
return nil, err
}

query = query.StartAfter(ds)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StartAfterにSnapshotを設定することで正しいカーソルの指定ができる

}

matchItem := query.Limit(first).Documents(ctx)
Expand Down