-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelationship_request.go
177 lines (141 loc) · 5.28 KB
/
relationship_request.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package repository
import (
"context"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/grpc/codes"
"github.com/wheatandcat/memoir-backend/graph/model"
ce "github.com/wheatandcat/memoir-backend/usecase/custom_error"
)
const (
RelationshipRequestStatusRequest = 1
RelationshipRequestStatusOK = 2
RelationshipRequestStatusNG = 3
)
//go:generate moq -out=moq/relationship_request.go -pkg=moqs . RelationshipRequestInterface
type RelationshipRequestInterface interface {
Create(ctx context.Context, f *firestore.Client, i *model.RelationshipRequest) error
Update(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, i *model.RelationshipRequest) error
DeleteByFollowedID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error
DeleteByFollowerID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error
Find(ctx context.Context, f *firestore.Client, i *model.RelationshipRequest) (*model.RelationshipRequest, error)
FindByFollowedID(ctx context.Context, f *firestore.Client, userID string, first int, cursor RelationshipRequestCursor) ([]*model.RelationshipRequest, error)
}
type RelationshipRequestRepository struct {
}
func NewRelationshipRequestRepository() RelationshipRequestInterface {
return &RelationshipRequestRepository{}
}
type RelationshipRequestCursor struct {
FollowerID string
FollowedID string
}
type RelationshipRequestData struct {
ID string
FollowerID string
FollowedID string
Status int
CreatedAt time.Time
UpdatedAt time.Time
}
// Create 作成する
func (re *RelationshipRequestRepository) Create(ctx context.Context, f *firestore.Client, i *model.RelationshipRequest) error {
rrd := RelationshipRequestData{
ID: i.ID,
FollowerID: i.FollowerID,
FollowedID: i.FollowedID,
Status: i.Status,
CreatedAt: i.CreatedAt,
UpdatedAt: i.UpdatedAt,
}
_, err := f.Collection("relationshipRequests").Doc(i.FollowerID+"_"+i.FollowedID).Set(ctx, rrd)
return ce.CustomError(err)
}
// Update 更新する
func (re *RelationshipRequestRepository) Update(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, i *model.RelationshipRequest) error {
var u []firestore.Update
if i.Status != 0 {
u = append(u, firestore.Update{Path: "Status", Value: i.Status})
}
u = append(u, firestore.Update{Path: "UpdatedAt", Value: i.UpdatedAt})
ref := f.Collection("relationshipRequests").Doc(i.FollowerID + "_" + i.FollowedID)
err := tx.Update(ref, u)
if err != nil {
return ce.CustomError(err)
}
return nil
}
// DeleteByFollowedID ユーザーIDから削除する
func (re *RelationshipRequestRepository) DeleteByFollowedID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error {
matchItem := f.Collection("relationshipRequests").Where("FollowedID", "==", userID).OrderBy("CreatedAt", firestore.Desc).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return ce.CustomError(err)
}
if len(docs) == 0 {
return nil
}
for _, doc := range docs {
err := tx.Delete(doc.Ref)
if err != nil {
return ce.CustomError(err)
}
}
return nil
}
// DeleteByFollowerID ユーザーIDから削除する
func (re *RelationshipRequestRepository) DeleteByFollowerID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error {
matchItem := f.Collection("relationshipRequests").Where("FollowerID", "==", userID).OrderBy("CreatedAt", firestore.Desc).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return ce.CustomError(err)
}
if len(docs) == 0 {
return nil
}
for _, doc := range docs {
err := tx.Delete(doc.Ref)
if err != nil {
return ce.CustomError(err)
}
}
return nil
}
// Find 取得する
func (re *RelationshipRequestRepository) Find(ctx context.Context, f *firestore.Client, i *model.RelationshipRequest) (*model.RelationshipRequest, error) {
var rr *model.RelationshipRequest
ds, err := f.Collection("relationshipRequests").Doc(i.FollowerID + "_" + i.FollowedID).Get(ctx)
if err != nil {
if GrpcErrorStatusCode(err) == codes.InvalidArgument || GrpcErrorStatusCode(err) == codes.NotFound {
return &model.RelationshipRequest{}, nil
}
return i, ce.CustomError(err)
}
err = ds.DataTo(&rr)
return rr, ce.CustomError(err)
}
// FindByFollowedID ページングで取得する
func (re *RelationshipRequestRepository) FindByFollowedID(ctx context.Context, f *firestore.Client, userID string, first int, cursor RelationshipRequestCursor) ([]*model.RelationshipRequest, error) {
query := f.Collection("relationshipRequests").Where("FollowedID", "==", userID).Where("Status", "==", RelationshipRequestStatusRequest).OrderBy("CreatedAt", firestore.Desc)
if cursor.FollowerID != "" {
ds, err := f.Collection("relationshipRequests").Doc(cursor.FollowerID + "_" + cursor.FollowedID).Get(ctx)
if err != nil {
return nil, ce.CustomError(err)
}
query = query.StartAfter(ds)
}
matchItem := query.Limit(first).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return nil, ce.CustomError(err)
}
items := make([]*model.RelationshipRequest, len(docs))
for i, doc := range docs {
var item *model.RelationshipRequest
if err = doc.DataTo(&item); err != nil {
return items, ce.CustomError(err)
}
items[i] = item
}
return items, nil
}