mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Added DeleteArticle route
This commit is contained in:
@@ -21,6 +21,7 @@ func initGin() {
|
|||||||
r.GET("/ping", internal.Ping)
|
r.GET("/ping", internal.Ping)
|
||||||
r.GET("/get-all-articles", internal.GetAllArticles)
|
r.GET("/get-all-articles", internal.GetAllArticles)
|
||||||
r.POST("/add-article", internal.AddArticle)
|
r.POST("/add-article", internal.AddArticle)
|
||||||
|
r.DELETE("/delete-article", internal.DeleteArticle)
|
||||||
|
|
||||||
r.Run(":" + os.Getenv("API_PORT"))
|
r.Run(":" + os.Getenv("API_PORT"))
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-5
@@ -1,6 +1,7 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -13,6 +14,10 @@ type Article struct {
|
|||||||
Content interface{} `json:"content" bson:"content"`
|
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"}
|
var ARTICLES_LOCATION = Location{Database: "gohcms", Collection: "articles"}
|
||||||
|
|
||||||
// TODO Change to GetArticleList that will find filter json context
|
// TODO Change to GetArticleList that will find filter json context
|
||||||
@@ -20,7 +25,8 @@ func GetAllArticles(c *gin.Context) {
|
|||||||
var articles []Article
|
var articles []Article
|
||||||
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
|
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendErrorMessageToClient(c, err.Error()); return
|
SendErrorMessageToClient(c, err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(documents); i++ {
|
for i := 0; i < len(documents); i++ {
|
||||||
@@ -47,22 +53,48 @@ func IsArticleIdAlreadyUsed(id string) bool {
|
|||||||
func AddArticle(c *gin.Context) {
|
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."); return
|
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."); return
|
SendErrorMessageToClient(c, "Could not correctly marshal the article.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsArticleIdAlreadyUsed(article.Id_name) {
|
if IsArticleIdAlreadyUsed(article.Id_name) {
|
||||||
SendErrorMessageToClient(c, "Article ID already used."); return
|
SendErrorMessageToClient(c, "Article ID already used.")
|
||||||
|
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."); return
|
SendErrorMessageToClient(c, "Could not insert document into DB.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SendOkMessageToClient(c, "Article successfully added!")
|
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))
|
||||||
|
}
|
||||||
|
|||||||
+14
-1
@@ -44,10 +44,23 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
|||||||
|
|
||||||
cursor, err := collection.Find(context.TODO(), filter)
|
cursor, err := collection.Find(context.TODO(), filter)
|
||||||
if err != nil {
|
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()) {
|
for cursor.TryNext(context.TODO()) {
|
||||||
results = append(results, cursor.Current)
|
results = append(results, cursor.Current)
|
||||||
}
|
}
|
||||||
return results, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
// import EditorJS from '@editorjs/editorjs';
|
// import EditorJS from '@editorjs/editorjs';
|
||||||
import { pingApi } from '@/utils/ping';
|
import { pingApi } from '@/utils/ping';
|
||||||
import { onMounted } from 'vue';
|
import { onMounted, ref } from 'vue';
|
||||||
|
|
||||||
|
const inputArticleID = ref('')
|
||||||
|
|
||||||
onMounted(async function() {
|
onMounted(async function() {
|
||||||
console.log('ping...')
|
console.log('ping...')
|
||||||
@@ -27,12 +29,26 @@ function logArticles() {
|
|||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(result => console.log(result))
|
.then(result => console.log(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteArticle(articleID: String) {
|
||||||
|
fetch("http://localhost:8080/delete-article", {
|
||||||
|
method: "DELETE",
|
||||||
|
body: JSON.stringify({
|
||||||
|
id_name: articleID
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => console.log(result))
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<p>Salut à tous</p>
|
<p>Salut à tous</p>
|
||||||
<button @click="addArticle">add rand article</button>
|
<button @click="addArticle">add rand article</button>
|
||||||
<button @click="logArticles">display articles in console</button>
|
<button @click="logArticles">display articles in console</button> <br>
|
||||||
|
<label for="articleIDinput"></label>
|
||||||
|
<input id="articleIDinput" name="articleIDinput" type="text" v-model="inputArticleID">
|
||||||
|
<button @click="deleteArticle(inputArticleID)">delete this article</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
Reference in New Issue
Block a user