Hey there everyone that they share.
And the previous video we saw that how we can have a simple method which can help us to add a review
and technically it is also acting as updating the review as well.
So add an update are happening all in the same case and we don't need to give any put request or anything
if somebody sends us just the post data.
We are managing everything on here.
Now, further down the road, I would like to have a delete method as well, so let's go ahead and have
a copy of this one.
So I'll just copy this and I'll say give me a paste of that and let's go ahead and shrink this one and
add this one.
So this one is going to be delete a review or delete review in simple.
So this one is delete review.
Okay.
Let's see how we're going to work with that.
Now again, of course, you are not going to send me this much of the information you would either be
sending me just the product ID now how we're going to product, how we expect the product idea to come
up to us if it comes in the requested body, that is also fine.
But you are going to notice in such cases, especially for the delete, it is kind of a recommended
that you come up or you just get that into the query.
So we're going to go ahead and say request or query DOT product ID, okay, we have extracted that and
now we have access to this product ID again.
Now again, if you want to access that in the params, totally a new query totally on you.
You just have to convey this information to the front end guy.
We don't need to construct an object like this because that's not necessary.
So there we go.
Now, obviously, we need to find a product based on this product ID, however you are receiving doesn't
really matter.
Okay.
Once I have gotten access to this one, now I need to update the reviews itself.
So let's go ahead and use a simple method.
I'm going to go ahead and say, hey, I will construct this reviews area.
The goal or what I'm trying to do here is I have this access to this reviews entire array.
I want to use a filter method and filter out the review which is being sent by that user.
That's all I'm trying to do.
So I'll be using the same method here.
So I'll be saying that, hey, reviews, let's go ahead and update that just like this.
And let's go ahead and say, hey, product dot reviews and let's use a filter method on that filter.
There we go.
Now filter actually goes and gives you again the same option that let's go ahead and call this one as
simple rev this time and this is going to work exactly same.
So we can actually go ahead and use this exact same line just like that.
Now Rev works, this filter actually works a little bit different than the other methods.
It actually allows to pass on all the values, but wherever the value or the condition matches out,
it doesn't allow you to pass that thing.
So the object we are looping through all the values, the object on which the review matches.
That means this is the review by the user.
It will not allow it to pass that.
So now we have a reviews, a new fresh error, which is not having the values which we are supposed
to delete.
So that's exactly what we want to save in the database.
We don't want this entirety of the stuff and F and LS and all of that.
So let's go ahead and remove that part.
We don't need that.
Okay.
Now I have this reviews.
I obviously have to adjust these ratings and stuff, which I just deleted.
And the only part that got deleted in that is this number of reviews.
So let's copy that and update that.
So let's go ahead and say, hey, now we have this number of reviews and that will be calculated from
this fresh reviews that we got after filtering and we can actually go ahead and calculator length on
that, if I can write that properly.
There we go.
Okay.
So this is all updated number of reviews and we got this rating as well.
So rating is accumulation fine.
So this is again recalculated.
That is absolutely fine.
I'm happy with that part.
Now all I need to do is insert a product dot save.
I have to update the data because remember we are having a lot of reviews and all those a number of
ratings and all these data.
So let's go ahead and work on with that.
So I'll remove this part.
Let's go ahead.
And instead of save, it would be better to say that update the product.
Product.
There we go.
Okay.
So how we're going to you update that again, this is a database operation and we're going to go ahead
and use simple product model itself.
And it has a property of find by ID and update.
There we go.
Now, first, we need to provide the ID itself and we are holding that ID in available.
So there we go, product ID.
Let's go ahead and copy that.
Copy that.
And I'll give you this product ID.
There we go.
Now, I need to provide you the data that you should be updating, and that data is actually inside
an object itself.
So what are the data that you should be updating first?
You should be updating the reviews, so update that, then you should be updating the ratings because
I have recalculated that ratings.
Ratings and what else?
A number of reviews also.
So go ahead and update that.
So these are the three things that needs to be updated there.
So this is all happy.
Now, also, we need to pass on the previous options as well.
So this is all there and there we go.
Now let's provide the options as well.
So do we want the new value to be returned?
Yes, definitely.
We want run validators as well.
Run validators.
Yep.
I want to do that.
And also optionally again, use, find and modify.
There we go.
I want to turn off this flag as well.
Let's save this.
So we are having product by it.
We are providing what product to update, what with, what data to update and optional, some of the
options going in.
Now, again, the response is pretty obvious and pretty good as well.
Okay.
So this is the basic method of how we are actually getting that and these are the only things we need
to worry about.
And basically we need to give us a method itself.
Now, optionally, we are going to design one more method because here is the situation for that.
Although whenever we are sending an individual product response, we are sending all the data through
it, we are sending up what all the reviews are, what all the ratings and everything is.
And technically, we shouldn't be sending the repetitive or iterative data when somebody in the front
end can grab it, actually.
But maybe, maybe for some reason in the front end, Guy has asked that, Hey, can you give me a simple
method in which if I provide you product ID, you just return me all the reviews.
It would be easier to display all the reviews in a separate component.
So we are going to design a method for him just for that.
And this is just to explain the stuff that yes, sometimes user or front end guy may ask you that I
don't want the entirety of the information.
Just give me some selected data on some of the route.
And yes, we can always go ahead and do that.
So we're going to say and say that get only reviews.
For one product.
So yeah, this is a really long one, but this is how we are going to do it.
We're going to say that this is going to be wrapping around Big Promise and we're going to say request
a response and the next just like that.
There we go.
And seems like there is a little bit of the issue.
Yep, there is a little bit of issue too fast.
There we go.
And I need to wrap everything around the big promise.
There we go.
That's why I usually copy and paste this stuff and this needs to be going a sync.
And after the async, this needs to be wrapped around like that and then I can fire my callback.
OC looks good now, so we're going to go ahead and say that, hey, you need to send me somehow the
ID of that product, of that one product.
So let's just say we're going to hold that into a product.
And how that is going to come up is up to you and how you talk to the front end guy.
Really, we don't really matter that much.
We have mastered that art already, so we're going to say, fine, buy ID and go ahead and just change
it in case things changes.
I'm going to say that, hey, let's go ahead and grab it into the query.
So request dot, query, dot ID or you can call this one as product ID however you like to go ahead
and use that.
Now we have this product, we are holding that.
So let's go ahead and send just only the reviews there.
So it's really simple.
We are going to say Rez dot status and we are going to go ahead and say 200 and let's send it JSON response.
There we go.
In the JSON response, we are going to say success is true in this case.
And the reviews, which is an error itself, is going to come up from the product DOT reviews.
There we go.
Make sure it is a TSS reviews and that's all what we got to do now in case somebody in the frontend
guy comes up and say, Hey, I need a specific model, I'm designing a special component in which just
the brand names are coming in.
So hey, can you send me just specifically the brand name or just the category up there?
You can go ahead and design those method.
And yes, these requests are very, very common.
They come up quite a lot.
In the next video.
We're going to go ahead and design the routes for that because we haven't yet implemented them in the
route.
Let's go ahead and catch up in the next video.