diff --git a/cmd/main.go b/cmd/main.go
index bc93212..6c0f6bd 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -21,6 +21,7 @@ func initGin() {
r.GET("/ping", internal.Ping)
r.GET("/get-all-articles", internal.GetAllArticles)
r.POST("/add-article", internal.AddArticle)
+ r.DELETE("/delete-article", internal.DeleteArticle)
r.Run(":" + os.Getenv("API_PORT"))
}
diff --git a/internal/articles.go b/internal/articles.go
index b27f084..7a0625f 100644
--- a/internal/articles.go
+++ b/internal/articles.go
@@ -1,6 +1,7 @@
package internal
import (
+ "fmt"
"net/http"
"github.com/gin-gonic/gin"
@@ -13,6 +14,10 @@ type Article struct {
Content interface{} `json:"content" bson:"content"`
}
+type DelArticle struct {
+ Id_name string `json:"id_name" bson:"id_name"`
+}
+
var ARTICLES_LOCATION = Location{Database: "gohcms", Collection: "articles"}
// TODO Change to GetArticleList that will find filter json context
@@ -20,7 +25,8 @@ func GetAllArticles(c *gin.Context) {
var articles []Article
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
if err != nil {
- SendErrorMessageToClient(c, err.Error()); return
+ SendErrorMessageToClient(c, err.Error())
+ return
}
for i := 0; i < len(documents); i++ {
@@ -32,7 +38,7 @@ func GetAllArticles(c *gin.Context) {
c.JSON(http.StatusOK, articles)
}
-func IsArticleIdAlreadyUsed(id string) bool {
+func IsArticleIdAlreadyUsed(id string) bool {
documents, _ := getDocuments(ARTICLES_LOCATION, bson.D{})
for i := 0; i < len(documents); i++ {
var newArticle Article
@@ -47,22 +53,48 @@ func IsArticleIdAlreadyUsed(id string) bool {
func AddArticle(c *gin.Context) {
var article Article
if c.BindJSON(&article) != nil {
- SendErrorMessageToClient(c, "Could not correctly parse the article."); return
+ SendErrorMessageToClient(c, "Could not correctly parse the article.")
+ return
}
document, err := bson.Marshal(article)
if err != nil {
- SendErrorMessageToClient(c, "Could not correctly marshal the article."); return
+ SendErrorMessageToClient(c, "Could not correctly marshal the article.")
+ return
}
if IsArticleIdAlreadyUsed(article.Id_name) {
- SendErrorMessageToClient(c, "Article ID already used."); return
+ SendErrorMessageToClient(c, "Article ID already used.")
+ return
}
err = pushDocument(ARTICLES_LOCATION, document)
if err != nil {
- SendErrorMessageToClient(c, "Could not insert document into DB."); return
+ SendErrorMessageToClient(c, "Could not insert document into DB.")
+ return
}
SendOkMessageToClient(c, "Article successfully added!")
}
+
+func DeleteArticle(c *gin.Context) {
+ var delArticle DelArticle
+ if c.BindJSON(&delArticle) != nil {
+ SendErrorMessageToClient(c, "Could not correctly parse the article ID.")
+ return
+ }
+
+ document, err := bson.Marshal(delArticle)
+ if err != nil {
+ SendErrorMessageToClient(c, "Could not correctly marshal the article ID.")
+ return
+ }
+
+ deleteCount, err := deleteDocument(ARTICLES_LOCATION, document)
+ if err != nil {
+ SendErrorMessageToClient(c, "Could not insert document into DB.")
+ return
+ }
+
+ SendOkMessageToClient(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
+}
diff --git a/internal/database.go b/internal/database.go
index a5fc5c3..b05be88 100644
--- a/internal/database.go
+++ b/internal/database.go
@@ -44,10 +44,23 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) {
cursor, err := collection.Find(context.TODO(), filter)
if err != nil {
- return results, errors.New("something is wrong with filter to find document")
+ 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 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/web/admin-gui/src/views/Home.vue b/web/admin-gui/src/views/Home.vue
index b400665..4faceb2 100644
--- a/web/admin-gui/src/views/Home.vue
+++ b/web/admin-gui/src/views/Home.vue
@@ -1,7 +1,9 @@
Salut à tous
-
+
+
+
+