Added route to get Article by ID

This commit is contained in:
Florian Sylvain
2022-10-20 13:29:59 +02:00
parent 0dd12627cf
commit 3b41b12d5e
3 changed files with 27 additions and 3 deletions
+1
View File
@@ -21,6 +21,7 @@ func initGin() {
r.GET("/ping", internal.Ping) r.GET("/ping", internal.Ping)
r.GET("/get-all-articles", internal.AuthCheck, internal.GetAllArticles) r.GET("/get-all-articles", internal.AuthCheck, internal.GetAllArticles)
r.GET("/articles/:id", internal.AuthCheck, internal.GetArticle)
r.POST("/add-article", internal.AuthCheck, internal.AddArticle) r.POST("/add-article", internal.AuthCheck, internal.AddArticle)
r.DELETE("/delete-article", internal.AuthCheck, internal.DeleteArticle) r.DELETE("/delete-article", internal.AuthCheck, internal.DeleteArticle)
+15 -1
View File
@@ -38,6 +38,19 @@ func GetAllArticles(c *gin.Context) {
c.JSON(http.StatusOK, articles) c.JSON(http.StatusOK, articles)
} }
func GetArticle(c *gin.Context) {
articleID := c.Params.ByName("id")
articles, err := getDocuments(ARTICLES_LOCATION,
bson.D{{Key: "id_name", Value: articleID}})
if err != nil {
SendErrorMessageToClient(c, err.Error())
return
}
var parsedArticle Article
bson.Unmarshal(articles[0], &parsedArticle)
c.JSON(http.StatusOK, parsedArticle)
}
func IsArticleIdAlreadyUsed(id string) bool { func IsArticleIdAlreadyUsed(id string) bool {
documents, _ := getDocuments(ARTICLES_LOCATION, bson.D{}) documents, _ := getDocuments(ARTICLES_LOCATION, bson.D{})
for i := 0; i < len(documents); i++ { for i := 0; i < len(documents); i++ {
@@ -96,5 +109,6 @@ func DeleteArticle(c *gin.Context) {
return return
} }
SendOkMessageToClient(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount)) SendOkMessageToClient(c,
fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
} }
+11 -2
View File
@@ -45,6 +45,14 @@ function deleteArticle(articleID: String) {
.then(result => console.log(result)) .then(result => console.log(result))
} }
function getArticle(articleID: String) {
fetch(`http://localhost:8080/articles/${articleID}`, {
headers: {"Authorization" : `Basic ${btoa(`${username.value}:${password.value}`)}`},
})
.then(response => response.json())
.then(result => console.log(result))
}
function auth(type: string, username: string, password: string) { function auth(type: string, username: string, password: string) {
fetch(`http://localhost:8080/${type}`, { fetch(`http://localhost:8080/${type}`, {
method: "POST", method: "POST",
@@ -63,8 +71,9 @@ function auth(type: string, username: string, password: string) {
<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="logArticles">display articles in console</button> <br>
<label for="articleIDinput"></label> <label for="articleIDinput"></label>
<input id="articleIDinput" name="articleIDinput" type="text" placeholder="article ID to delete" v-model="inputArticleID"> <input id="articleIDinput" name="articleIDinput" type="text" placeholder="article ID" v-model="inputArticleID">
<button @click="deleteArticle(inputArticleID)">delete this article</button> <br> <button @click="deleteArticle(inputArticleID)">delete this article</button>
<button @click="getArticle(inputArticleID)">get this article</button> <br>
<input type="text" name="username" id="username" v-model="username" placeholder="username"> <input type="text" name="username" id="username" v-model="username" placeholder="username">
<input type="text" name="password" id="password" v-model="password" placeholder="password"> <input type="text" name="password" id="password" v-model="password" placeholder="password">