Complete file tree and packages managment rework

This commit is contained in:
Florian Sylvain
2022-12-27 21:00:19 +01:00
parent 39afc0c066
commit 451fd8452b
11 changed files with 153 additions and 161 deletions
+38
View File
@@ -0,0 +1,38 @@
package articles
import (
"github.com/Floriansylvain/GohCMS/internal/database"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
type Article struct {
IdName string `json:"id_name" bson:"id_name"`
Date int64 `json:"date" bson:"date"`
Content gin.H `json:"content" bson:"content"`
}
var articlesLocation = database.Location{Database: "gohcms", Collection: "articles"}
func GetAllArticlesBusiness(documents [][]byte) []Article {
var articles = []Article{}
for i := 0; i < len(documents); i++ {
var newArticle Article
bson.Unmarshal(documents[i], &newArticle)
articles = append(articles, newArticle)
}
return articles
}
func IsArticleIdAlreadyUsed(id string, documents [][]byte) bool {
for i := 0; i < len(documents); i++ {
var newArticle Article
bson.Unmarshal(documents[i], &newArticle)
if newArticle.IdName == id {
return true
}
}
return false
}
+42
View File
@@ -0,0 +1,42 @@
package articles
import (
"log"
"testing"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
var Article1, _ = bson.Marshal(Article{
IdName: "test_article_1",
Date: 1671482492,
Content: gin.H{},
})
var Article2, _ = bson.Marshal(Article{
IdName: "test_article_2",
Date: 1671899112,
Content: gin.H{},
})
var BSONConvertedArticles = [][]byte{Article1, Article2}
func TestGetAllArticlesBusiness(t *testing.T) {
documents := GetAllArticlesBusiness(BSONConvertedArticles)
article := documents[0]
if article.IdName != "test_article_1" {
log.Fatalf(`Excepted "test_article_1" as IdName, found "%v"`, article.IdName)
} else if article.Date != 1671482492 {
log.Fatalf(`Excepted 1671482492 as Date, found "%v"`, article.Date)
}
}
func TestIsArticleIdAlreadyUsed(t *testing.T) {
if IsArticleIdAlreadyUsed("test_article_1", BSONConvertedArticles) == false {
log.Fatalf(`Excepted true, found false`)
} else if IsArticleIdAlreadyUsed("test_article_3", BSONConvertedArticles) == true {
log.Fatalf(`Excepted false, found true`)
}
}
+98
View File
@@ -0,0 +1,98 @@
package articles
import (
"fmt"
"net/http"
"github.com/Floriansylvain/GohCMS/internal/api"
"github.com/Floriansylvain/GohCMS/internal/database"
"github.com/gin-gonic/gin"
"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{"id_name": articleID})
if err != nil {
api.SendBadRequest(c, "The ID provided doesn't match any article.")
return
}
var parsedArticle Article
bson.Unmarshal(article, &parsedArticle)
c.JSON(http.StatusOK, parsedArticle)
}
func AddArticleHandler(c *gin.Context) {
var article Article
article.IdName = c.Params.ByName("id")
if c.BindJSON(&article) != nil {
api.SendBadRequest(c, "Could not correctly parse the article.")
return
}
document, err := bson.Marshal(article)
if err != nil {
api.SendBadRequest(c, "Could not correctly marshal the article.")
return
}
documents, _ := database.GetDocuments(articlesLocation, gin.H{})
if IsArticleIdAlreadyUsed(article.IdName, documents) {
api.SendBadRequest(c, "Article ID already used.")
return
}
err = database.PushDocument(articlesLocation, document)
if err != nil {
api.SendBadRequest(c, fmt.Sprintf(`Could not insert document into DB: %v`, err.Error()))
return
}
api.SendOk(c, "Article successfully added!")
}
func DeleteArticleHandler(c *gin.Context) {
id := c.Params.ByName("id")
deleteCount, err := database.DeleteDocument(articlesLocation, gin.H{"id_name": id})
if err != nil {
api.SendBadRequest(c, "Could not delete document into DB.")
return
}
if deleteCount != 0 {
api.SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
} else {
api.SendOk(c, "No articles were deleted.")
}
}
func EditArticleHandler(c *gin.Context) {
id := c.Params.ByName("id")
var articleUpdate database.DocumentUpdate
articleUpdate.Filter = gin.H{"id_name": id}
c.BindJSON(&articleUpdate.Update)
editCount, err := database.EditDocument(articlesLocation, articleUpdate)
if err != nil {
api.SendBadRequest(c, err.Error())
return
}
if editCount != 0 {
api.SendOk(c, fmt.Sprintf("%d articles were successfully edited!", editCount))
} else {
api.SendOk(c, "No articles were edited.")
}
}