-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
134 lines (116 loc) · 3.2 KB
/
db.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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DB struct {
client *mongo.Client
}
type Meigen struct {
ID int `json:"id"`
Author string `json:"author"`
Text string `json:"text"`
}
func (db *DB) getURI() (uri string) {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
if uri = os.Getenv("DB_URI"); uri == "" {
log.Fatal("DB_URI is not set")
}
return uri
}
func (db *DB) Connect(ctx context.Context) (err error) {
opt := options.Client().ApplyURI(db.getURI())
if err = opt.Validate(); err != nil {
return err
}
db.client, err = mongo.Connect(ctx, opt)
if err != nil {
return err
}
return nil
}
func (db *DB) Disconnect(ctx context.Context) error {
return db.client.Disconnect(ctx)
}
func (db *DB) Ping(ctx context.Context) (err error) {
if err = db.client.Ping(ctx, nil); err != nil {
return err
}
fmt.Println("Ping to MongoDB successful")
return nil
}
func (db *DB) AddMeigen(ctx context.Context, a string, t string) (err error) {
DB_NAME := os.Getenv("DB_NAME")
COLLECTION_NAME := os.Getenv("DB_COLLECTION_NAME")
collection := db.client.Database(DB_NAME).Collection(COLLECTION_NAME)
m := Meigen{}
m.ID = db.CountMeigen(ctx) + 1
m.Author = a
m.Text = t
_, err = collection.InsertOne(ctx, m)
if err != nil {
return err
}
return nil
}
func (db *DB) CountMeigen(ctx context.Context) int {
DB_NAME := os.Getenv("DB_NAME")
COLLECTION_NAME := os.Getenv("DB_COLLECTION_NAME")
collection := db.client.Database(DB_NAME).Collection(COLLECTION_NAME)
count, _ := collection.CountDocuments(ctx, bson.D{})
return int(count)
}
// TextとAuthor似すぎ、まとめられないかな
func (db *DB) SearchMeigenFromAuthor(ctx context.Context, a string) (result []Meigen, err error) {
DB_NAME := os.Getenv("DB_NAME")
COLLECTION_NAME := os.Getenv("DB_COLLECTION_NAME")
collection := db.client.Database(DB_NAME).Collection(COLLECTION_NAME)
cursor, err := collection.Find(ctx, bson.D{{"author", a}})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var m Meigen
if err = cursor.Decode(&m); err != nil {
return nil, err
}
result = append(result, m)
}
return result, nil
}
func (db *DB) SearchMeigenFromText(ctx context.Context, t string) (result []Meigen, err error) {
DB_NAME := os.Getenv("DB_NAME")
COLLECTION_NAME := os.Getenv("DB_COLLECTION_NAME")
collection := db.client.Database(DB_NAME).Collection(COLLECTION_NAME)
cursor, err := collection.Find(ctx, bson.D{{"text", t}})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var m Meigen
if err = cursor.Decode(&m); err != nil {
return nil, err
}
result = append(result, m)
}
return result, nil
}
func (db *DB) SearchMeigenFromID(ctx context.Context, id int) (result Meigen, err error) {
DB_NAME := os.Getenv("DB_NAME")
COLLECTION_NAME := os.Getenv("DB_COLLECTION_NAME")
collection := db.client.Database(DB_NAME).Collection(COLLECTION_NAME)
if err = collection.FindOne(ctx, bson.D{{"id", id}}).Decode(&result); err != nil {
return Meigen{}, err
}
return result, nil
}