Exercise: Update Movie Reviews
Practice updating a document in MongoDB using the Go API.
We'll cover the following
Problem Statement
Updating a document is a basic operation in a MongoDB project. This exercise tests your knowledge of updating a document in the collection.
Suppose a reviewer, Alice
, has changed their mind about a specific movie. They need help to update their review.
The review model
package models
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Reviews struct {
Id primitive.ObjectID `bson:"_id"`
Movie_id int `json:"movie_id"`
Review_id int `json:"review_id"`
Review *string `json:"review" validate:"required"`
Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"`
Reviewer_id string `json:"reviewer_id"`
}
The review route
package routes
import (
controllers "shive-app/controllers"
"shive-app/middleware"
"github.com/gin-gonic/gin"
)
func ReviewRoutes(router gin.Engine) {
router.Use(middleware.AuthenticateUser())
router.POST("reviews/addreview", controllers.AddReview())
router.GET("reviews/filter", controllers.ViewAMovieReviews())
router.DELETE("reviews/:reviewer_id", controllers.DeleteReview())
router.GET("reviews/user_reviews/:reviewer_id", controllers.AllUserReviews()),
// Update Movie Review
router.GET("reviews/editreview/:reviewer_id", controllers.editReviews())
}
Get hands-on with 1400+ tech skills courses.