Deleting a Review

Learn how the delete operation works on the basis of review ID.

Setting up the controller

Imagine that after writing a review, the user changes their mind. The ability to delete their review would be beneficial if they made a mistake or decided they no longer wanted the review to be public. All we’d need is a command that searches for reviews matching the review_id and deletes them from the database. It’s that easy!

/ Delete a review
func DeleteReview() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
reviewId := c.Param("review_id")
defer cancel()
i,erro:= strconv.Atoi(reviewId)
if erro != nil {
// Handle error
}
result, err := reviewCollection.DeleteOne(ctx, bson.M{"review_id": i})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"Status": http.StatusInternalServerError,
"Message": "error",
"Data": map[string]interface{}{"data": err.Error()}})
return
}
if result.DeletedCount < 1 {
c.JSON(http.StatusNotFound,
gin.H{
" Status": http.StatusNotFound,
" Message": "error",
" Data": map[string]interface{}{"data": "Review with specified ID not found!"}},
)
return
}
c.JSON(http.StatusOK,
gin.H{
"Status": http.StatusOK,
"Message": "success",
"Data": map[string]interface{}{"data": "Your review was successfully deleted!"}},
)
}
}
The reviewControllers.go file

Setting up the router

Following that, we must create the route required to communicate with this controller. This will be a simple ...