mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Fixed get responses and push/getDocs
This commit is contained in:
@@ -22,16 +22,19 @@ func AddArticle(c *gin.Context) {
|
|||||||
var article Article
|
var article Article
|
||||||
if c.BindJSON(&article) != nil {
|
if c.BindJSON(&article) != nil {
|
||||||
SendErrorMessageToClient(c, "Could not correctly parse the article.")
|
SendErrorMessageToClient(c, "Could not correctly parse the article.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
document, err := bson.Marshal(article)
|
document, err := bson.Marshal(article)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendErrorMessageToClient(c, "could not correctly marshal the article")
|
SendErrorMessageToClient(c, "could not correctly marshal the article")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pushDocument(ARTICLES_LOCATION, document)
|
err = pushDocument(ARTICLES_LOCATION, document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendErrorMessageToClient(c, "could not insert document into DB")
|
SendErrorMessageToClient(c, "could not insert document into DB")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SendOkMessageToClient(c, "Article successfully added!")
|
SendOkMessageToClient(c, "Article successfully added!")
|
||||||
@@ -43,6 +46,7 @@ func GetAllArticles(c *gin.Context) {
|
|||||||
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
|
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendErrorMessageToClient(c, err.Error())
|
SendErrorMessageToClient(c, err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(documents); i++ {
|
for i := 0; i < len(documents); i++ {
|
||||||
|
|||||||
@@ -23,14 +23,11 @@ func getNewClient() *mongo.Client {
|
|||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCollection(location Location) *mongo.Collection {
|
|
||||||
client := getNewClient()
|
|
||||||
defer client.Disconnect(context.TODO())
|
|
||||||
return client.Database(location.Database).Collection(location.Collection)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushDocument(location Location, document interface{}) error {
|
func pushDocument(location Location, document interface{}) error {
|
||||||
collection := getCollection(location)
|
client := getNewClient()
|
||||||
|
collection := client.Database(location.Database).Collection(location.Collection)
|
||||||
|
defer client.Disconnect(context.TODO())
|
||||||
|
|
||||||
_, err := collection.InsertOne(context.TODO(), document)
|
_, err := collection.InsertOne(context.TODO(), document)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -39,7 +36,10 @@ func pushDocument(location Location, document interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
||||||
collection := getCollection(location)
|
client := getNewClient()
|
||||||
|
collection := client.Database(location.Database).Collection(location.Collection)
|
||||||
|
defer client.Disconnect(context.TODO())
|
||||||
|
|
||||||
var results [][]byte
|
var results [][]byte
|
||||||
|
|
||||||
cursor, err := collection.Find(context.TODO(), filter)
|
cursor, err := collection.Find(context.TODO(), filter)
|
||||||
|
|||||||
Reference in New Issue
Block a user