From 245ddbc137ab7aa2b54cc1e2beab3ba2fb545a21 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Wed, 18 Jan 2023 20:55:51 +0100 Subject: [PATCH] Database related operation files reorganization --- internal/database/database.go | 77 ----------------------------------- internal/database/delete.go | 19 +++++++++ internal/database/edit.go | 29 +++++++++++++ internal/database/get.go | 35 ++++++++++++++++ internal/database/push.go | 15 +++++++ 5 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 internal/database/delete.go create mode 100644 internal/database/edit.go create mode 100644 internal/database/get.go create mode 100644 internal/database/push.go diff --git a/internal/database/database.go b/internal/database/database.go index 0ad9853..1602d82 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -2,9 +2,7 @@ package database import ( "context" - "errors" - "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -14,11 +12,6 @@ type Location struct { Collection string } -type DocumentUpdate struct { - Filter gin.H `json:"filter"` - Update gin.H `json:"update"` -} - func GetNewClient() *mongo.Client { client, err := mongo.Connect( context.TODO(), @@ -28,73 +21,3 @@ func GetNewClient() *mongo.Client { } return client } - -func PushDocument(location Location, document interface{}) error { - client := GetNewClient() - collection := client.Database(location.Database).Collection(location.Collection) - defer client.Disconnect(context.TODO()) - - _, err := collection.InsertOne(context.TODO(), document) - if err != nil { - return err - } - return nil -} - -func GetDocuments(location Location, filter interface{}) ([][]byte, error) { - client := GetNewClient() - collection := client.Database(location.Database).Collection(location.Collection) - defer client.Disconnect(context.TODO()) - - var results [][]byte - - cursor, err := collection.Find(context.TODO(), filter) - if err != nil { - return results, errors.New("something is wrong with filter to find the document.") - } - for cursor.TryNext(context.TODO()) { - results = append(results, cursor.Current) - } - return results, nil -} - -func GetUniqueDocument(location Location, filter interface{}) ([]byte, error) { - client := GetNewClient() - collection := client.Database(location.Database).Collection(location.Collection) - defer client.Disconnect(context.TODO()) - - singleResult := collection.FindOne(context.TODO(), filter) - if singleResult.Err() != nil { - return nil, errors.New("no document was found, check the filter or the location.") - } - return singleResult.DecodeBytes() -} - -func DeleteDocument(location Location, filter interface{}) (int64, error) { - client := GetNewClient() - collection := client.Database(location.Database).Collection(location.Collection) - defer client.Disconnect(context.TODO()) - - result, err := collection.DeleteOne(context.TODO(), filter) - if err != nil { - return 0, errors.New("something is wrong with filter to delete the document.") - } - - return result.DeletedCount, nil -} - -func EditDocument(location Location, jsons DocumentUpdate) (int64, error) { - client := GetNewClient() - collection := client.Database(location.Database).Collection(location.Collection) - defer client.Disconnect(context.TODO()) - - result, err := collection.UpdateOne(context.TODO(), jsons.Filter, gin.H{"$set": jsons.Update}) - if err != nil { - return 0, err - } - if result.MatchedCount == 0 { - return 0, errors.New("cannot find document matching filter.") - } - - return result.ModifiedCount, nil -} diff --git a/internal/database/delete.go b/internal/database/delete.go new file mode 100644 index 0000000..0669b20 --- /dev/null +++ b/internal/database/delete.go @@ -0,0 +1,19 @@ +package database + +import ( + "context" + "errors" +) + +func DeleteDocument(location Location, filter interface{}) (int64, error) { + client := GetNewClient() + collection := client.Database(location.Database).Collection(location.Collection) + defer client.Disconnect(context.TODO()) + + result, err := collection.DeleteOne(context.TODO(), filter) + if err != nil { + return 0, errors.New("something is wrong with filter to delete the document.") + } + + return result.DeletedCount, nil +} diff --git a/internal/database/edit.go b/internal/database/edit.go new file mode 100644 index 0000000..11ae8db --- /dev/null +++ b/internal/database/edit.go @@ -0,0 +1,29 @@ +package database + +import ( + "context" + "errors" + + "github.com/gin-gonic/gin" +) + +type DocumentUpdate struct { + Filter gin.H `json:"filter"` + Update gin.H `json:"update"` +} + +func EditDocument(location Location, jsons DocumentUpdate) (int64, error) { + client := GetNewClient() + collection := client.Database(location.Database).Collection(location.Collection) + defer client.Disconnect(context.TODO()) + + result, err := collection.UpdateOne(context.TODO(), jsons.Filter, gin.H{"$set": jsons.Update}) + if err != nil { + return 0, err + } + if result.MatchedCount == 0 { + return 0, errors.New("cannot find document matching filter.") + } + + return result.ModifiedCount, nil +} diff --git a/internal/database/get.go b/internal/database/get.go new file mode 100644 index 0000000..e53e806 --- /dev/null +++ b/internal/database/get.go @@ -0,0 +1,35 @@ +package database + +import ( + "context" + "errors" +) + +func GetDocuments(location Location, filter interface{}) ([][]byte, error) { + client := GetNewClient() + collection := client.Database(location.Database).Collection(location.Collection) + defer client.Disconnect(context.TODO()) + + var results [][]byte + + cursor, err := collection.Find(context.TODO(), filter) + if err != nil { + return results, errors.New("something is wrong with filter to find the document.") + } + for cursor.TryNext(context.TODO()) { + results = append(results, cursor.Current) + } + return results, nil +} + +func GetUniqueDocument(location Location, filter interface{}) ([]byte, error) { + client := GetNewClient() + collection := client.Database(location.Database).Collection(location.Collection) + defer client.Disconnect(context.TODO()) + + singleResult := collection.FindOne(context.TODO(), filter) + if singleResult.Err() != nil { + return nil, errors.New("no document was found, check the filter or the location.") + } + return singleResult.DecodeBytes() +} diff --git a/internal/database/push.go b/internal/database/push.go new file mode 100644 index 0000000..370cd81 --- /dev/null +++ b/internal/database/push.go @@ -0,0 +1,15 @@ +package database + +import "context" + +func PushDocument(location Location, document interface{}) error { + client := GetNewClient() + collection := client.Database(location.Database).Collection(location.Collection) + defer client.Disconnect(context.TODO()) + + _, err := collection.InsertOne(context.TODO(), document) + if err != nil { + return err + } + return nil +}