WEBVTT

00:02.100 --> 00:02.640
Okay.

00:02.970 --> 00:06.150
We have our application almost ready.

00:06.180 --> 00:15.330
Our API is very functional at this point, but there is thing that I would like to do for our backend

00:15.330 --> 00:17.380
to be more functional.

00:17.400 --> 00:25.010
So basically what we have here is we might have a ratings and we have might have a movies here.

00:25.020 --> 00:28.290
So if I will use this, we have a movie.

00:28.290 --> 00:36.090
But basically what I will need to do is to get some information about the ratings for that specific

00:36.090 --> 00:36.690
movie.

00:36.720 --> 00:46.680
So what we can do is we can do what we've done in our previous section is to include that rating in

00:46.680 --> 00:54.030
the sorry here in the movie Serializer, include that ratings and do related name here.

00:54.030 --> 01:03.460
But in fact, I don't need to see all the stars object in our serializer because if I will see a list

01:03.460 --> 01:10.000
of ratings here and let's say star two and half to one and a half and so on and so on, that's not really

01:10.000 --> 01:10.540
useful.

01:10.540 --> 01:20.020
What we would like to see here or display on the page is to display how many ratings we have and also

01:20.020 --> 01:23.890
what is the average rating for that specific movie.

01:23.890 --> 01:26.380
So how can we achieve that?

01:26.410 --> 01:34.660
We need to create our own function and we can do it in many places and we've done it already in the

01:34.660 --> 01:35.020
views.

01:35.020 --> 01:37.000
That's our function and we use it.

01:37.000 --> 01:41.950
But also we can go to the models and we can use a function here.

01:41.950 --> 01:45.130
We can create our function and refer to that function.

01:45.130 --> 01:48.400
Also, we can do the same for the serializer.

01:48.790 --> 01:55.750
Let's do it in models and I will show you how easily we can create a new function and how we can use

01:55.750 --> 01:56.950
it in serializer.

01:56.950 --> 01:58.360
So let's say.

01:59.180 --> 02:03.920
We would like to have a function for a number of ratings.

02:03.950 --> 02:04.940
Number.

02:06.390 --> 02:07.020
Of.

02:08.820 --> 02:10.620
Ratings like that.

02:10.740 --> 02:16.170
And then we have a self and that self will refer to the current movie.

02:16.170 --> 02:22.440
So if we'll call that function on the movie, then it the self will be the movie itself.

02:22.440 --> 02:31.050
So what we need to do, we will need to get a ratings and then we need to check how many ratings we

02:31.050 --> 02:35.430
have for that specific movie so we can do ratings here.

02:35.460 --> 02:40.200
That's a local variable and then we'll do rating.

02:42.850 --> 02:45.730
And then I can do objects.

02:49.150 --> 02:49.930
Filter.

02:52.300 --> 02:55.620
By movie is equal to self.

02:56.630 --> 03:06.160
So what I'm doing here is I'm selecting all ratings in our database that have movie like this movie.

03:06.170 --> 03:12.080
So that means for each movie we will run this function and the self is that specific movie.

03:12.080 --> 03:15.140
So we will filter for each movie here.

03:15.140 --> 03:23.330
And then what I can do is I can return length, which is length of, of an array.

03:23.360 --> 03:25.310
Length of rating.

03:26.830 --> 03:27.730
Like that.

03:29.100 --> 03:37.830
So if I will save it now, our function is ready now, so we'll copy that and I can go to serializers

03:37.830 --> 03:40.620
and I can include it here.

03:40.620 --> 03:45.360
So number of writing, it's a function available on our movie.

03:45.390 --> 03:49.230
That function will be available for us in The Serializer.

03:49.230 --> 03:52.020
So let's go back here and test it now.

03:53.380 --> 03:57.700
And you can see here number of rating one, number of rating one.

03:57.710 --> 03:59.770
So if I will go.

04:01.850 --> 04:04.550
To ratings like that.

04:07.080 --> 04:09.750
And we need to use cat.

04:11.090 --> 04:12.610
So we have two ratings.

04:12.620 --> 04:15.080
Let's remove this rating for.

04:15.120 --> 04:20.780
So we'll do rating for Slash, and then I will use method lead.

04:22.030 --> 04:26.620
Removing that we have only supposed to have only one rating now.

04:34.130 --> 04:36.200
And indeed, we have one rating.

04:36.200 --> 04:42.500
So let's come back here, refresh it, and we can see a number of ratings for this movie is zero, a

04:42.500 --> 04:45.200
number of rating for this, it's one.

04:45.560 --> 04:53.900
So in that very easy way, we could include extra information and that's very custom information that

04:53.920 --> 04:56.570
we've created manually here.

04:56.570 --> 05:03.290
So we don't need to stick with the methods that are available for serializer or for the other models.

05:03.290 --> 05:05.180
We can create our own.

05:05.180 --> 05:09.710
So let's do another function and use something similar.

05:09.710 --> 05:13.280
So definition and we can do average.

05:14.970 --> 05:15.750
Rating.

05:16.770 --> 05:18.030
Same 12.

05:18.630 --> 05:23.310
And then what we need to do, we need to copy this.

05:23.760 --> 05:24.720
So.

05:31.450 --> 05:32.110
I like that.

05:32.130 --> 05:39.420
We'll select all our ratings and then we'll create another variable and I will call it sum, which is

05:39.420 --> 05:41.100
at the beginning, it's zero.

05:41.100 --> 05:46.170
So what we will do is we will loop through the ratings and we will sum it up.

05:46.380 --> 05:56.730
So for rating in ratings, we'll do a loop here and then I will do some.

06:00.050 --> 06:03.510
Add rating like that.

06:03.530 --> 06:11.300
So basically what I'm doing here is I set up some to zero at the beginning, so it will be zero.

06:11.300 --> 06:19.160
And then I select as before method, I select all the ratings for that specific movie, and then I loop

06:19.160 --> 06:20.180
through all of them.

06:20.180 --> 06:25.730
So this ratings here is an array of all the ratings for that movie, and we loop through them, and

06:25.730 --> 06:32.660
every time I loop, I have a local variable rating available and I just add this rating to the sum.

06:32.660 --> 06:37.160
So basically every single time we loop it and we add it here.

06:37.160 --> 06:45.200
So at the end of this line, I have already summed up the result here.

06:45.770 --> 06:47.240
So this is the sum.

06:47.240 --> 06:50.570
But in fact I would like to see an average.

06:50.570 --> 06:57.740
So average I can do return sum divide it by.

07:00.080 --> 07:00.770
Length.

07:02.300 --> 07:03.130
Rating.

07:04.730 --> 07:10.520
So I will have some and I divide it by number of ratings we have in our system.

07:10.670 --> 07:19.790
But that might throw an error because if the length is zero, then I am not allowed to divide by zero

07:19.790 --> 07:20.300
here.

07:20.300 --> 07:23.870
So what I need to do is I need to check it if.

07:32.930 --> 07:38.930
Is bigger than zero, then I can do this.

07:43.280 --> 07:43.970
Hulls.

07:48.380 --> 07:55.370
Return zero or whatever, we we can return null or whatever we have here.

07:55.670 --> 07:59.090
So basically that's what we have here.

07:59.090 --> 08:03.800
So once again, I set up assume a zero.

08:03.800 --> 08:11.660
Then I select all the ratings, I loop through them and add each rating to the sum, and then I return

08:11.690 --> 08:15.680
sum divided by number of ratings for that specific movie.

08:15.680 --> 08:21.350
And I do only that when we have at least one rating.

08:21.380 --> 08:23.300
Otherwise I just return zero.

08:23.300 --> 08:28.790
Because if I will have zero divided by zero then it will throw an error.

08:28.790 --> 08:30.500
So I will save it.

08:30.500 --> 08:34.640
Now let's come back to our postman and test how it works.

08:37.590 --> 08:44.640
Uh, actually, actually, I forgot to put average rating in our serializer, so we need to go here

08:44.640 --> 08:46.230
in the serializer.

08:46.290 --> 08:47.400
Another field.

08:47.850 --> 08:49.020
Average rating.

08:50.260 --> 08:52.570
Coming back here, Postman.

08:53.820 --> 08:57.480
Sending again so unsupported operand.

08:57.960 --> 09:09.510
I use Add like this for a rating, but in fact I should do if I go back to the models, I'm adding rating

09:09.510 --> 09:09.870
here.

09:09.870 --> 09:12.210
But that's the rating is an object rating.

09:12.210 --> 09:16.320
But we would like to add rating stars.

09:16.590 --> 09:23.970
So stars is the real rating rating has more things like movie and user.

09:23.970 --> 09:28.440
But we would like to use the stars for some some of the average rating.

09:28.440 --> 09:33.000
So we'll save it once again and then test it.

09:33.360 --> 09:37.620
And you can see average rating, three, average rating zero.

09:38.040 --> 09:45.180
So we have our two methods here and that object is very optimized for our front end.

09:45.210 --> 09:49.410
We'll ask for the list of movies and you can see here a list of movies.

09:49.410 --> 09:53.710
This one movie will contain ID title.

09:53.710 --> 10:00.730
We can actually exclude this ID if we like, but we have title description number of ratings and average

10:00.730 --> 10:05.290
rating here so we can actually display that on the on the screen.

10:05.290 --> 10:10.270
And we will we can do that using only one query to our database.

10:10.270 --> 10:12.540
So we don't need to query a.

10:13.360 --> 10:17.940
Movies and then ask for ratings and calculate that on the front end.

10:17.950 --> 10:19.590
That would be also possible.

10:19.600 --> 10:27.490
But this is more optimized and using one query, we can get all that data coming back to us.

10:27.670 --> 10:29.770
So this is now functional.

10:29.770 --> 10:33.120
We have ratings, we have movies, we have all the information we need.

10:33.130 --> 10:42.340
In the next videos, we will focus on doing our user login and logout and registering new users.
