WEBVTT 00:02.460 --> 00:09.420 In this lesson we're going to complete the rest backend of our project by creating two more view classes. 00:09.420 --> 00:16.410 The first of a two will allow us to retrieve update and delete single and switch instances while the 00:16.410 --> 00:19.430 other one will allow our users to live like on the elsewhere. 00:19.470 --> 00:20.900 They like the most. 00:21.060 --> 00:27.410 The first view that we're going to create is going to use their retrieve update destroyed API view classes. 00:27.840 --> 00:41.460 So class password retrieve update destroy API view and EHR generics dot retrieve update destroy API 00:41.490 --> 00:42.040 view. 00:42.130 --> 00:43.680 And here we can define quite he said. 00:43.950 --> 00:54.230 As I said Dot objects dot all realize that plaza is the answer to it. 00:55.820 --> 01:02.960 Utilize it in the permission classes that are going to be the same ones we've used up here and that's 01:02.960 --> 01:12.230 of courses for of use reasons lets not create a proper endpoint for the view and actually error can 01:12.230 --> 01:18.370 change the name of this endpoint to answer create to be a bit more consistent. 01:18.420 --> 01:22.240 So like so a comma. 01:22.400 --> 01:30.500 This is going to be answered in detail and of course we want to call answer retrieve update delete API 01:30.500 --> 01:31.190 view. 01:31.190 --> 01:33.500 The End Point is going to be elsewhere. 01:34.220 --> 01:36.880 And then we're just going to use a primary key. 01:36.970 --> 01:40.900 So integer primary key we are now good to go. 01:40.900 --> 01:46.280 So let's round the development server let's move to Chrome as always. 01:46.520 --> 01:50.500 Let's go to API questions 01:53.450 --> 01:55.210 let's login. 01:55.430 --> 02:03.550 I mean and from here we can use these questions as log to get the only answer but actually that we've 02:03.550 --> 02:04.090 created. 02:04.090 --> 02:06.690 So the less answers. 02:07.150 --> 02:12.850 And of course we know that these answer as I.D. one which we could have actually guessed it being the 02:12.850 --> 02:13.810 only one we created. 02:13.810 --> 02:15.290 But let's be explicit. 02:15.290 --> 02:28.020 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 02:28.170 --> 02:38.530 deleted if we want to that's now looking it's random so random user and of course because you are not 02:38.560 --> 02:43.990 the author of these question we can't neither delete or update the instance even though of course the 02:43.990 --> 02:47.240 tenth point accepts these kind of requests. 02:47.410 --> 02:49.030 So let's answer one of these questions. 02:49.060 --> 02:49.730 OK. 02:49.930 --> 02:59.980 Let's go to questions and let's answer this second question we have so questions like answer. 03:01.030 --> 03:03.550 So do you like jazz music. 03:03.550 --> 03:06.540 Yeah it's really powerful. 03:08.630 --> 03:10.610 Post and here it is. 03:10.610 --> 03:12.920 So let's go to API. 03:13.200 --> 03:23.230 That's where is with primary key to how we now can update these this implementing this endpoint was 03:23.230 --> 03:25.100 in fact really simple. 03:25.120 --> 03:32.020 Let's now see if we can create a like end point for answers to respect to the rest architectural guidelines 03:32.050 --> 03:37.190 and specifically our resources are endless in arrest API. 03:37.240 --> 03:45.810 We can now create an API view to manage only two kinds of age GDP requests post and delete it post the 03:45.810 --> 03:51.740 request that sent it to the endpoint which will serve D EPA view will mean that a user is leaving a 03:51.760 --> 03:58.780 like on an answer while a delete request instead will mean that a user is removing the like. 03:58.900 --> 04:02.140 So the end is going to be exactly the same. 04:02.140 --> 04:11.100 The only thing changing is going to be the kind of to request classic and elsewhere like EPA view. 04:12.070 --> 04:14.950 And here we can use API view directly. 04:15.580 --> 04:25.950 But of course we first need to import it so air from rest framework not views import API. 04:26.140 --> 04:38.740 If you're we've only one dot and you're also going to need startles and you're from rest framework dot 04:39.760 --> 04:40.950 response. 04:41.110 --> 04:43.520 Import response. 04:43.700 --> 04:48.460 Now don't add it we can add first of all see that is it class and emission classes 04:52.760 --> 04:56.560 and yet we only need is authenticated like so. 04:56.710 --> 05:05.030 And now we can implement deep post method so that again by sending a post request for a specific answer 05:05.030 --> 05:05.870 What is this. 05:05.880 --> 05:07.930 Users will be able to leave alike. 05:08.090 --> 05:17.030 And of course this primary key will be passed as an endpoint parameter so as work equals get object 05:17.330 --> 05:22.240 or photo for and swear we became equals okay. 05:22.340 --> 05:27.550 Let's also get the user equals self doubt request that user. 05:27.830 --> 05:29.360 And now to live a lie. 05:29.390 --> 05:39.230 We can simply do answer dot voters not add we're going to add the user and then we're not safe. 05:39.230 --> 05:45.470 That is one more thing that we need to do and that's to add the request to the context because as you 05:45.470 --> 05:52.390 see in the were to see that as their class we are getting the request from the context in order to check. 05:52.410 --> 05:56.510 Basically if the request user is already voted a question. 05:56.610 --> 06:03.400 So let's go back to the views that by file and here we now can do to utilize it. 06:03.750 --> 06:09.550 Context equals a dictionary request request. 06:10.080 --> 06:19.440 So we're passing this request object and now she that a equals self not to proceed is class so we can 06:19.440 --> 06:22.680 pass the answer instance. 06:22.800 --> 06:29.450 And in that context sterilized context we are now finally ready to return. 06:29.730 --> 06:38.250 The response which is going to contain you realize that got data and as a stop loss it's not there's 06:38.340 --> 06:40.300 not HDP 200. 06:40.510 --> 06:44.320 Okay so this post method was to add a lie to an answer. 06:44.560 --> 06:57.140 Let's no CEO to remove a like so we can define delete self request and keep. 06:57.260 --> 07:03.950 Well as a matter of fact we can copy all this code because the implementation is basically the same 07:04.340 --> 07:10.070 but yeah you going to use remove and well everything else stays the same. 07:10.090 --> 07:14.570 We're getting it and so it is thus we are getting to use it was making the request we were removing 07:14.660 --> 07:21.150 that user from the voters square we set of that specific ones for instance where then saving what is 07:21.160 --> 07:21.690 this. 07:22.160 --> 07:28.100 And then we returning this serialized data for that and so we've stopped those code 200. 07:28.190 --> 07:31.500 OK let's not leave the class to an endpoint. 07:31.520 --> 07:40.370 So you're as that's by and yet I can just copy this path like so which is going to be called password 07:40.900 --> 07:41.800 like. 07:42.120 --> 07:50.330 And of course he's going to call on sweet like EPA if you notice you and it to distinguish the 20 points. 07:50.520 --> 07:57.020 So I'd like let's now restart the development server and let's check our way of but if in the works 07:59.980 --> 08:07.030 so these is the house where we have primary key to at the moment we have zero likes. 08:07.050 --> 08:14.750 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 08:14.750 --> 08:21.800 up yet we see an aged AML form even though if we switch to raw data you'll see that there is no content 08:21.810 --> 08:22.510 right. 08:22.620 --> 08:26.370 And this is in fact more appropriate for these specific end point. 08:26.460 --> 08:30.860 We are not going to send any data as you can see where logged in as random user. 08:30.870 --> 08:39.330 So by pressing post we are adding a like you see as random and if you know try to send another post 08:39.330 --> 08:47.010 request you see that likes count stays at one but by pressing delete by making a deed request to the 08:47.010 --> 08:49.230 simple point it goes back to zero. 08:49.440 --> 08:56.040 And if you now make another post request it goes back to one let's log out as random and let's log working 08:56.310 --> 09:01.820 as admin so let's make another post request. 09:01.950 --> 09:08.000 Let's see now if two now back in business to your code I notice that there is a small thing that we 09:08.000 --> 09:14.270 can do to improve the code yet I've written self to request due to user but in fact we have our request 09:14.270 --> 09:18.920 object is passed as parameter so let's just remove self 09:22.760 --> 09:25.850 and to this you can see everything clearly works as expected. 09:25.880 --> 09:31.770 Even using self though to request dot user but this way I think the code is a bit cleaner. 09:31.850 --> 09:38.600 The very last thing that we now need to do is to go back to the settings not by file because we want 09:38.600 --> 09:40.070 our data to be badly needed. 09:40.130 --> 09:43.820 Therefore here we now need to add default 09:46.490 --> 10:01.290 education class which is going to be rest framework that larger nation dot page no larger nation with 10:02.400 --> 10:04.610 page size. 10:04.710 --> 10:10.200 And just to be developer friendly we can now set to sort of make our life easier while developing the 10:10.190 --> 10:13.050 front end of our single page application. 10:13.050 --> 10:18.900 Now that the backend of our Question Time single page application is completed it is finally time to 10:18.900 --> 10:20.530 test everything at once. 10:20.560 --> 10:22.290 We're going to do that in the next lesson.