mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Complete file tree and packages managment rework
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package internal
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Floriansylvain/GohCMS/internal/database"
|
||||
jwt "github.com/appleboy/gin-jwt/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@@ -15,7 +16,7 @@ type User struct {
|
||||
Password string `json:"password" bson:"password"`
|
||||
}
|
||||
|
||||
var UsersLocation = Location{Database: "gohcms", Collection: "users"}
|
||||
var UsersLocation = database.Location{Database: "gohcms", Collection: "users"}
|
||||
|
||||
var AuthMiddleware, _ = jwt.New(&jwt.GinJWTMiddleware{
|
||||
Realm: "GohCMS",
|
||||
@@ -32,7 +33,7 @@ func JWTAuthenticator(c *gin.Context) (interface{}, error) {
|
||||
return nil, errors.New("wrong credentials json format.")
|
||||
}
|
||||
|
||||
_, err = getUniqueDocument(UsersLocation, bson.D{
|
||||
_, err = database.GetUniqueDocument(UsersLocation, bson.D{
|
||||
{Key: "email", Value: user.Email},
|
||||
{Key: "password", Value: user.Password},
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,6 +1,7 @@
|
||||
package internal
|
||||
package articles
|
||||
|
||||
import (
|
||||
"github.com/Floriansylvain/GohCMS/internal/database"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
@@ -11,7 +12,7 @@ type Article struct {
|
||||
Content gin.H `json:"content" bson:"content"`
|
||||
}
|
||||
|
||||
var articlesLocation = Location{Database: "gohcms", Collection: "articles"}
|
||||
var articlesLocation = database.Location{Database: "gohcms", Collection: "articles"}
|
||||
|
||||
func GetAllArticlesBusiness(documents [][]byte) []Article {
|
||||
var articles = []Article{}
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package articles
|
||||
|
||||
import (
|
||||
"log"
|
||||
@@ -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.")
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
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, gin.H{})
|
||||
if err != nil {
|
||||
SendBadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, GetAllArticlesBusiness(documents))
|
||||
}
|
||||
|
||||
func GetArticleHandler(c *gin.Context) {
|
||||
articleID := c.Params.ByName("id")
|
||||
article, err := getUniqueDocument(articlesLocation,
|
||||
gin.H{"id_name": articleID})
|
||||
if err != nil {
|
||||
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 {
|
||||
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, fmt.Sprintf(`Could not insert document into DB: %v`, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
SendOk(c, "Article successfully added!")
|
||||
}
|
||||
|
||||
func DeleteArticleHandler(c *gin.Context) {
|
||||
id := c.Params.ByName("id")
|
||||
|
||||
deleteCount, err := deleteDocument(articlesLocation, gin.H{"id_name": id})
|
||||
if err != nil {
|
||||
SendBadRequest(c, "Could not delete document into DB.")
|
||||
return
|
||||
}
|
||||
|
||||
if deleteCount != 0 {
|
||||
SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
|
||||
} else {
|
||||
SendOk(c, "No articles were deleted.")
|
||||
}
|
||||
}
|
||||
|
||||
func EditArticleHandler(c *gin.Context) {
|
||||
id := c.Params.ByName("id")
|
||||
|
||||
var articleUpdate DocumentUpdate
|
||||
articleUpdate.Filter = gin.H{"id_name": id}
|
||||
c.BindJSON(&articleUpdate.Update)
|
||||
|
||||
editCount, err := editDocument(articlesLocation, articleUpdate)
|
||||
if err != nil {
|
||||
SendBadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if editCount != 0 {
|
||||
SendOk(c, fmt.Sprintf("%d articles were successfully edited!", editCount))
|
||||
} else {
|
||||
SendOk(c, "No articles were edited.")
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package internal
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -19,7 +19,7 @@ type DocumentUpdate struct {
|
||||
Update gin.H `json:"update"`
|
||||
}
|
||||
|
||||
func getNewClient() *mongo.Client {
|
||||
func GetNewClient() *mongo.Client {
|
||||
client, err := mongo.Connect(
|
||||
context.TODO(),
|
||||
options.Client().ApplyURI("mongodb://db:27017/"))
|
||||
@@ -29,8 +29,8 @@ func getNewClient() *mongo.Client {
|
||||
return client
|
||||
}
|
||||
|
||||
func pushDocument(location Location, document interface{}) error {
|
||||
client := getNewClient()
|
||||
func PushDocument(location Location, document interface{}) error {
|
||||
client := GetNewClient()
|
||||
collection := client.Database(location.Database).Collection(location.Collection)
|
||||
defer client.Disconnect(context.TODO())
|
||||
|
||||
@@ -41,8 +41,8 @@ func pushDocument(location Location, document interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
||||
client := getNewClient()
|
||||
func GetDocuments(location Location, filter interface{}) ([][]byte, error) {
|
||||
client := GetNewClient()
|
||||
collection := client.Database(location.Database).Collection(location.Collection)
|
||||
defer client.Disconnect(context.TODO())
|
||||
|
||||
@@ -58,8 +58,8 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func getUniqueDocument(location Location, filter interface{}) ([]byte, error) {
|
||||
client := getNewClient()
|
||||
func GetUniqueDocument(location Location, filter interface{}) ([]byte, error) {
|
||||
client := GetNewClient()
|
||||
collection := client.Database(location.Database).Collection(location.Collection)
|
||||
defer client.Disconnect(context.TODO())
|
||||
|
||||
@@ -70,8 +70,8 @@ func getUniqueDocument(location Location, filter interface{}) ([]byte, error) {
|
||||
return singleResult.DecodeBytes()
|
||||
}
|
||||
|
||||
func deleteDocument(location Location, filter interface{}) (int64, error) {
|
||||
client := getNewClient()
|
||||
func DeleteDocument(location Location, filter interface{}) (int64, error) {
|
||||
client := GetNewClient()
|
||||
collection := client.Database(location.Database).Collection(location.Collection)
|
||||
defer client.Disconnect(context.TODO())
|
||||
|
||||
@@ -83,8 +83,8 @@ func deleteDocument(location Location, filter interface{}) (int64, error) {
|
||||
return result.DeletedCount, nil
|
||||
}
|
||||
|
||||
func editDocument(location Location, jsons DocumentUpdate) (int64, error) {
|
||||
client := getNewClient()
|
||||
func EditDocument(location Location, jsons DocumentUpdate) (int64, error) {
|
||||
client := GetNewClient()
|
||||
collection := client.Database(location.Database).Collection(location.Collection)
|
||||
defer client.Disconnect(context.TODO())
|
||||
|
||||
Reference in New Issue
Block a user