1
00:00:00,360 --> 00:00:00,750
All right.

2
00:00:00,780 --> 00:00:02,040
I saved the best for last.

3
00:00:02,390 --> 00:00:10,560
And now I want to show you how we can automatically update average rating and number of reviews in the

4
00:00:10,560 --> 00:00:15,450
product scheme when we remove, create or update review.

5
00:00:16,050 --> 00:00:18,570
We'll do that using aggregate pipeline.

6
00:00:18,870 --> 00:00:26,310
And since there's quite a bit to cover, I'll purposely split this up in multiple videos and utilize

7
00:00:26,310 --> 00:00:28,350
quite a few console logs.

8
00:00:28,410 --> 00:00:34,350
So first thing that I want to do is navigate to my product model.

9
00:00:35,280 --> 00:00:43,920
And if you remember, we're over here, the average rating and right below it or above, it doesn't

10
00:00:43,920 --> 00:00:44,520
really matter.

11
00:00:44,970 --> 00:00:51,810
I'm also going to add number of reviews, so a number of reviews, and by default, it's going to be

12
00:00:51,810 --> 00:00:56,310
exactly the same where we'll go with number and zero.

13
00:00:56,430 --> 00:00:59,580
Make sure to add a comma so you have a proper syntax.

14
00:01:00,060 --> 00:01:04,470
And with this in place now, I can navigate to a postman.

15
00:01:05,920 --> 00:01:13,450
I'm going to logged in as John, meaning I want to log in as admin just so we can start from scratch.

16
00:01:13,930 --> 00:01:19,180
So get me out of years and the moment that shows me that I have no arrears.

17
00:01:19,630 --> 00:01:20,740
That is awesome.

18
00:01:21,280 --> 00:01:27,070
And now I can navigate to review model and for time being.

19
00:01:27,490 --> 00:01:29,290
Or just log some stuff.

20
00:01:29,980 --> 00:01:35,230
So let's go below the next, and let's just go with review schema.

21
00:01:35,650 --> 00:01:36,520
Review your schema.

22
00:01:37,090 --> 00:01:39,120
And here, let's go with post.

23
00:01:39,970 --> 00:01:40,960
Like I said, save.

24
00:01:41,380 --> 00:01:48,370
We're going to go with async, and let's call a good old function for time being.

25
00:01:48,850 --> 00:01:58,600
As far as logic, let's just call log and let's say, post save book called and we want to do the same

26
00:01:58,600 --> 00:01:59,020
thing.

27
00:02:00,070 --> 00:02:06,640
Where I'll copy and paste and instead of post, save or go with post remove.

28
00:02:07,950 --> 00:02:11,039
And of course, then here I want to change it where post.

29
00:02:12,610 --> 00:02:19,780
Remove, and now let's go back to the postman, and let's test that out, whether everything works.

30
00:02:20,290 --> 00:02:23,740
So I mean, navigate back or create a new review.

31
00:02:24,400 --> 00:02:26,080
I mean, it doesn't really matter again.

32
00:02:26,080 --> 00:02:31,240
Let me just double check whether the product exists the way it is that's not going to make sense.

33
00:02:31,570 --> 00:02:34,340
So notice here, I have no other reviews.

34
00:02:34,390 --> 00:02:40,240
Zero, that makes sense since we're just set it up online as far as creating the review.

35
00:02:41,870 --> 00:02:42,680
Grab it here.

36
00:02:43,070 --> 00:02:47,250
Copy and paste, we created an right away.

37
00:02:47,840 --> 00:02:49,340
Update and delete.

38
00:02:49,760 --> 00:02:52,700
Just so you can see that we're calling those shots.

39
00:02:53,150 --> 00:02:55,580
So now let me go here with update review.

40
00:02:56,930 --> 00:02:59,600
And I'm going to be looking for one.

41
00:03:00,620 --> 00:03:03,910
And let me just get the review, Heidi.

42
00:03:04,400 --> 00:03:08,810
So again, this is not a product that now I'm looking for your idea here.

43
00:03:10,300 --> 00:03:12,940
So let me update it correctly through one.

44
00:03:13,850 --> 00:03:16,130
And now let me remove the reviewer as well.

45
00:03:16,460 --> 00:03:20,690
So let me copy and paste the idea for you.

46
00:03:21,260 --> 00:03:24,920
Let me call it, and now I have successfully removed that review.

47
00:03:25,310 --> 00:03:33,980
Now what I'm interested, though, is the council where essentially I want to see that my post service

48
00:03:33,980 --> 00:03:38,060
called as well as post remove book is called.

49
00:03:38,480 --> 00:03:44,870
And if you're seeing a council through council logs now, in my case, I might have more because I think

50
00:03:44,870 --> 00:03:48,440
I send the same request twice to patch one.

51
00:03:48,770 --> 00:03:54,000
But essentially, you should see the first one when we call the post reviews.

52
00:03:54,050 --> 00:03:56,990
This is where we created one when we patch it.

53
00:03:57,080 --> 00:04:01,850
We also call post-stroke and then when we remove, when we go delete.

54
00:04:02,820 --> 00:04:12,360
Then we also call post remove this, why it was so crucial for us when we set up the Delete review,

55
00:04:12,870 --> 00:04:20,040
as well as update review to use review, not save and review doctrine move again.

56
00:04:20,640 --> 00:04:23,010
I kept saying that I'll explain this later.

57
00:04:23,820 --> 00:04:31,950
Later is now where I'm explaining that in order to trigger those hooks, we need to use, remove as

58
00:04:31,950 --> 00:04:34,410
well as save in this case.

59
00:04:34,920 --> 00:04:40,440
And with this in place, the last thing that I want to cover in this video is the static methods.

60
00:04:40,830 --> 00:04:46,410
So if you remember when we were setting up the user, we set up a method.

61
00:04:46,950 --> 00:04:48,780
Now method is on the instance.

62
00:04:49,200 --> 00:04:50,100
What does that mean?

63
00:04:50,460 --> 00:04:57,630
Well, that means that in the user controller, once I create the user, I can use that method.

64
00:04:58,350 --> 00:05:00,060
Now let me double check what is the method?

65
00:05:00,270 --> 00:05:06,630
Compare passwords so that one, if I remember correctly, we used in the logging, so it may go to user

66
00:05:06,630 --> 00:05:07,440
controller.

67
00:05:08,020 --> 00:05:13,590
Let me take a look at the log in one of that user, or I'm sorry, that was actually off.

68
00:05:14,100 --> 00:05:14,760
My apologies.

69
00:05:15,030 --> 00:05:17,760
So let me go to log in and remember.

70
00:05:18,750 --> 00:05:20,100
This was the function correct?

71
00:05:20,520 --> 00:05:25,860
We created a instance here, and then on the instance, we can use the method.

72
00:05:26,640 --> 00:05:34,170
So that's one option we can take, another option we can take, we can create a static method on schema.

73
00:05:34,860 --> 00:05:38,850
Now what that means is that we're not calling it on the instance that we create.

74
00:05:39,120 --> 00:05:41,790
We actually call it on the schema.

75
00:05:42,330 --> 00:05:44,130
And the way it's going to look like.

76
00:05:45,060 --> 00:05:50,070
First, we need to go with the schema name, market status review schema.

77
00:05:50,430 --> 00:05:52,510
So let's go here with review schema.

78
00:05:52,530 --> 00:05:56,550
But then we go with statics and you'll see how we call it.

79
00:05:57,070 --> 00:06:01,190
But in order to set it up, we go with static, not methods again.

80
00:06:01,770 --> 00:06:04,680
And I'm repeating myself, but this is extremely important.

81
00:06:05,130 --> 00:06:11,160
Where in the user we want with methods and then the function that in this case, since I want to call

82
00:06:11,160 --> 00:06:16,890
it on the schema, not on the instance, I'm going to go with statics and then whatever name you want.

83
00:06:17,250 --> 00:06:19,650
So in my case, it's going to be calculated.

84
00:06:20,720 --> 00:06:21,350
Average.

85
00:06:22,540 --> 00:06:23,380
And rating.

86
00:06:24,310 --> 00:06:29,740
Now, once I have this in place, this is going to be a sink function and it's going to be looking for

87
00:06:29,740 --> 00:06:37,890
one thing, it's going to be looking for a product I.D. because again, there's going to be a more functional

88
00:06:38,200 --> 00:06:38,700
bit later.

89
00:06:39,160 --> 00:06:46,480
For now, what I want you to do is just log the product I.D. or basically whatever we are getting there.

90
00:06:46,750 --> 00:06:48,850
So it bingo here with the product.

91
00:06:49,600 --> 00:06:52,050
And you know what I think I forgot about?

92
00:06:52,060 --> 00:06:52,780
Here are the.

93
00:06:54,010 --> 00:07:02,140
And once I have both of them marching band, we're good, and now in order to call this static method,

94
00:07:02,680 --> 00:07:05,920
we need to go back to the hook and then remember.

95
00:07:06,910 --> 00:07:15,160
When we go with this, what we access, when we access the actual schema correct now in order to call

96
00:07:15,160 --> 00:07:23,080
the static method that schema has, we need to go with refresh, but we will right away go with a wait

97
00:07:23,860 --> 00:07:24,740
in line.

98
00:07:24,760 --> 00:07:30,790
We need to go with constructor, constructor, dot and then whatever is the name.

99
00:07:30,910 --> 00:07:31,720
So in my case?

100
00:07:32,740 --> 00:07:35,620
We're going to go here with calculate rating like so.

101
00:07:36,530 --> 00:07:37,700
So let's do it this way.

102
00:07:38,180 --> 00:07:45,110
And as far as the product I.D., well, we have the product property correct.

103
00:07:45,500 --> 00:07:46,700
Now what is this referencing?

104
00:07:47,150 --> 00:07:50,370
It's referencing a product and what is the value?

105
00:07:50,750 --> 00:07:51,520
It's a product.

106
00:07:51,900 --> 00:07:52,310
Correct.

107
00:07:52,730 --> 00:08:00,960
So I want to invoke we calculate average rating and as far as the product that we pass it using that

108
00:08:00,960 --> 00:08:02,720
dot and then the product.

109
00:08:03,110 --> 00:08:06,440
And we want to do the same thing well, well in a remove.

110
00:08:07,010 --> 00:08:14,240
So let me take this or move the console.log and I'll invoke calculate average rating.

111
00:08:14,810 --> 00:08:15,980
And what's the next step?

112
00:08:16,370 --> 00:08:21,830
Well, again, in the postman, I simply want to create a review.

113
00:08:22,100 --> 00:08:23,840
So the product will stay the same.

114
00:08:24,380 --> 00:08:26,990
I'll update you one more time.

115
00:08:27,230 --> 00:08:29,240
So in this case, the idea is going to be different.

116
00:08:30,170 --> 00:08:33,710
I mean, it should be correct because we remove the previous one.

117
00:08:34,490 --> 00:08:36,289
So we go here with update.

118
00:08:37,650 --> 00:08:39,990
And then lastly, I want to wonder more as well.

119
00:08:41,240 --> 00:08:42,440
So I want to remove it.

120
00:08:43,230 --> 00:08:43,820
Say.

121
00:08:45,070 --> 00:08:46,420
I should have known of years.

122
00:08:46,960 --> 00:08:57,050
And back in council, I should see three times a product I.D. when we create the review, when we update

123
00:08:57,050 --> 00:09:01,120
the review and also when we remove the review.

124
00:09:01,690 --> 00:09:11,440
So we're leaving a review for the product and a reverse I.D. is going to match that product.

125
00:09:12,310 --> 00:09:19,270
And the reason why we're passing in because eventually there will be one more logic than just simple

126
00:09:19,270 --> 00:09:19,930
console.log.

127
00:09:20,470 --> 00:09:27,370
And if you can see all three console logs, we are in good shape and we can move on to the next step.

