WEBVTT 00:00.510 --> 00:05.100 So let's talk about a situation where a user up voted something, but they take it back. 00:05.130 --> 00:07.680 They actually didn't want to vote for that particular thing. 00:08.430 --> 00:10.080 How do we handle that in our API? 00:10.110 --> 00:13.380 Essentially, we need to be able to delete the vote object. 00:13.410 --> 00:14.490 But how do we do that? 00:15.000 --> 00:19.350 Well, what we can do is go back to our code here and we could create. 00:19.620 --> 00:25.410 If we go to our views, a completely separate class based view that, you know, instead of the create 00:25.620 --> 00:33.240 API view, it's going to be I believe the term is the destroy API view, but we have a much simpler 00:33.240 --> 00:34.560 way that we can do this. 00:34.570 --> 00:39.270 We can use a mix in to add a function animal, implement that. 00:40.010 --> 00:41.760 You know, delete functionality for us. 00:41.940 --> 00:45.630 So how to handle this is we have to be able to use mixin some. 00:45.650 --> 00:47.190 We've got to import this. 00:47.190 --> 00:49.110 So let's scroll to the very top here. 00:49.680 --> 00:53.250 And just after permissions, we're going to add mix ins. 00:53.540 --> 00:53.930 OK. 00:55.120 --> 01:01.840 And we're going to come to our vote, create and say that this also adheres to this is where we want 01:01.840 --> 01:07.180 to do Mexicans dot and then we want to do the destroy. 01:09.560 --> 01:11.570 Model Mixin. 01:12.360 --> 01:16.980 OK, so now that we have this, we'll go ahead and save that. 01:17.180 --> 01:22.520 And the way to implement the code, to destroy something, you could just, you know, go ahead and 01:22.520 --> 01:23.480 leave it as is. 01:23.480 --> 01:26.930 But I think it's best if we have a little function here. 01:26.930 --> 01:30.830 So I'm gonna go ahead and get down and create a new line. 01:30.920 --> 01:34.190 So we want to make a new function here. 01:34.340 --> 01:36.290 And this is going to be delete. 01:38.270 --> 01:41.300 OK, and here's the different parameters that we need, we need self. 01:41.900 --> 01:43.220 We need the request. 01:44.060 --> 01:50.150 We need the Star ARGs and we also need these star star. 01:51.250 --> 01:51.650 OK. 01:51.780 --> 01:54.030 W ARC's awesome. 01:54.120 --> 02:00.660 And a colon here and then basically we need to be allow someone to delete their upvote. 02:00.840 --> 02:06.180 Now the reason that we needed to implement this function is because we don't want them to be able to 02:06.180 --> 02:09.820 delete something that doesn't exist if they've never upvoted something. 02:09.840 --> 02:12.690 There's no vote object in the database, so there's no point for that. 02:13.230 --> 02:15.180 So similarly, like we did above. 02:15.210 --> 02:17.640 We need to check and see if a vote exists in the database. 02:17.670 --> 02:18.770 We already have the code for that. 02:18.780 --> 02:25.020 So going to copy this line where we say, you know, if this thing exists inside of the database K. 02:25.440 --> 02:32.250 So if a vote exists for this particular user on this particular post, then we will allow them to delete 02:32.250 --> 02:32.460 that. 02:32.490 --> 02:38.310 We're gonna say self get query set, dot delete. 02:38.760 --> 02:38.940 OK. 02:39.310 --> 02:40.890 That'll go ahead and get rid of it. 02:41.280 --> 02:44.900 And then we want the user to know that we that the delete was successful. 02:44.910 --> 02:48.490 So we are going to return a response. 02:49.190 --> 02:49.470 OK. 02:49.530 --> 02:51.170 And we've got to import the code for this. 02:51.180 --> 02:52.650 Let's go ahead and scroll to the top. 02:53.420 --> 02:55.370 And this is something else from the rest framework. 02:55.420 --> 02:58.780 So we're going to say from Rest. underscore framework. 03:00.660 --> 03:03.000 DOT response. 03:03.360 --> 03:07.140 We want to import capital response. 03:09.470 --> 03:13.370 So now we're going to return this response object in this response object. 03:13.480 --> 03:19.660 Basically, we can pass forward, you know, a particular code to them to say, you know, this is what 03:19.660 --> 03:20.050 happened. 03:20.050 --> 03:23.200 So we're gonna save status, which remember, there's all those status codes. 03:23.680 --> 03:27.220 And in this case, we want the status. 03:29.380 --> 03:34.670 Dot, I want status is something that we got to import, so let's go ahead and scroll back to the top 03:34.670 --> 03:35.150 here. 03:35.210 --> 03:37.670 After mix ends, we're going to import status. 03:39.680 --> 03:42.540 And we're going to say that we want to return back. 03:42.620 --> 03:52.640 H TTP underscore two oh four underscore no underscore, calm, all caps content. 03:53.170 --> 03:57.290 OK, so that is the status code to say, hey, there was nothing here. 03:57.950 --> 04:02.260 So, you know, we got rid of whatever it is that you were trying to get rid of. 04:02.570 --> 04:04.460 So that's if a vote exists. 04:04.760 --> 04:06.380 What if a vote does not exist? 04:06.410 --> 04:08.170 Well, that's our ELTs. 04:08.570 --> 04:13.940 And in that situation, we want to return or raise a validation error, just like we had before. 04:14.340 --> 04:15.590 So I'm going to copy and paste this. 04:15.620 --> 04:22.250 But rather than saying you early voted, we're gonna say, you know, something like you never voted 04:22.970 --> 04:26.640 for this post, silly thing. 04:26.880 --> 04:31.340 It's kind of fun to work with your API because the only the developers are gonna see this, the users 04:31.340 --> 04:37.420 never gonna see this information unless the developer somehow leaks this in their app or whoever is 04:37.430 --> 04:38.300 using your API. 04:38.360 --> 04:42.500 But anyway, it's kind of fun, I think, to put this kind of thing in there. 04:42.620 --> 04:47.090 So with this in place, let's go back and test this out. 04:47.120 --> 04:52.610 So if we reload the page here, you'll notice look what popped up, this awesome delete button. 04:52.880 --> 04:58.370 So the zappy code user, he can delete his vote just like this. 04:59.620 --> 05:05.320 And look, it is gone, and the way that we know that it's gone is if I can now make another vote. 05:05.350 --> 05:09.040 So if I hit posts, which is essentially creating a vote, I can do that. 05:09.110 --> 05:12.600 And it gives me an idea, meaning that it's added a vote to the database. 05:12.640 --> 05:14.200 But if I try and do it again and again. 05:14.830 --> 05:19.030 Well, it's it's, you know, not going to allow me to do that anymore so I can successfully do that. 05:19.040 --> 05:21.070 And if I delete once. 05:21.270 --> 05:21.570 OK. 05:22.690 --> 05:25.020 And then I come back and I try and delete again. 05:26.430 --> 05:29.750 It's gonna give me that area to say, hey, you never voted for this post, silly. 05:30.420 --> 05:32.190 I can't deleted if it's not there. 05:32.340 --> 05:32.650 So. 05:33.090 --> 05:38.190 And the way to really see that this is all working and this is really going to start tying things together. 05:38.190 --> 05:39.900 Let's go back to the post list. 05:40.020 --> 05:40.280 Right. 05:40.680 --> 05:42.660 If someone wants to see all the different posts. 05:42.690 --> 05:43.020 Yes. 05:43.020 --> 05:45.250 It's cool to see the title in the You Are. 05:45.930 --> 05:48.330 But we want to know how popular things are right now. 05:48.360 --> 05:51.790 Nothing is being listed for how popular things are. 05:51.810 --> 05:53.280 How many votes that they got. 05:53.640 --> 06:00.480 So if we go ahead and go back to our code, we can implement inside of our serializer as pi inside of 06:00.480 --> 06:01.540 the post serializer. 06:01.560 --> 06:04.800 A way to go get all the different votes that someone has. 06:05.340 --> 06:13.160 So we're gonna say that votes is equal to Serializer is with the S dot capital. 06:13.500 --> 06:14.760 Serializer. 06:16.190 --> 06:20.640 Make sure that that's capital serializer method. 06:21.640 --> 06:22.210 Field. 06:22.630 --> 06:29.140 So that means, you know, we want to include votes inside of here as something that's going to be, 06:29.400 --> 06:35.080 you know, returned back, but the way that the information will be generated is through a function. 06:35.110 --> 06:40.600 And so down here, still inside of this post serializer class, we're going to say, let's make a function 06:40.600 --> 06:41.410 called get. 06:42.190 --> 06:44.260 And then underscore votes. 06:44.890 --> 06:50.530 So basically, the name of your function has to be the same name of the variable that we have here. 06:50.530 --> 06:53.200 So get underscore whatever the name your variable is. 06:53.830 --> 07:00.460 And then inside of our get votes, it's going to have self and then also an object of the post object. 07:01.420 --> 07:04.750 And we're going to say here that we want to return. 07:05.930 --> 07:08.870 Capital vote dot objects. 07:09.170 --> 07:13.820 Basically, we want to go see how many vote objects there are for this particular vote. 07:13.850 --> 07:14.910 So that's what we're gonna say. 07:15.320 --> 07:17.750 Vote not objects. 07:17.930 --> 07:19.730 Dot filter. 07:20.850 --> 07:26.460 And say, give me all the vote objects for this particular posts or against a post is equal to post 07:27.060 --> 07:29.490 and then we just want the count of this. 07:29.910 --> 07:30.270 All right. 07:30.900 --> 07:36.660 So with just this piece here, if I go ahead and save this, I didn't have to change anything inside 07:36.660 --> 07:37.370 of my views. 07:37.380 --> 07:39.450 It's all handled via the serializer. 07:39.870 --> 07:44.010 If I come back and I reload the list of all these posts. 07:46.340 --> 07:46.970 It doesn't work. 07:47.000 --> 07:53.720 Let's see, we got hit rest frame worked out, serializer is has no attribute serializer of misspelled 07:53.780 --> 07:54.560 method field. 07:54.820 --> 07:55.010 OK. 07:55.100 --> 07:58.570 So it's telling me this at line seven of the serializer is the PI. 07:58.660 --> 08:02.000 So it's got a serialized pipeline seven. 08:02.480 --> 08:06.860 Let's change this to Method Field. 08:07.160 --> 08:07.540 OK. 08:07.700 --> 08:08.750 Let's save this. 08:09.360 --> 08:13.160 And now if I try and load this page, there we go. 08:13.160 --> 08:14.090 And look at this. 08:14.600 --> 08:15.590 We have the votes. 08:15.680 --> 08:19.890 So everything has no votes except for a hole. 08:20.180 --> 08:23.660 The very first post, which does have one vote currently from Nick. 08:23.690 --> 08:25.100 But it doesn't have one from zappy. 08:25.270 --> 08:30.680 So this will be a great way to show that our API is working because I'm logged into zappy code. 08:30.710 --> 08:34.730 I'm going to go to the one post slash vote. 08:34.790 --> 08:36.320 I'm going to then vote. 08:36.770 --> 08:37.760 And look, it's generated. 08:37.880 --> 08:40.490 Give me the tool one to say that it's created something. 08:40.940 --> 08:43.940 Now, if I go back to the post, let's look at that. 08:45.140 --> 08:48.660 Now, two upvotes for this particular post. 08:48.910 --> 08:52.790 Oh, you got to just be feeling great about what's going on here. 08:52.820 --> 08:56.540 We are getting really close to be done with our API. 08:56.990 --> 09:02.210 Really, the next step for us here is to allow someone who's made a post to say, you know what, I 09:02.210 --> 09:03.460 didn't really want to post that. 09:03.470 --> 09:05.330 How do I delete a post?