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("/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.GET("/articles/:id", internal.AuthCheck, internal.GetArticle)
|
||||||
r.POST("/add-article", internal.AuthCheck, internal.AddArticle)
|
r.POST("/articles/:id", internal.AuthCheck, internal.AddArticle)
|
||||||
r.DELETE("/delete-article", internal.AuthCheck, internal.DeleteArticle)
|
r.DELETE("/articles/:id", internal.AuthCheck, internal.DeleteArticle)
|
||||||
|
|
||||||
r.POST("/login", internal.LoginUser)
|
r.POST("/login", internal.LoginUser)
|
||||||
r.POST("/logout", internal.LogoutUser)
|
r.POST("/logout", internal.LogoutUser)
|
||||||
|
|||||||
@@ -46,6 +46,10 @@ func GetArticle(c *gin.Context) {
|
|||||||
SendErrorMessageToClient(c, err.Error())
|
SendErrorMessageToClient(c, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(articles) == 0 {
|
||||||
|
SendErrorMessageToClient(c, "The ID provided doesn't match any article.")
|
||||||
|
return
|
||||||
|
}
|
||||||
var parsedArticle Article
|
var parsedArticle Article
|
||||||
bson.Unmarshal(articles[0], &parsedArticle)
|
bson.Unmarshal(articles[0], &parsedArticle)
|
||||||
c.JSON(http.StatusOK, parsedArticle)
|
c.JSON(http.StatusOK, parsedArticle)
|
||||||
@@ -65,6 +69,7 @@ func IsArticleIdAlreadyUsed(id string) bool {
|
|||||||
|
|
||||||
func AddArticle(c *gin.Context) {
|
func AddArticle(c *gin.Context) {
|
||||||
var article Article
|
var article Article
|
||||||
|
article.Id_name = c.Params.ByName("id")
|
||||||
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
|
return
|
||||||
@@ -92,6 +97,7 @@ func AddArticle(c *gin.Context) {
|
|||||||
|
|
||||||
func DeleteArticle(c *gin.Context) {
|
func DeleteArticle(c *gin.Context) {
|
||||||
var delArticle DelArticle
|
var delArticle DelArticle
|
||||||
|
delArticle.Id_name = c.Params.ByName("id")
|
||||||
if c.BindJSON(&delArticle) != nil {
|
if c.BindJSON(&delArticle) != nil {
|
||||||
SendErrorMessageToClient(c, "Could not correctly parse the article ID.")
|
SendErrorMessageToClient(c, "Could not correctly parse the article ID.")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -14,11 +14,10 @@ onMounted(async function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
function addArticle() {
|
function addArticle() {
|
||||||
fetch("http://localhost:8080/add-article", {
|
fetch(`http://localhost:8080/articles/${Math.random() * 100}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`},
|
headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id_name: `${Math.random() * 100}`,
|
|
||||||
content: {},
|
content: {},
|
||||||
date: new Date().getTime()
|
date: new Date().getTime()
|
||||||
})
|
})
|
||||||
@@ -27,14 +26,8 @@ function addArticle() {
|
|||||||
.then(result => console.log(result))
|
.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) {
|
function deleteArticle(articleID: String) {
|
||||||
fetch("http://localhost:8080/delete-article", {
|
fetch(`http://localhost:8080/articles/${articleID}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`},
|
headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -69,7 +62,7 @@ function auth(type: string, username: string, password: string) {
|
|||||||
<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> <br>
|
<button @click="getArticle('')">display articles in console</button> <br>
|
||||||
<label for="articleIDinput"></label>
|
<label for="articleIDinput"></label>
|
||||||
<input id="articleIDinput" name="articleIDinput" type="text" placeholder="article ID" v-model="inputArticleID">
|
<input id="articleIDinput" name="articleIDinput" type="text" placeholder="article ID" v-model="inputArticleID">
|
||||||
<button @click="deleteArticle(inputArticleID)">delete this article</button>
|
<button @click="deleteArticle(inputArticleID)">delete this article</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user