Okay.
We have our application almost ready.
Our API is very functional at this point, but there is thing that I would like to do for our backend
to be more functional.
So basically what we have here is we might have a ratings and we have might have a movies here.
So if I will use this, we have a movie.
But basically what I will need to do is to get some information about the ratings for that specific
movie.
So what we can do is we can do what we've done in our previous section is to include that rating in
the sorry here in the movie Serializer, include that ratings and do related name here.
But in fact, I don't need to see all the stars object in our serializer because if I will see a list
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
useful.
What we would like to see here or display on the page is to display how many ratings we have and also
what is the average rating for that specific movie.
So how can we achieve that?
We need to create our own function and we can do it in many places and we've done it already in the
views.
That's our function and we use it.
But also we can go to the models and we can use a function here.
We can create our function and refer to that function.
Also, we can do the same for the serializer.
Let's do it in models and I will show you how easily we can create a new function and how we can use
it in serializer.
So let's say.
We would like to have a function for a number of ratings.
Number.
Of.
Ratings like that.
And then we have a self and that self will refer to the current movie.
So if we'll call that function on the movie, then it the self will be the movie itself.
So what we need to do, we will need to get a ratings and then we need to check how many ratings we
have for that specific movie so we can do ratings here.
That's a local variable and then we'll do rating.
And then I can do objects.
Filter.
By movie is equal to self.
So what I'm doing here is I'm selecting all ratings in our database that have movie like this movie.
So that means for each movie we will run this function and the self is that specific movie.
So we will filter for each movie here.
And then what I can do is I can return length, which is length of, of an array.
Length of rating.
Like that.
So if I will save it now, our function is ready now, so we'll copy that and I can go to serializers
and I can include it here.
So number of writing, it's a function available on our movie.
That function will be available for us in The Serializer.
So let's go back here and test it now.
And you can see here number of rating one, number of rating one.
So if I will go.
To ratings like that.
And we need to use cat.
So we have two ratings.
Let's remove this rating for.
So we'll do rating for Slash, and then I will use method lead.
Removing that we have only supposed to have only one rating now.
And indeed, we have one rating.
So let's come back here, refresh it, and we can see a number of ratings for this movie is zero, a
number of rating for this, it's one.
So in that very easy way, we could include extra information and that's very custom information that
we've created manually here.
So we don't need to stick with the methods that are available for serializer or for the other models.
We can create our own.
So let's do another function and use something similar.
So definition and we can do average.
Rating.
Same 12.
And then what we need to do, we need to copy this.
So.
I like that.
We'll select all our ratings and then we'll create another variable and I will call it sum, which is
at the beginning, it's zero.
So what we will do is we will loop through the ratings and we will sum it up.
So for rating in ratings, we'll do a loop here and then I will do some.
Add rating like that.
So basically what I'm doing here is I set up some to zero at the beginning, so it will be zero.
And then I select as before method, I select all the ratings for that specific movie, and then I loop
through all of them.
So this ratings here is an array of all the ratings for that movie, and we loop through them, and
every time I loop, I have a local variable rating available and I just add this rating to the sum.
So basically every single time we loop it and we add it here.
So at the end of this line, I have already summed up the result here.
So this is the sum.
But in fact I would like to see an average.
So average I can do return sum divide it by.
Length.
Rating.
So I will have some and I divide it by number of ratings we have in our system.
But that might throw an error because if the length is zero, then I am not allowed to divide by zero
here.
So what I need to do is I need to check it if.
Is bigger than zero, then I can do this.
Hulls.
Return zero or whatever, we we can return null or whatever we have here.
So basically that's what we have here.
So once again, I set up assume a zero.
Then I select all the ratings, I loop through them and add each rating to the sum, and then I return
sum divided by number of ratings for that specific movie.
And I do only that when we have at least one rating.
Otherwise I just return zero.
Because if I will have zero divided by zero then it will throw an error.
So I will save it.
Now let's come back to our postman and test how it works.
Uh, actually, actually, I forgot to put average rating in our serializer, so we need to go here
in the serializer.
Another field.
Average rating.
Coming back here, Postman.
Sending again so unsupported operand.
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
here.
But that's the rating is an object rating.
But we would like to add rating stars.
So stars is the real rating rating has more things like movie and user.
But we would like to use the stars for some some of the average rating.
So we'll save it once again and then test it.
And you can see average rating, three, average rating zero.
So we have our two methods here and that object is very optimized for our front end.
We'll ask for the list of movies and you can see here a list of movies.
This one movie will contain ID title.
We can actually exclude this ID if we like, but we have title description number of ratings and average
rating here so we can actually display that on the on the screen.
And we will we can do that using only one query to our database.
So we don't need to query a.
Movies and then ask for ratings and calculate that on the front end.
That would be also possible.
But this is more optimized and using one query, we can get all that data coming back to us.
So this is now functional.
We have ratings, we have movies, we have all the information we need.
In the next videos, we will focus on doing our user login and logout and registering new users.