-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_repository.go
80 lines (66 loc) · 1.74 KB
/
project_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package repository
import (
"cloud.google.com/go/datastore"
"github.com/hayashiki/onestep/pkg/model"
"google.golang.org/api/iterator"
"context"
"fmt"
)
//go:generate mockgen -source user_repository.go -destination mock_repo/user_repository.go
type ProjectRepository interface {
List(cursor string, limit int) ([]*model.Project, string, error)
Put(project *model.Project) error
}
type projectRepository struct {
dsClient *datastore.Client
}
func (r *projectRepository) List(cursor string, limit int) ([]*model.Project, string, error) {
q := datastore.NewQuery(model.ProjectKind)
if cursor != "" {
dsCursor, err := datastore.DecodeCursor(cursor)
if err != nil {
}
q = q.Start(dsCursor)
}
q = q.Limit(limit)
var el []*model.Project
ctx := context.Background()
it := r.dsClient.Run(ctx, q)
for {
var e model.Project
if _, err := it.Next(&e); err == iterator.Done {
break
} else if err != nil {
fmt.Errorf("fail err=%v", err)
}
e.ID = int(e.Key.ID)
el = append(el, &e)
}
nextCursor, err := it.Cursor()
if err != nil {
return nil, "", err
}
return el, nextCursor.String(), nil
}
func (r *projectRepository) key() *datastore.Key {
return datastore.IncompleteKey(model.ProjectKind, nil)
}
func NewProjectRepository(client *datastore.Client) ProjectRepository {
return &projectRepository{dsClient: client}
}
func (r *projectRepository) Put(project *model.Project) error {
ctx := context.Background()
_, err := r.dsClient.Put(ctx, r.key(), project)
if err != nil {
return err
}
return nil
}
//func (r *userRepository) Get(ctx context.Context, id string) (*user.User, error) {
// dst := &model.User{}
// err := r.dsClient.Get(ctx, repo.key(id), dst)
// if err != nil {
// return nil, err
// }
// return dst, nil
//}