-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
162 lines (121 loc) · 3.33 KB
/
board.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package persistence
import (
"context"
"google.golang.org/api/iterator"
"github.com/calmato/gran/api/todo/internal/domain"
"github.com/calmato/gran/api/todo/internal/domain/repository"
"github.com/calmato/gran/api/todo/lib/firebase/firestore"
)
type boardPersistence struct {
firestore *firestore.Firestore
}
// NewBoardPersistence - BoardRepositoryの生成
func NewBoardPersistence(fs *firestore.Firestore) repository.BoardRepository {
return &boardPersistence{
firestore: fs,
}
}
func (bp *boardPersistence) Index(ctx context.Context, groupID string) ([]*domain.Board, error) {
bs := make([]*domain.Board, 0)
boardCollection := getBoardCollection(groupID)
iter := bp.firestore.GetAll(ctx, boardCollection)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
b := &domain.Board{}
err = doc.DataTo(b)
if err != nil {
return nil, err
}
bs = append(bs, b)
}
return bs, nil
}
func (bp *boardPersistence) Show(ctx context.Context, groupID string, boardID string) (*domain.Board, error) {
boardCollection := getBoardCollection(groupID)
doc, err := bp.firestore.Get(ctx, boardCollection, boardID)
if err != nil {
return nil, err
}
b := &domain.Board{}
err = doc.DataTo(b)
if err != nil {
return nil, err
}
return b, nil
}
func (bp *boardPersistence) Create(ctx context.Context, b *domain.Board) error {
boardCollection := getBoardCollection(b.GroupID)
if err := bp.firestore.Set(ctx, boardCollection, b.ID, b); err != nil {
return err
}
return nil
}
func (bp *boardPersistence) Update(ctx context.Context, b *domain.Board) error {
boardCollection := getBoardCollection(b.GroupID)
if err := bp.firestore.Set(ctx, boardCollection, b.ID, b); err != nil {
return err
}
return nil
}
func (bp *boardPersistence) IndexBoardList(
ctx context.Context, groupID string, boardID string,
) ([]*domain.BoardList, error) {
bls := make([]*domain.BoardList, 0)
boardListCollection := getBoardListCollection(groupID, boardID)
iter := bp.firestore.GetAll(ctx, boardListCollection)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
bl := &domain.BoardList{}
err = doc.DataTo(bl)
if err != nil {
return nil, err
}
bls = append(bls, bl)
}
return bls, nil
}
func (bp *boardPersistence) ShowBoardList(
ctx context.Context, groupID string, boardID string, boardListID string,
) (*domain.BoardList, error) {
boardListCollection := getBoardListCollection(groupID, boardID)
doc, err := bp.firestore.Get(ctx, boardListCollection, boardListID)
if err != nil {
return nil, err
}
bl := &domain.BoardList{}
err = doc.DataTo(bl)
if err != nil {
return nil, err
}
return bl, nil
}
func (bp *boardPersistence) CreateBoardList(
ctx context.Context, groupID string, boardID string, bl *domain.BoardList,
) error {
boardListCollection := getBoardListCollection(groupID, boardID)
if err := bp.firestore.Set(ctx, boardListCollection, bl.ID, bl); err != nil {
return err
}
return nil
}
func (bp *boardPersistence) UpdateBoardList(
ctx context.Context, groupID string, boardID string, bl *domain.BoardList,
) error {
boardListCollection := getBoardListCollection(groupID, boardID)
if err := bp.firestore.Set(ctx, boardListCollection, bl.ID, bl); err != nil {
return err
}
return nil
}