-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathitem_detail.go
84 lines (63 loc) · 2.18 KB
/
item_detail.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
package repository
import (
"context"
"cloud.google.com/go/firestore"
"github.com/wheatandcat/Peperomia/backend/domain"
)
// ItemDetailRepository is repository for itemDetail
type ItemDetailRepository struct {
}
// NewItemDetailRepository is Create new ItemDetailRepository
func NewItemDetailRepository() domain.ItemDetailRepository {
return &ItemDetailRepository{}
}
func getItemDetailDocID(uID string, itemID string, itemDetailID string) string {
doc := uID + "_" + itemID + "_" + itemDetailID
return doc
}
// Create アイテム詳細を作成する
func (re *ItemDetailRepository) Create(ctx context.Context, f *firestore.Client, i domain.ItemDetailRecord) error {
idDoc := getItemDetailDocID(i.UID, i.ItemID, i.ID)
_, err := f.Collection("itemDetails").Doc(idDoc).Set(ctx, i)
return err
}
// Update アイテム詳細を更新する
func (re *ItemDetailRepository) Update(ctx context.Context, f *firestore.Client, i domain.ItemDetailRecord) error {
idDoc := getItemDetailDocID(i.UID, i.ItemID, i.ID)
_, err := f.Collection("itemDetails").Doc(idDoc).Set(ctx, i)
return err
}
// Delete アイテム詳細を削除する
func (re *ItemDetailRepository) Delete(ctx context.Context, f *firestore.Client, i domain.ItemDetailRecord) error {
idDoc := getItemDetailDocID(i.UID, i.ItemID, i.ID)
_, err := f.Collection("itemDetails").Doc(idDoc).Delete(ctx)
return err
}
// DeleteByUID ユーザーIDから削除する
func (re *ItemDetailRepository) DeleteByUID(ctx context.Context, f *firestore.Client, uid string) error {
matchItem := f.Collection("itemDetails").Where("uid", "==", uid).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return err
}
for _, doc := range docs {
if _, err := doc.Ref.Delete(ctx); err != nil {
return err
}
}
return nil
}
// DeleteByItemID ItemIDから削除する
func (re *ItemDetailRepository) DeleteByItemID(ctx context.Context, f *firestore.Client, itemID string) error {
matchItem := f.Collection("itemDetails").Where("itemId", "==", itemID).Documents(ctx)
docs, err := matchItem.GetAll()
if err != nil {
return err
}
for _, doc := range docs {
if _, err := doc.Ref.Delete(ctx); err != nil {
return err
}
}
return nil
}