mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Merge pull request #4 from Floriansylvain/dev
Added article edition to backend
This commit is contained in:
@@ -51,6 +51,7 @@ func initArticlesRoutes(r *gin.Engine) {
|
|||||||
articlesRouter.GET("/", internal.GetAllArticlesHandler)
|
articlesRouter.GET("/", internal.GetAllArticlesHandler)
|
||||||
articlesRouter.GET("/:id", internal.GetArticleHandler)
|
articlesRouter.GET("/:id", internal.GetArticleHandler)
|
||||||
articlesRouter.POST("/:id", internal.AddArticleHandler)
|
articlesRouter.POST("/:id", internal.AddArticleHandler)
|
||||||
|
articlesRouter.PATCH("/:id", internal.EditArticleHandler)
|
||||||
articlesRouter.DELETE("/:id", internal.DeleteArticleHandler)
|
articlesRouter.DELETE("/:id", internal.DeleteArticleHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
IdName string `json:"id_name" bson:"id_name"`
|
IdName string `json:"id_name" bson:"id_name"`
|
||||||
Date int64 `json:"date" bson:"date"`
|
Date int64 `json:"date" bson:"date"`
|
||||||
Content interface{} `json:"content" bson:"content"`
|
Content gin.H `json:"content" bson:"content"`
|
||||||
}
|
|
||||||
|
|
||||||
type DelArticle struct {
|
|
||||||
IdName string `json:"id_name" bson:"id_name"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var articlesLocation = Location{Database: "gohcms", Collection: "articles"}
|
var articlesLocation = Location{Database: "gohcms", Collection: "articles"}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetAllArticlesHandler(c *gin.Context) {
|
func GetAllArticlesHandler(c *gin.Context) {
|
||||||
documents, err := getDocuments(articlesLocation, bson.D{})
|
documents, err := getDocuments(articlesLocation, gin.H{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendBadRequest(c, err.Error())
|
SendBadRequest(c, err.Error())
|
||||||
return
|
return
|
||||||
@@ -20,7 +20,7 @@ func GetAllArticlesHandler(c *gin.Context) {
|
|||||||
func GetArticleHandler(c *gin.Context) {
|
func GetArticleHandler(c *gin.Context) {
|
||||||
articleID := c.Params.ByName("id")
|
articleID := c.Params.ByName("id")
|
||||||
article, err := getUniqueDocument(articlesLocation,
|
article, err := getUniqueDocument(articlesLocation,
|
||||||
bson.D{{Key: "id_name", Value: articleID}})
|
gin.H{"id_name": articleID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendBadRequest(c, "The ID provided doesn't match any article.")
|
SendBadRequest(c, "The ID provided doesn't match any article.")
|
||||||
return
|
return
|
||||||
@@ -60,24 +60,37 @@ func AddArticleHandler(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteArticleHandler(c *gin.Context) {
|
func DeleteArticleHandler(c *gin.Context) {
|
||||||
var delArticle DelArticle
|
id := c.Params.ByName("id")
|
||||||
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)
|
deleteCount, err := deleteDocument(articlesLocation, gin.H{"id_name": id})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendBadRequest(c, "Could not correctly marshal the article ID.")
|
SendBadRequest(c, "Could not delete document into DB.")
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteCount, err := deleteDocument(articlesLocation, document)
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, "Could not insert document into DB.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if deleteCount != 0 {
|
||||||
SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
|
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.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-2
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
@@ -13,6 +14,11 @@ type Location struct {
|
|||||||
Collection string
|
Collection string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DocumentUpdate struct {
|
||||||
|
Filter gin.H `json:"filter"`
|
||||||
|
Update gin.H `json:"update"`
|
||||||
|
}
|
||||||
|
|
||||||
func getNewClient() *mongo.Client {
|
func getNewClient() *mongo.Client {
|
||||||
client, err := mongo.Connect(
|
client, err := mongo.Connect(
|
||||||
context.TODO(),
|
context.TODO(),
|
||||||
@@ -44,7 +50,7 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
|||||||
|
|
||||||
cursor, err := collection.Find(context.TODO(), filter)
|
cursor, err := collection.Find(context.TODO(), filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return results, errors.New("something is wrong with filter to find the document")
|
return results, errors.New("something is wrong with filter to find the document.")
|
||||||
}
|
}
|
||||||
for cursor.TryNext(context.TODO()) {
|
for cursor.TryNext(context.TODO()) {
|
||||||
results = append(results, cursor.Current)
|
results = append(results, cursor.Current)
|
||||||
@@ -71,8 +77,24 @@ func deleteDocument(location Location, filter interface{}) (int64, error) {
|
|||||||
|
|
||||||
result, err := collection.DeleteOne(context.TODO(), filter)
|
result, err := collection.DeleteOne(context.TODO(), filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("something is wrong with filter to delete the document")
|
return 0, errors.New("something is wrong with filter to delete the document.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.DeletedCount, nil
|
return result.DeletedCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func editDocument(location Location, jsons DocumentUpdate) (int64, error) {
|
||||||
|
client := getNewClient()
|
||||||
|
collection := client.Database(location.Database).Collection(location.Collection)
|
||||||
|
defer client.Disconnect(context.TODO())
|
||||||
|
|
||||||
|
result, err := collection.UpdateOne(context.TODO(), jsons.Filter, gin.H{"$set": jsons.Update})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if result.MatchedCount == 0 {
|
||||||
|
return 0, errors.New("cannot find document matching filter.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ModifiedCount, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user