1
|
|
|
package mongo |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"os" |
6
|
|
|
|
7
|
|
|
"go.mongodb.org/mongo-driver/bson" |
8
|
|
|
"go.mongodb.org/mongo-driver/mongo" |
9
|
|
|
"go.mongodb.org/mongo-driver/mongo/options" |
10
|
|
|
|
11
|
|
|
"github.com/rfinochi/golang-workshop-todo/pkg/common" |
12
|
|
|
"github.com/rfinochi/golang-workshop-todo/pkg/models" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// ItemRepository godoc |
16
|
|
|
type ItemRepository struct { |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// GetItems godoc |
20
|
|
|
func (ItemRepository) GetItems() (items []models.Item, err error) { |
21
|
|
|
ctx, client, err := connnect() |
22
|
|
|
if err != nil { |
23
|
|
|
return |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
collection := client.Database("todo").Collection("items") |
27
|
|
|
cursor, err := collection.Find(ctx, bson.M{}) |
28
|
|
|
|
29
|
|
|
if err == nil { |
30
|
|
|
defer cursor.Close(ctx) |
31
|
|
|
for cursor.Next(ctx) { |
32
|
|
|
var oneItem models.Item |
33
|
|
|
cursor.Decode(&oneItem) |
34
|
|
|
items = append(items, oneItem) |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
disconnect(ctx, client) |
39
|
|
|
|
40
|
|
|
return |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// GetItem godoc |
44
|
|
|
func (ItemRepository) GetItem(id int) (item models.Item, err error) { |
45
|
|
|
ctx, client, err := connnect() |
46
|
|
|
if err != nil { |
47
|
|
|
return |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
options := options.Find() |
51
|
|
|
options.SetLimit(1) |
52
|
|
|
|
53
|
|
|
collection := client.Database("todo").Collection("items") |
54
|
|
|
cursor, err := collection.Find(ctx, models.Item{ID: id}, options) |
55
|
|
|
|
56
|
|
|
if err == nil { |
57
|
|
|
defer cursor.Close(ctx) |
58
|
|
|
for cursor.Next(ctx) { |
59
|
|
|
cursor.Decode(&item) |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
disconnect(ctx, client) |
64
|
|
|
|
65
|
|
|
return |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// CreateItem godoc |
69
|
|
|
func (ItemRepository) CreateItem(newItem models.Item) (err error) { |
70
|
|
|
ctx, client, err := connnect() |
71
|
|
|
if err != nil { |
72
|
|
|
return |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
collection := client.Database("todo").Collection("items") |
76
|
|
|
_, err = collection.InsertOne(ctx, newItem) |
77
|
|
|
|
78
|
|
|
disconnect(ctx, client) |
79
|
|
|
|
80
|
|
|
return |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// UpdateItem godoc |
84
|
|
|
func (ItemRepository) UpdateItem(item models.Item) (err error) { |
85
|
|
|
update := bson.M{"$set": bson.M{"title": item.Title, "isdone": item.IsDone}} |
86
|
|
|
|
87
|
|
|
ctx, client, err := connnect() |
88
|
|
|
if err != nil { |
89
|
|
|
return |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
collection := client.Database("todo").Collection("items") |
93
|
|
|
_, err = collection.UpdateOne(ctx, models.Item{ID: item.ID}, update) |
94
|
|
|
|
95
|
|
|
disconnect(ctx, client) |
96
|
|
|
|
97
|
|
|
return |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// DeleteItem godoc |
101
|
|
|
func (ItemRepository) DeleteItem(id int) (err error) { |
102
|
|
|
ctx, client, err := connnect() |
103
|
|
|
if err != nil { |
104
|
|
|
return |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
collection := client.Database("todo").Collection("items") |
108
|
|
|
_, err = collection.DeleteMany(ctx, models.Item{ID: id}) |
109
|
|
|
|
110
|
|
|
disconnect(ctx, client) |
111
|
|
|
|
112
|
|
|
return |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
func connnect() (ctx context.Context, client *mongo.Client, err error) { |
116
|
|
|
ctx = context.Background() |
117
|
|
|
client, err = mongo.Connect(ctx, options.Client().ApplyURI(getURI())) |
118
|
|
|
if err != nil { |
119
|
|
|
return |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
func disconnect(ctx context.Context, client *mongo.Client) { |
126
|
|
|
defer client.Disconnect(ctx) |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
func getURI() (uri string) { |
130
|
|
|
var ok bool |
131
|
|
|
|
132
|
|
|
if uri, ok = os.LookupEnv(common.RepositoryMongoURIEnvVarName); !ok { |
133
|
|
|
uri = common.RepositoryMongoURIDefault |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return |
137
|
|
|
} |
138
|
|
|
|