diff --git a/cmd/main.go b/cmd/main.go index bffbdcf..88dae0f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,10 +20,10 @@ func initGin() { r.GET("/ping", internal.Ping) - r.GET("/get-all-articles", internal.AuthCheck, internal.GetAllArticles) + r.GET("/articles/", internal.AuthCheck, internal.GetAllArticles) r.GET("/articles/:id", internal.AuthCheck, internal.GetArticle) - r.POST("/add-article", internal.AuthCheck, internal.AddArticle) - r.DELETE("/delete-article", internal.AuthCheck, internal.DeleteArticle) + r.POST("/articles/:id", internal.AuthCheck, internal.AddArticle) + r.DELETE("/articles/:id", internal.AuthCheck, internal.DeleteArticle) r.POST("/login", internal.LoginUser) r.POST("/logout", internal.LogoutUser) diff --git a/internal/articles.go b/internal/articles.go index 7c92a9d..132b507 100644 --- a/internal/articles.go +++ b/internal/articles.go @@ -46,6 +46,10 @@ func GetArticle(c *gin.Context) { SendErrorMessageToClient(c, err.Error()) return } + if len(articles) == 0 { + SendErrorMessageToClient(c, "The ID provided doesn't match any article.") + return + } var parsedArticle Article bson.Unmarshal(articles[0], &parsedArticle) c.JSON(http.StatusOK, parsedArticle) @@ -65,6 +69,7 @@ func IsArticleIdAlreadyUsed(id string) bool { func AddArticle(c *gin.Context) { var article Article + article.Id_name = c.Params.ByName("id") if c.BindJSON(&article) != nil { SendErrorMessageToClient(c, "Could not correctly parse the article.") return @@ -92,6 +97,7 @@ func AddArticle(c *gin.Context) { func DeleteArticle(c *gin.Context) { var delArticle DelArticle + delArticle.Id_name = c.Params.ByName("id") if c.BindJSON(&delArticle) != nil { SendErrorMessageToClient(c, "Could not correctly parse the article ID.") return diff --git a/web/admin-gui/src/views/Home.vue b/web/admin-gui/src/views/Home.vue index 88de10e..591a6e5 100644 --- a/web/admin-gui/src/views/Home.vue +++ b/web/admin-gui/src/views/Home.vue @@ -14,11 +14,10 @@ onMounted(async function() { }) function addArticle() { - fetch("http://localhost:8080/add-article", { + fetch(`http://localhost:8080/articles/${Math.random() * 100}`, { method: "POST", headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`}, body: JSON.stringify({ - id_name: `${Math.random() * 100}`, content: {}, date: new Date().getTime() }) @@ -27,14 +26,8 @@ function addArticle() { .then(result => console.log(result)) } -function logArticles() { - fetch("http://localhost:8080/get-all-articles", {headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`}}) - .then(response => response.json()) - .then(result => console.log(result)) -} - function deleteArticle(articleID: String) { - fetch("http://localhost:8080/delete-article", { + fetch(`http://localhost:8080/articles/${articleID}`, { method: "DELETE", headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`}, body: JSON.stringify({ @@ -69,7 +62,7 @@ function auth(type: string, username: string, password: string) {