mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Added getAllArticles and refactor
This commit is contained in:
@@ -19,6 +19,7 @@ func initGin() {
|
|||||||
r.SetTrustedProxies([]string{"localhost"})
|
r.SetTrustedProxies([]string{"localhost"})
|
||||||
|
|
||||||
r.GET("/ping", internal.Ping)
|
r.GET("/ping", internal.Ping)
|
||||||
|
r.GET("/get-all-articles", internal.GetAllArticles)
|
||||||
r.POST("/add-article", internal.AddArticle)
|
r.POST("/add-article", internal.AddArticle)
|
||||||
|
|
||||||
r.Run(":" + os.Getenv("API_PORT"))
|
r.Run(":" + os.Getenv("API_PORT"))
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Article struct {
|
|
||||||
Id_name string `json:"id_name" bson:"id_name"`
|
|
||||||
Date int64 `json:"date" bson:"date"`
|
|
||||||
Content interface{} `json:"content" bson:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddArticle(c *gin.Context) {
|
|
||||||
var article Article
|
|
||||||
if c.BindJSON(&article) != nil {
|
|
||||||
SendErrorMessageToClient(c, "Could not correctly parse the article.")
|
|
||||||
}
|
|
||||||
|
|
||||||
document, err := bson.Marshal(article)
|
|
||||||
if err != nil {
|
|
||||||
SendErrorMessageToClient(c, "Could not correctly marshal the article.")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pushDocument(Location{Database: "gohcms", Collection: "articles"}, document)
|
|
||||||
if err != nil {
|
|
||||||
SendErrorMessageToClient(c, "Could not insert document into DB.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
Id_name string `json:"id_name" bson:"id_name"`
|
||||||
|
Date int64 `json:"date" bson:"date"`
|
||||||
|
Content interface{} `json:"content" bson:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var ARTICLES_LOCATION = Location{Database: "gohcms", Collection: "articles"}
|
||||||
|
|
||||||
|
// TODO check if id_name is unique among all articles
|
||||||
|
|
||||||
|
func AddArticle(c *gin.Context) {
|
||||||
|
var article Article
|
||||||
|
if c.BindJSON(&article) != nil {
|
||||||
|
SendErrorMessageToClient(c, "Could not correctly parse the article.")
|
||||||
|
}
|
||||||
|
|
||||||
|
document, err := bson.Marshal(article)
|
||||||
|
if err != nil {
|
||||||
|
SendErrorMessageToClient(c, "could not correctly marshal the article")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pushDocument(ARTICLES_LOCATION, document)
|
||||||
|
if err != nil {
|
||||||
|
SendErrorMessageToClient(c, "could not insert document into DB")
|
||||||
|
}
|
||||||
|
|
||||||
|
SendOkMessageToClient(c, "Article successfully added!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllArticles(c *gin.Context) {
|
||||||
|
var articles []Article
|
||||||
|
documents, err := getDocuments(ARTICLES_LOCATION, bson.D{})
|
||||||
|
if err != nil {
|
||||||
|
SendErrorMessageToClient(c, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(documents); i++ {
|
||||||
|
var newArticle Article
|
||||||
|
bson.Unmarshal(documents[i], &newArticle)
|
||||||
|
fmt.Println(newArticle)
|
||||||
|
articles = append(articles, newArticle)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, articles)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user