1
00:00:00,470 --> 00:00:00,950
All right, guys.

2
00:00:00,950 --> 00:00:04,070
So now we're going to move on to some extra features.

3
00:00:04,070 --> 00:00:06,140
And I want to start with reviews.

4
00:00:06,170 --> 00:00:12,200
Now to create a review, we're going to have to have an end point in our back end that we hit, which

5
00:00:12,200 --> 00:00:19,600
is going to be slash API, slash products, slash whatever the ID of the product and then slash reviews.

6
00:00:19,610 --> 00:00:23,990
So let's go into back end and we'll start off with the controller.

7
00:00:23,990 --> 00:00:27,020
So let's go into product controller.

8
00:00:27,590 --> 00:00:31,580
And let's see, we're going to go down to the bottom here.

9
00:00:32,720 --> 00:00:34,580
Actually, let's just copy.

10
00:00:34,910 --> 00:00:36,500
We'll just copy this.

11
00:00:38,590 --> 00:00:41,380
And let's say this is going to be two.

12
00:00:43,170 --> 00:00:46,020
Create a new review.

13
00:00:47,180 --> 00:00:51,530
And it's going to be a post request.

14
00:00:51,860 --> 00:00:57,020
And like I said, it's going to be API products ID and then slash review.

15
00:00:57,320 --> 00:01:01,450
Or let's do reviews and it's going to be private.

16
00:01:01,460 --> 00:01:03,230
So you have to be logged in.

17
00:01:03,840 --> 00:01:06,150
And let's call this create.

18
00:01:06,540 --> 00:01:09,420
We'll say create product review.

19
00:01:12,170 --> 00:01:14,810
And let's see.

20
00:01:14,840 --> 00:01:17,810
We'll go ahead and get from the body what we need.

21
00:01:17,810 --> 00:01:22,340
I'm just going to get rid of actually, no, we do need to get the product.

22
00:01:22,340 --> 00:01:24,950
But above that, let's get.

23
00:01:25,630 --> 00:01:31,540
The rating and the comment, okay, because they're going to be able to send a number rating and then

24
00:01:31,540 --> 00:01:32,530
also a comment.

25
00:01:32,530 --> 00:01:34,390
That's what a review contains.

26
00:01:34,990 --> 00:01:40,060
And if we look in our model, so in our product model, we have a review schema.

27
00:01:40,060 --> 00:01:45,100
So rating comment, the name is going to come from the user.

28
00:01:45,100 --> 00:01:52,480
And then we also have the user ID, So here we're getting the product.

29
00:01:52,480 --> 00:01:55,060
So we can actually just keep this the way it is.

30
00:01:55,090 --> 00:02:01,900
Then we're going to check for the product and if there is one, we want to check to see if it's already

31
00:02:01,900 --> 00:02:07,780
reviewed because we don't want the same person to be able to review the product twice.

32
00:02:07,900 --> 00:02:10,389
In fact, we can go ahead and just put this in.

33
00:02:10,539 --> 00:02:13,420
So we have a variable called already reviewed.

34
00:02:13,450 --> 00:02:17,920
We're going to say product dot reviews and then dot find.

35
00:02:17,920 --> 00:02:23,140
Okay, So we're not finding through the products, we're finding through the reviews, which is basically

36
00:02:23,140 --> 00:02:25,900
like a like a sub collection.

37
00:02:25,900 --> 00:02:33,010
And in here we're saying find passing in a callback function for each review, we're going to say if

38
00:02:33,010 --> 00:02:39,580
that review user and we're going to turn that user object ID into a string and match it to the logged

39
00:02:39,580 --> 00:02:41,860
in users ID as a string.

40
00:02:41,980 --> 00:02:46,300
So if that turns out to be true, that means it's already reviewed.

41
00:02:46,300 --> 00:02:51,670
And if it's already reviewed, we're going to send a 400 and just say product already reviewed.

42
00:02:51,700 --> 00:02:54,280
Then we're going to create the review object here.

43
00:02:54,280 --> 00:02:58,300
So the name again comes from the user, the logged in user's name.

44
00:02:58,300 --> 00:03:00,910
The rating comes from the body.

45
00:03:00,910 --> 00:03:02,200
It's going to be sent with the form.

46
00:03:02,200 --> 00:03:07,510
We're just making sure it's a number and also the comment and then the user is going to be the user

47
00:03:07,510 --> 00:03:10,660
ID, which comes from the request object.

48
00:03:11,020 --> 00:03:11,410
Okay?

49
00:03:11,410 --> 00:03:17,290
Then once we do that, we're going to take our product reviews and we're going to push onto it the new

50
00:03:17,290 --> 00:03:18,160
review.

51
00:03:18,160 --> 00:03:25,420
And we want to also set the Num reviews to whatever the length of the reviews because remember, products

52
00:03:25,420 --> 00:03:32,480
have a num reviews field and then to get the rating, we're going to factor in the new rating by using

53
00:03:32,480 --> 00:03:34,670
reduce and adding them all together.

54
00:03:34,670 --> 00:03:39,170
So all the ratings together and then dividing it by the length of reviews.

55
00:03:39,170 --> 00:03:41,690
So that will give us the overall rating.

56
00:03:41,690 --> 00:03:47,630
Then we can save it and we'll send a 201, which means something was created and we'll just say review

57
00:03:47,630 --> 00:03:48,950
added else.

58
00:03:48,950 --> 00:03:52,880
If there's no review, then we get a 404 just like with everything else.

59
00:03:52,970 --> 00:03:53,300
Okay.

60
00:03:53,300 --> 00:03:55,750
So pretty straightforward.

61
00:03:55,760 --> 00:03:57,590
Let's go ahead and now.

62
00:03:58,300 --> 00:03:59,680
Export create.

63
00:04:00,540 --> 00:04:01,660
I created.

64
00:04:01,660 --> 00:04:03,010
It should be create.

65
00:04:04,900 --> 00:04:06,640
Create product review.

66
00:04:08,170 --> 00:04:08,530
Okay.

67
00:04:08,530 --> 00:04:10,450
And then we need to go to our route.

68
00:04:10,450 --> 00:04:19,089
So let's go to routes and we want to go to our product routes and let's first off import, create product

69
00:04:19,089 --> 00:04:26,920
review, and then we're going to add let's go down here and we're going to say router dot route and

70
00:04:26,920 --> 00:04:34,330
it's going to be slash colon ID slash reviews, post requests, protect middleware and call create product

71
00:04:34,330 --> 00:04:35,050
review.

72
00:04:35,320 --> 00:04:37,810
So that's it for the back end.

73
00:04:37,810 --> 00:04:43,870
So now in the next video, we're going to jump into the front end and start to implement reviews there.

