mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Split business logic from routes handler logic
This commit is contained in:
+4
-4
@@ -31,10 +31,10 @@ func initArticlesRoutes(r *gin.Engine) {
|
|||||||
articlesRouter := r.Group("/articles/")
|
articlesRouter := r.Group("/articles/")
|
||||||
articlesRouter.Use(internal.AuthMiddleware.MiddlewareFunc())
|
articlesRouter.Use(internal.AuthMiddleware.MiddlewareFunc())
|
||||||
|
|
||||||
articlesRouter.GET("/", internal.GetAllArticles)
|
articlesRouter.GET("/", internal.GetAllArticlesHandler)
|
||||||
articlesRouter.GET("/:id", internal.GetArticle)
|
articlesRouter.GET("/:id", internal.GetArticleHandler)
|
||||||
articlesRouter.POST("/:id", internal.AddArticle)
|
articlesRouter.POST("/:id", internal.AddArticleHandler)
|
||||||
articlesRouter.DELETE("/:id", internal.DeleteArticle)
|
articlesRouter.DELETE("/:id", internal.DeleteArticleHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initGin() {
|
func initGin() {
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
IdName string `json:"id_name" bson:"id_name"`
|
||||||
|
Date int64 `json:"date" bson:"date"`
|
||||||
|
Content interface{} `json:"content" bson:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DelArticle struct {
|
||||||
|
IdName string `json:"id_name" bson:"id_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var articlesLocation = 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
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAllArticlesHandler(c *gin.Context) {
|
||||||
|
documents, err := getDocuments(articlesLocation, bson.D{})
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, GetAllArticlesBusiness(documents))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetArticleHandler(c *gin.Context) {
|
||||||
|
articleID := c.Params.ByName("id")
|
||||||
|
articles, err := getDocuments(articlesLocation,
|
||||||
|
bson.D{{Key: "id_name", Value: articleID}})
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(articles) == 0 {
|
||||||
|
SendBadRequest(c, "The ID provided doesn't match any article.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var parsedArticle Article
|
||||||
|
bson.Unmarshal(articles[0], &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 {
|
||||||
|
SendBadRequest(c, "Could not correctly parse the article.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
document, err := bson.Marshal(article)
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, "Could not correctly marshal the article.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
documents, _ := getDocuments(articlesLocation, gin.H{})
|
||||||
|
if IsArticleIdAlreadyUsed(article.IdName, documents) {
|
||||||
|
SendBadRequest(c, "Article ID already used.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pushDocument(articlesLocation, document)
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, "Could not insert document into DB.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
SendOk(c, "Article successfully added!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteArticleHandler(c *gin.Context) {
|
||||||
|
var delArticle DelArticle
|
||||||
|
delArticle.IdName = c.Params.ByName("id")
|
||||||
|
if c.BindJSON(&delArticle) != nil {
|
||||||
|
SendBadRequest(c, "Could not correctly parse the article ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
document, err := bson.Marshal(delArticle)
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, "Could not correctly marshal the article ID.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteCount, err := deleteDocument(articlesLocation, document)
|
||||||
|
if err != nil {
|
||||||
|
SendBadRequest(c, "Could not insert document into DB.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user