mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Added getDocuments + refactor
This commit is contained in:
+20
-6
@@ -2,6 +2,7 @@ package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
@@ -22,18 +23,31 @@ func getNewClient() *mongo.Client {
|
||||
return client
|
||||
}
|
||||
|
||||
func pushDocument(location Location, document interface{}) error {
|
||||
func getCollection(location Location) *mongo.Collection {
|
||||
client := getNewClient()
|
||||
defer client.Disconnect(context.TODO())
|
||||
return client.Database(location.Database).Collection(location.Collection)
|
||||
}
|
||||
|
||||
coll := client.Database(location.Database).Collection(location.Collection)
|
||||
_, err := coll.InsertOne(context.TODO(), document)
|
||||
func pushDocument(location Location, document interface{}) error {
|
||||
collection := getCollection(location)
|
||||
_, err := collection.InsertOne(context.TODO(), document)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func getDocument(database string, collection string) {
|
||||
// }
|
||||
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
||||
collection := getCollection(location)
|
||||
var results [][]byte
|
||||
|
||||
cursor, err := collection.Find(context.TODO(), filter)
|
||||
if err != nil {
|
||||
return results, errors.New("something is wrong with filter to find document")
|
||||
}
|
||||
for cursor.TryNext(context.TODO()) {
|
||||
results = append(results, cursor.Current)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user