mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Article GET route now returns Array
This commit is contained in:
@@ -17,7 +17,7 @@ type Article struct {
|
||||
|
||||
var articlesLocation = database.Location{Database: "gohcms", Collection: "articles"}
|
||||
|
||||
func GetAllArticlesBusiness(documents [][]byte) []Article {
|
||||
func ParseArticlesFromBytesToArray(documents [][]byte) []Article {
|
||||
var articles = []Article{}
|
||||
|
||||
for i := 0; i < len(documents); i++ {
|
||||
|
||||
@@ -27,7 +27,7 @@ var Article2, _ = bson.Marshal(Article{
|
||||
var BSONConvertedArticles = [][]byte{Article1, Article2}
|
||||
|
||||
func TestGetAllArticlesBusiness(t *testing.T) {
|
||||
documents := GetAllArticlesBusiness(BSONConvertedArticles)
|
||||
documents := ParseArticlesFromBytesToArray(BSONConvertedArticles)
|
||||
article := documents[0]
|
||||
|
||||
if article.TitleID != "test_article_1" {
|
||||
|
||||
@@ -10,26 +10,21 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func GetAllArticlesHandler(c *gin.Context) {
|
||||
documents, err := database.GetDocuments(articlesLocation, gin.H{})
|
||||
if err != nil {
|
||||
api.SendBadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, GetAllArticlesBusiness(documents))
|
||||
}
|
||||
|
||||
func GetArticleHandler(c *gin.Context) {
|
||||
articleID := c.Params.ByName("id")
|
||||
article, err := database.GetUniqueDocument(articlesLocation,
|
||||
gin.H{"titleID": articleID})
|
||||
|
||||
filter := gin.H{}
|
||||
if articleID != "" {
|
||||
filter["titleID"] = articleID
|
||||
}
|
||||
|
||||
articles, err := database.GetDocuments(articlesLocation, filter)
|
||||
if err != nil {
|
||||
api.SendBadRequest(c, fmt.Sprintf("The ID '%v' doesn't match any article.", articleID))
|
||||
return
|
||||
}
|
||||
var parsedArticle Article
|
||||
bson.Unmarshal(article, &parsedArticle)
|
||||
c.JSON(http.StatusOK, parsedArticle)
|
||||
|
||||
c.JSON(http.StatusOK, ParseArticlesFromBytesToArray(articles))
|
||||
}
|
||||
|
||||
func AddArticleHandler(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user