mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Improved Article model
This commit is contained in:
@@ -7,10 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
IdName string `json:"id_name" bson:"id_name"`
|
TitleID string `json:"titleID" bson:"titleID"`
|
||||||
|
Title string `json:"title" bson:"title"`
|
||||||
Date int64 `json:"date" bson:"date"`
|
Date int64 `json:"date" bson:"date"`
|
||||||
Content gin.H `json:"content" bson:"content"`
|
Content gin.H `json:"content" bson:"content"`
|
||||||
PageID string `json:"page_id" bson:"page_id"`
|
Tags []string `json:"tags" bson:"tags"`
|
||||||
Online bool `json:"online" bson:"online"`
|
Online bool `json:"online" bson:"online"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ func IsArticleIdAlreadyUsed(id string, documents [][]byte) bool {
|
|||||||
for i := 0; i < len(documents); i++ {
|
for i := 0; i < len(documents); i++ {
|
||||||
var newArticle Article
|
var newArticle Article
|
||||||
bson.Unmarshal(documents[i], &newArticle)
|
bson.Unmarshal(documents[i], &newArticle)
|
||||||
if newArticle.IdName == id {
|
if newArticle.TitleID == id {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,18 +9,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var Article1, _ = bson.Marshal(Article{
|
var Article1, _ = bson.Marshal(Article{
|
||||||
IdName: "test_article_1",
|
TitleID: "test_article_1",
|
||||||
Date: 1671482492,
|
Date: 1671482492,
|
||||||
Content: gin.H{},
|
Content: gin.H{},
|
||||||
PageID: "blog",
|
Tags: []string{"blog"},
|
||||||
Online: false,
|
Online: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
var Article2, _ = bson.Marshal(Article{
|
var Article2, _ = bson.Marshal(Article{
|
||||||
IdName: "test_article_2",
|
TitleID: "test_article_2",
|
||||||
Date: 1671899112,
|
Date: 1671899112,
|
||||||
Content: gin.H{},
|
Content: gin.H{},
|
||||||
PageID: "blog",
|
Tags: []string{"blog"},
|
||||||
Online: false,
|
Online: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -30,8 +30,8 @@ func TestGetAllArticlesBusiness(t *testing.T) {
|
|||||||
documents := GetAllArticlesBusiness(BSONConvertedArticles)
|
documents := GetAllArticlesBusiness(BSONConvertedArticles)
|
||||||
article := documents[0]
|
article := documents[0]
|
||||||
|
|
||||||
if article.IdName != "test_article_1" {
|
if article.TitleID != "test_article_1" {
|
||||||
log.Fatalf(`Excepted "test_article_1" as IdName, found "%v"`, article.IdName)
|
log.Fatalf(`Excepted "test_article_1" as IdName, found "%v"`, article.TitleID)
|
||||||
} else if article.Date != 1671482492 {
|
} else if article.Date != 1671482492 {
|
||||||
log.Fatalf(`Excepted 1671482492 as Date, found "%v"`, article.Date)
|
log.Fatalf(`Excepted 1671482492 as Date, found "%v"`, article.Date)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func GetArticleHandler(c *gin.Context) {
|
|||||||
|
|
||||||
func AddArticleHandler(c *gin.Context) {
|
func AddArticleHandler(c *gin.Context) {
|
||||||
var article Article
|
var article Article
|
||||||
article.IdName = c.Params.ByName("id")
|
article.TitleID = c.Params.ByName("id")
|
||||||
if c.BindJSON(&article) != nil {
|
if c.BindJSON(&article) != nil {
|
||||||
api.SendBadRequest(c, "Could not correctly parse the article.")
|
api.SendBadRequest(c, "Could not correctly parse the article.")
|
||||||
return
|
return
|
||||||
@@ -47,7 +47,7 @@ func AddArticleHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
documents, _ := database.GetDocuments(articlesLocation, gin.H{})
|
documents, _ := database.GetDocuments(articlesLocation, gin.H{})
|
||||||
if IsArticleIdAlreadyUsed(article.IdName, documents) {
|
if IsArticleIdAlreadyUsed(article.TitleID, documents) {
|
||||||
api.SendBadRequest(c, "Article ID already used.")
|
api.SendBadRequest(c, "Article ID already used.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-7
@@ -4,23 +4,26 @@ const db = conn.getDB("gohcms")
|
|||||||
db.createCollection('articles', {
|
db.createCollection('articles', {
|
||||||
validator: {
|
validator: {
|
||||||
$jsonSchema: {
|
$jsonSchema: {
|
||||||
required: ['content', 'date', 'id_name', 'page_id', 'online'],
|
required: ['titleID', 'title', 'content', 'date', 'tags', 'online'],
|
||||||
properties: {
|
properties: {
|
||||||
_id: {
|
_id: {
|
||||||
bsonType: 'objectId'
|
bsonType: 'objectId'
|
||||||
},
|
},
|
||||||
|
titleID: {
|
||||||
|
bsonType: 'string',
|
||||||
|
pattern: '^[-a-z]+$'
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
bsonType: 'string'
|
||||||
|
},
|
||||||
content: {
|
content: {
|
||||||
bsonType: 'object'
|
bsonType: 'object'
|
||||||
},
|
},
|
||||||
date: {
|
date: {
|
||||||
bsonType: 'number'
|
bsonType: 'number'
|
||||||
},
|
},
|
||||||
id_name: {
|
tags: {
|
||||||
bsonType: 'string',
|
bsonType: 'array'
|
||||||
pattern: '^.+$'
|
|
||||||
},
|
|
||||||
page_id: {
|
|
||||||
bsonType: 'string'
|
|
||||||
},
|
},
|
||||||
online: {
|
online: {
|
||||||
bsonType: 'bool'
|
bsonType: 'bool'
|
||||||
|
|||||||
Reference in New Issue
Block a user