mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
getUniqueDocument implementation
This commit is contained in:
@@ -19,18 +19,14 @@ 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")
|
||||||
articles, err := getDocuments(articlesLocation,
|
article, err := getUniqueDocument(articlesLocation,
|
||||||
bson.D{{Key: "id_name", Value: articleID}})
|
bson.D{{Key: "id_name", Value: articleID}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendBadRequest(c, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(articles) == 0 {
|
|
||||||
SendBadRequest(c, "The ID provided doesn't match any article.")
|
SendBadRequest(c, "The ID provided doesn't match any article.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var parsedArticle Article
|
var parsedArticle Article
|
||||||
bson.Unmarshal(articles[0], &parsedArticle)
|
bson.Unmarshal(article, &parsedArticle)
|
||||||
c.JSON(http.StatusOK, parsedArticle)
|
c.JSON(http.StatusOK, parsedArticle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,18 @@ func getDocuments(location Location, filter interface{}) ([][]byte, error) {
|
|||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUniqueDocument(location Location, filter interface{}) ([]byte, error) {
|
||||||
|
client := getNewClient()
|
||||||
|
collection := client.Database(location.Database).Collection(location.Collection)
|
||||||
|
defer client.Disconnect(context.TODO())
|
||||||
|
|
||||||
|
singleResult := collection.FindOne(context.TODO(), filter)
|
||||||
|
if singleResult.Err() != nil {
|
||||||
|
return nil, errors.New("no document was found, check the filter or the location.")
|
||||||
|
}
|
||||||
|
return singleResult.DecodeBytes()
|
||||||
|
}
|
||||||
|
|
||||||
func deleteDocument(location Location, filter interface{}) (int64, error) {
|
func deleteDocument(location Location, filter interface{}) (int64, error) {
|
||||||
client := getNewClient()
|
client := getNewClient()
|
||||||
collection := client.Database(location.Database).Collection(location.Collection)
|
collection := client.Database(location.Database).Collection(location.Collection)
|
||||||
|
|||||||
Reference in New Issue
Block a user