In this lesson we're going to complete the rest backend of our project by creating two more view classes.
The first of a two will allow us to retrieve update and delete single and switch instances while the
other one will allow our users to live like on the elsewhere.
They like the most.
The first view that we're going to create is going to use their retrieve update destroyed API view classes.
So class password retrieve update destroy API view and EHR generics dot retrieve update destroy API
view.
And here we can define quite he said.
As I said Dot objects dot all realize that plaza is the answer to it.
Utilize it in the permission classes that are going to be the same ones we've used up here and that's
of courses for of use reasons lets not create a proper endpoint for the view and actually error can
change the name of this endpoint to answer create to be a bit more consistent.
So like so a comma.
This is going to be answered in detail and of course we want to call answer retrieve update delete API
view.
The End Point is going to be elsewhere.
And then we're just going to use a primary key.
So integer primary key we are now good to go.
So let's round the development server let's move to Chrome as always.
Let's go to API questions
let's login.
I mean and from here we can use these questions as log to get the only answer but actually that we've
created.
So the less answers.
And of course we know that these answer as I.D. one which we could have actually guessed it being the
only one we created.
But let's be explicit.
So let's go to API answers and then 1 and as you can see we can now update the instance and we can also
deleted if we want to that's now looking it's random so random user and of course because you are not
the author of these question we can't neither delete or update the instance even though of course the
tenth point accepts these kind of requests.
So let's answer one of these questions.
OK.
Let's go to questions and let's answer this second question we have so questions like answer.
So do you like jazz music.
Yeah it's really powerful.
Post and here it is.
So let's go to API.
That's where is with primary key to how we now can update these this implementing this endpoint was
in fact really simple.
Let's now see if we can create a like end point for answers to respect to the rest architectural guidelines
and specifically our resources are endless in arrest API.
We can now create an API view to manage only two kinds of age GDP requests post and delete it post the
request that sent it to the endpoint which will serve D EPA view will mean that a user is leaving a
like on an answer while a delete request instead will mean that a user is removing the like.
So the end is going to be exactly the same.
The only thing changing is going to be the kind of to request classic and elsewhere like EPA view.
And here we can use API view directly.
But of course we first need to import it so air from rest framework not views import API.
If you're we've only one dot and you're also going to need startles and you're from rest framework dot
response.
Import response.
Now don't add it we can add first of all see that is it class and emission classes
and yet we only need is authenticated like so.
And now we can implement deep post method so that again by sending a post request for a specific answer
What is this.
Users will be able to leave alike.
And of course this primary key will be passed as an endpoint parameter so as work equals get object
or photo for and swear we became equals okay.
Let's also get the user equals self doubt request that user.
And now to live a lie.
We can simply do answer dot voters not add we're going to add the user and then we're not safe.
That is one more thing that we need to do and that's to add the request to the context because as you
see in the were to see that as their class we are getting the request from the context in order to check.
Basically if the request user is already voted a question.
So let's go back to the views that by file and here we now can do to utilize it.
Context equals a dictionary request request.
So we're passing this request object and now she that a equals self not to proceed is class so we can
pass the answer instance.
And in that context sterilized context we are now finally ready to return.
The response which is going to contain you realize that got data and as a stop loss it's not there's
not HDP 200.
Okay so this post method was to add a lie to an answer.
Let's no CEO to remove a like so we can define delete self request and keep.
Well as a matter of fact we can copy all this code because the implementation is basically the same
but yeah you going to use remove and well everything else stays the same.
We're getting it and so it is thus we are getting to use it was making the request we were removing
that user from the voters square we set of that specific ones for instance where then saving what is
this.
And then we returning this serialized data for that and so we've stopped those code 200.
OK let's not leave the class to an endpoint.
So you're as that's by and yet I can just copy this path like so which is going to be called password
like.
And of course he's going to call on sweet like EPA if you notice you and it to distinguish the 20 points.
So I'd like let's now restart the development server and let's check our way of but if in the works
so these is the house where we have primary key to at the moment we have zero likes.
So let's go to the cement bind but let's add like at the end because of the way the browser API is set
up yet we see an aged AML form even though if we switch to raw data you'll see that there is no content
right.
And this is in fact more appropriate for these specific end point.
We are not going to send any data as you can see where logged in as random user.
So by pressing post we are adding a like you see as random and if you know try to send another post
request you see that likes count stays at one but by pressing delete by making a deed request to the
simple point it goes back to zero.
And if you now make another post request it goes back to one let's log out as random and let's log working
as admin so let's make another post request.
Let's see now if two now back in business to your code I notice that there is a small thing that we
can do to improve the code yet I've written self to request due to user but in fact we have our request
object is passed as parameter so let's just remove self
and to this you can see everything clearly works as expected.
Even using self though to request dot user but this way I think the code is a bit cleaner.
The very last thing that we now need to do is to go back to the settings not by file because we want
our data to be badly needed.
Therefore here we now need to add default
education class which is going to be rest framework that larger nation dot page no larger nation with
page size.
And just to be developer friendly we can now set to sort of make our life easier while developing the
front end of our single page application.
Now that the backend of our Question Time single page application is completed it is finally time to
test everything at once.
We're going to do that in the next lesson.