mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Routes standard improvement
This commit is contained in:
+3
-3
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
<template>
|
||||
<p>Salut à tous</p>
|
||||
<button @click="addArticle">add rand article</button>
|
||||
<button @click="logArticles">display articles in console</button> <br>
|
||||
<button @click="getArticle('')">display articles in console</button> <br>
|
||||
<label for="articleIDinput"></label>
|
||||
<input id="articleIDinput" name="articleIDinput" type="text" placeholder="article ID" v-model="inputArticleID">
|
||||
<button @click="deleteArticle(inputArticleID)">delete this article</button>
|
||||
|
||||
Reference in New Issue
Block a user