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:
+1
-1
@@ -54,7 +54,7 @@ func initArticlesRoutes(r *gin.Engine) {
|
||||
articlesRouter := r.Group("/articles")
|
||||
articlesRouter.Use(corsMiddleware, api.AuthMiddleware.MiddlewareFunc())
|
||||
|
||||
articlesRouter.GET("/", articles.GetAllArticlesHandler)
|
||||
articlesRouter.GET("/", articles.GetArticleHandler)
|
||||
articlesRouter.GET("/:id", articles.GetArticleHandler)
|
||||
articlesRouter.POST("/:id", articles.AddArticleHandler)
|
||||
articlesRouter.PATCH("/:id", articles.EditArticleHandler)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -13,7 +13,7 @@ export interface Article {
|
||||
|
||||
const baseURL = `http://${__APP_ENV__.APP_HOST_ADDRESS}:${__APP_ENV__.APP_API_PORT}`
|
||||
|
||||
export async function getArticle(id: string) : Promise<Article> {
|
||||
export async function getArticles(id: string) : Promise<Array<Article>> {
|
||||
return await fetch(`${baseURL}/articles/${id}`, {
|
||||
method: 'GET',
|
||||
headers: { "Authorization": `Bearer ${useAuthStore().token}` }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { getArticle, type Article } from '@/utils/database';
|
||||
import { getArticles, type Article } from '@/utils/database';
|
||||
import Editor from '@tinymce/tinymce-vue';
|
||||
import { onMounted, ref, type Ref } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
@@ -8,7 +8,8 @@ const article: Ref<Article | void> = ref()
|
||||
const editorData: Ref<string> = ref('')
|
||||
|
||||
onMounted(async () => {
|
||||
article.value = await getArticle(useRoute().params.articleID as string)
|
||||
const articleFetch = await getArticles(useRoute().params.articleID as string)
|
||||
article.value = articleFetch[0]
|
||||
editorData.value = article.value.content.html
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user