From e6e15392d1164df7ed075b0347e44323936654cb Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 18:05:53 +0100 Subject: [PATCH 1/4] Removed useless code for DeleteArticleHandler --- internal/articlesBusiness.go | 4 ---- internal/articlesHandlers.go | 21 +++++---------------- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/internal/articlesBusiness.go b/internal/articlesBusiness.go index 54c22db..0b2b6f5 100644 --- a/internal/articlesBusiness.go +++ b/internal/articlesBusiness.go @@ -10,10 +10,6 @@ type Article struct { 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 { diff --git a/internal/articlesHandlers.go b/internal/articlesHandlers.go index c870aff..7e5da79 100644 --- a/internal/articlesHandlers.go +++ b/internal/articlesHandlers.go @@ -9,7 +9,7 @@ import ( ) func GetAllArticlesHandler(c *gin.Context) { - documents, err := getDocuments(articlesLocation, bson.D{}) + documents, err := getDocuments(articlesLocation, gin.H{}) if err != nil { SendBadRequest(c, err.Error()) return @@ -20,7 +20,7 @@ func GetAllArticlesHandler(c *gin.Context) { func GetArticleHandler(c *gin.Context) { articleID := c.Params.ByName("id") article, err := getUniqueDocument(articlesLocation, - bson.D{{Key: "id_name", Value: articleID}}) + gin.H{"id_name": articleID}) if err != nil { SendBadRequest(c, "The ID provided doesn't match any article.") return @@ -60,22 +60,11 @@ func AddArticleHandler(c *gin.Context) { } 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 - } + id := c.Params.ByName("id") - document, err := bson.Marshal(delArticle) + deleteCount, err := deleteDocument(articlesLocation, gin.H{"id_name": id}) 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.") + SendBadRequest(c, "Could not delete document into DB.") return } From bb324d3df44517bf979cca4e3f9015b118508460 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 18:45:21 +0100 Subject: [PATCH 2/4] Added edition workflow --- cmd/main.go | 1 + internal/articlesHandlers.go | 20 ++++++++++++++++++++ internal/database.go | 26 ++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index a886200..d2f2a4d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -51,6 +51,7 @@ func initArticlesRoutes(r *gin.Engine) { articlesRouter.GET("/", internal.GetAllArticlesHandler) articlesRouter.GET("/:id", internal.GetArticleHandler) articlesRouter.POST("/:id", internal.AddArticleHandler) + articlesRouter.PATCH("/:id", internal.EditArticleHandler) articlesRouter.DELETE("/:id", internal.DeleteArticleHandler) } diff --git a/internal/articlesHandlers.go b/internal/articlesHandlers.go index 7e5da79..7d34805 100644 --- a/internal/articlesHandlers.go +++ b/internal/articlesHandlers.go @@ -70,3 +70,23 @@ func DeleteArticleHandler(c *gin.Context) { SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount)) } + +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.") + } +} diff --git a/internal/database.go b/internal/database.go index da6e592..b190406 100644 --- a/internal/database.go +++ b/internal/database.go @@ -4,6 +4,7 @@ import ( "context" "errors" + "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -13,6 +14,11 @@ type Location struct { Collection string } +type DocumentUpdate struct { + Filter gin.H `json:"filter"` + Update gin.H `json:"update"` +} + func getNewClient() *mongo.Client { client, err := mongo.Connect( context.TODO(), @@ -44,7 +50,7 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) { cursor, err := collection.Find(context.TODO(), filter) 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()) { results = append(results, cursor.Current) @@ -71,8 +77,24 @@ func deleteDocument(location Location, filter interface{}) (int64, error) { result, err := collection.DeleteOne(context.TODO(), filter) 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 } + +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 +} From 6593f38eb53722b5884afe686fbadf82a2039919 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 18:46:35 +0100 Subject: [PATCH 3/4] Added some errors accuracy on del article handler --- internal/articlesHandlers.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/articlesHandlers.go b/internal/articlesHandlers.go index 7d34805..dff2757 100644 --- a/internal/articlesHandlers.go +++ b/internal/articlesHandlers.go @@ -68,7 +68,11 @@ func DeleteArticleHandler(c *gin.Context) { return } - SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount)) + 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) { From 39afc0c066986b34c5286ee278137ff746777054 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Tue, 27 Dec 2022 19:54:46 +0100 Subject: [PATCH 4/4] Fixed Article's "Content" field type --- internal/articlesBusiness.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/articlesBusiness.go b/internal/articlesBusiness.go index 0b2b6f5..4366153 100644 --- a/internal/articlesBusiness.go +++ b/internal/articlesBusiness.go @@ -1,13 +1,14 @@ package internal import ( + "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 interface{} `json:"content" bson:"content"` + IdName string `json:"id_name" bson:"id_name"` + Date int64 `json:"date" bson:"date"` + Content gin.H `json:"content" bson:"content"` } var articlesLocation = Location{Database: "gohcms", Collection: "articles"}