/
mongo.go
98 lines (74 loc) · 1.95 KB
/
mongo.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
package persistence
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"github.com/jbenet/go-base58"
"github.com/science-engineering-art/gotify/peer/repository"
)
type MongoDb struct {
repository repository.MongoRepository
}
func NewMongoDb(database, collection, mongoDbIP string) (s *MongoDb) {
mongoDbUri := fmt.Sprintf("mongodb://user:password@%s:27017/?maxPoolSize=20&w=majority", mongoDbIP)
//fmt.Println("Trying to connect...", mongoDbIP)
client, err := mongo.NewClient(options.Client().ApplyURI(mongoDbUri))
if err != nil {
log.Fatal(err)
}
ctx := context.TODO()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
if err := client.Ping(ctx, readpref.Primary()); err != nil {
panic(err)
}
//fmt.Println("MongoDb successfully connected to", mongoDbIP)
// Collections
songCollection := client.Database(database).Collection(collection)
songRepo := repository.NewMongoRepository(songCollection)
newMongo := MongoDb{}
newMongo.repository = songRepo
return &newMongo
}
func (s *MongoDb) Create(key []byte, data *[]byte) error {
b64 := base58.Encode(key)
err := s.repository.CreateSong(b64, data)
if err != nil {
return err
}
return nil
}
func (s *MongoDb) Read(key []byte, start int64, end int64) (data *[]byte, err error) {
b64 := base58.Encode(key)
objID, err := primitive.ObjectIDFromHex(b64)
if err != nil {
return nil, err
}
song, err := s.repository.GetSongById(&objID)
if err != nil {
return nil, err
}
response := song.RawSong[start:end]
return &response, nil
}
func (s *MongoDb) Delete(key []byte) error {
b64 := base58.Encode(key)
objID, err := primitive.ObjectIDFromHex(b64)
if err != nil {
return err
}
s.repository.RemoveSongById(&objID)
if err != nil {
return err
}
return nil
}
func (rdb *MongoDb) GetKeys() [][]byte {
return nil
}