Professional Documents
Culture Documents
Servidor - 3
Servidor - 3
Web
Daniel Sánchez Ruiz
Unidad Profesional Interdisciplinaria de Ingeniería campus Tlaxcala
Leaving movie reviews
Besides searching movies, users can leave reviews for them. So let’s
create the routes to post, put and delete reviews. post is for creating a
review, put is for editing a review, and delete for deleting reviews.
Leaving movie reviews
We then add a route ‘/review’ which handles post, put and delete http
requests all within this one route call.
That is to say, if the ‘/review’ route receives a post http request to add a
review, we call apiPostReview.
If ‘ /review ’ receives a put http request to edit a review, call
apiUpdateReview.
And finally, if ‘ /review ’ receives a delete http request to delete a
review, call apiDeleteReview.
Leaving movie reviews
Leaving movie reviews
We get information from the request ’ s body parameter. Previously in
MoviesController, we got information from the request ’ s query
parameter as we extracted data from the URL e.g. req.query.title. This
time, we retrieve the data from the body of the request.
In the Vue frontend of the app, we call this endpoint with something
like: this._http.post<any>("http://localhost:5000/api/v1/movies/review",
data)
Leaving movie reviews
Leaving movie reviews
We then call ReviewsDAO.updateReview and pass in user_id to ensure that the user
who is updating the view is the one who has created it.
if (ReviewResponse.modifiedCount === 0) {
throw new Error('unable to update review. User may not be original poster');
}
updateReview returns a document ReviewResponse which contains the property
modifiedCount. modifiedCount contains the number of modified documents. We
check modifiedCount to ensure that it is not zero. If it is, it means the review has not
been updated and we throw an error.
Leaving movie reviews
Leaving movie reviews
Leaving movie reviews
We import mongodb to get access to ObjectId. We need ObjectId to
convert an id string to a MongoDB Object id later on.
The rest of the code, notice that it is similar to MoviesDAO. If
ReviewsDAO.reviews is not filled, we then access the database reviews
collection. Note that if the reviews collection doesn ’ t yet exist in the
database, MongoDB automatically creates it for us.
Design patterns
MVC (Model-View-Controller) is a pattern in software design
commonly used to implement user interfaces, data, and controlling
logic. It emphasizes a separation between the software's business logic
and display.
This "separation of concerns" provides for a better division of labor and
improved maintenance. Some other design patterns are based on MVC,
such as MVVM (Model-View-Viewmodel), MVP (Model-View-
Presenter), and MVW (Model-View-Whatever).
Design patterns
The three parts of the MVC software-design pattern can be described as
follows:
Imagine a simple shopping list app. All we want is a list of the name, quantity
and price of each item we need to buy this week.
Design patterns
Design patterns
The Model
The model defines what data the app should contain. If the state of this
data changes, then the model will usually notify the view (so the display
can change as needed) and sometimes the controller (if different logic is
needed to control the updated view).
Going back to our shopping list app, the model would specify what data
the list items should contain — item, price, etc. — and what list items
are already present.
Design patterns
The View