-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvite.go
115 lines (88 loc) · 3.11 KB
/
invite.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
package repository
import (
"context"
"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"
)
//go:generate moq -out=moq/invite.go -pkg=moqs . InviteRepositoryInterface
type InviteRepositoryInterface interface {
Create(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, i *model.Invite) error
Delete(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, code string) error
DeleteByUserID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error
Find(ctx context.Context, f *firestore.Client, code string) (*model.Invite, error)
FindByUserID(ctx context.Context, f *firestore.Client, userID string) (*model.Invite, error)
}
type InviteRepository struct {
}
func NewInviteRepository() InviteRepositoryInterface {
return &InviteRepository{}
}
// Create 招待を作成する
func (re *InviteRepository) Create(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, i *model.Invite) error {
ref := f.Collection("invites").Doc(i.Code)
err := tx.Set(ref, i)
if err != nil {
return ce.CustomError(err)
}
return nil
}
// Delete アイテムを削除する
func (re *InviteRepository) Delete(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, code string) error {
ref := f.Collection("invites").Doc(code)
err := tx.Delete(ref)
if err != nil {
return ce.CustomError(err)
}
return nil
}
// FindByUserID ユーザーIDから取得する
func (re *InviteRepository) FindByUserID(ctx context.Context, f *firestore.Client, userID string) (*model.Invite, error) {
matchItem := f.Collection("invites").Where("UserID", "==", userID).OrderBy("CreatedAt", firestore.Desc).Limit(1).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return nil, ce.CustomError(err)
}
if len(docs) == 0 {
return &model.Invite{}, nil
}
var invite *model.Invite
if err = docs[0].DataTo(&invite); err != nil {
return nil, ce.CustomError(err)
}
return invite, nil
}
// Find 取得する
func (re *InviteRepository) Find(ctx context.Context, f *firestore.Client, code string) (*model.Invite, error) {
var i *model.Invite
ds, err := f.Collection("invites").Doc(code).Get(ctx)
if err != nil {
if GrpcErrorStatusCode(err) == codes.InvalidArgument || GrpcErrorStatusCode(err) == codes.NotFound {
return &model.Invite{}, nil
}
return i, ce.CustomError(err)
}
if err = ds.DataTo(&i); err != nil {
return nil, ce.CustomError(err)
}
return i, ce.CustomError(err)
}
// DeleteByUserID ユーザーIDから削除する
func (re *InviteRepository) DeleteByUserID(ctx context.Context, f *firestore.Client, tx *firestore.Transaction, userID string) error {
matchItem := f.Collection("invites").Where("UserID", "==", 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
}