1 00:00:01,330 --> 00:00:03,670 Onto our last row handler of delete. 2 00:00:03,670 --> 00:00:08,020 Now when a user makes a DELETE request we're not going to actually delete the underlying order. 3 00:00:08,020 --> 00:00:13,990 Instead we're going to update its status over two canceled so remember a order has many different statuses. 4 00:00:13,990 --> 00:00:17,410 We can take a look at them inside of our order file in the models directory. 5 00:00:17,500 --> 00:00:20,240 So we've got order status right there if we do a command click on it. 6 00:00:20,350 --> 00:00:21,610 We'll go to the definition of that. 7 00:00:21,610 --> 00:00:26,430 You know the thing can be created awaiting payment complete or canceled. 8 00:00:26,440 --> 00:00:30,790 So when we say that we're going to delete an order again we're not actually deleting it out of the database. 9 00:00:30,790 --> 00:00:34,670 Instead we're just marking in as canceled now that mind. 10 00:00:34,840 --> 00:00:37,900 Does it still make sense to have this be a DELETE request. 11 00:00:38,020 --> 00:00:42,900 Maybe not really a DELETE request kind of implies that we're actually deleting a resource. 12 00:00:43,000 --> 00:00:44,640 We're not really deleting a resource here. 13 00:00:44,650 --> 00:00:50,800 Instead maybe it would make more sense to make this a put or a patch to say that we are updating a property 14 00:00:50,920 --> 00:00:54,060 and just happens that we are updating the property over to canceled. 15 00:00:54,220 --> 00:00:59,680 So maybe it's not the best to say that this is going to be a delete nonetheless. 16 00:00:59,860 --> 00:01:02,120 It's what we're going to do. 17 00:01:02,400 --> 00:01:06,180 Anyways back over to our editor inside of our roots directory. 18 00:01:06,180 --> 00:01:12,110 We're going to find that delete T.S. file so inside of here the logic we're going to write out is going 19 00:01:12,110 --> 00:01:18,330 to look very similar to what we did back inside of show to make sure that a user is authenticated. 20 00:01:18,330 --> 00:01:21,900 We're going to make sure that we find some order inside of our database. 21 00:01:21,940 --> 00:01:26,450 We're also going to make sure that the user who is making this request owns this order as well. 22 00:01:26,610 --> 00:01:27,320 And that's pretty much it. 23 00:01:27,330 --> 00:01:32,080 After we find that do those checks we can update the order status and we're complete. 24 00:01:32,280 --> 00:01:41,150 So first off couple of imports at the top are going to import order from up one directory models order. 25 00:01:41,150 --> 00:01:46,580 We probably also need that order status enum so we can properly set it to cancel that as well to also 26 00:01:46,580 --> 00:01:50,190 get order status. 27 00:01:50,260 --> 00:01:52,650 We need a couple of different middleware from our common module. 28 00:01:52,690 --> 00:02:01,980 So add in and import for require off from our common module and I think that should probably be it right 29 00:02:01,980 --> 00:02:03,050 now. 30 00:02:03,050 --> 00:02:09,730 So inside the root handler itself I'll apply the require auth middleware then inside the root handler 31 00:02:10,820 --> 00:02:16,490 first are going to pull off the order I.D. off of the request parameters object so we'll get order I.D. 32 00:02:18,020 --> 00:02:23,490 from wrecked dock ramps and then like I just said same thing we just did inside of show Find the order 33 00:02:24,590 --> 00:02:25,750 make sure we found in order. 34 00:02:25,790 --> 00:02:27,490 Make sure the user owns the order. 35 00:02:27,530 --> 00:02:39,070 And then finally apply the update they'll do an order is a wait order that find by I.D. and will pass 36 00:02:39,070 --> 00:02:43,920 in the order I.D. then if there is not an order. 37 00:02:43,920 --> 00:02:45,120 So if we did not find one. 38 00:02:45,240 --> 00:02:49,250 Let's throw a new not found error. 39 00:02:49,350 --> 00:02:56,880 In addition if the order does exist but the owner of the order so the user I.D. is not equal to recurrent 40 00:02:56,880 --> 00:03:01,560 user exclamation dot I.D. then we should also forbid this. 41 00:03:01,560 --> 00:03:11,410 So we'll throw a new not authorized error let's add imports for these two errors up at the top we'll 42 00:03:11,410 --> 00:03:20,330 find our common module and add in not found error not authorized error. 43 00:03:20,450 --> 00:03:24,620 Then after that we'll go ahead and make our update to the order so we're all we really have to here 44 00:03:24,620 --> 00:03:25,610 is update the status. 45 00:03:25,610 --> 00:03:26,810 That's pretty much it. 46 00:03:26,810 --> 00:03:36,500 So order not status is going to be order status dot canceled will then save it and that's pretty much 47 00:03:36,500 --> 00:03:36,680 it. 48 00:03:38,250 --> 00:03:41,220 At the very bottom well then send back the updated order. 49 00:03:41,220 --> 00:03:48,630 Like so if this was really a delete handler we should technically also send back specifically a status 50 00:03:48,630 --> 00:03:51,570 of two a four which indicates that a record was deleted. 51 00:03:51,600 --> 00:03:55,440 So if you really want to follow these delete semantics we should have the two a four there. 52 00:03:55,440 --> 00:04:00,060 Again we're not really deleting a record per say we're canceling it but we're kind of pretending as 53 00:04:00,060 --> 00:04:02,250 though that's kind of a delete. 54 00:04:02,250 --> 00:04:06,900 Again I think maybe I kind of made a mistake here now that I'm going through this stuff really maybe 55 00:04:06,900 --> 00:04:11,690 it would have been made a lot more sense to make this into a patch but this is there. 56 00:04:11,700 --> 00:04:15,850 Let me just be clear this is a really small nit pick on my side super not critical. 57 00:04:15,900 --> 00:04:17,670 You know I'm just going to stop complaining about it. 58 00:04:17,670 --> 00:04:22,900 We'll stick with a delete method and a status of two a four and to say it's good enough all right. 59 00:04:22,910 --> 00:04:25,790 So let's say this we do naturally need to make sure that it all works. 60 00:04:25,790 --> 00:04:29,750 So now that we've got the implementation put together quick pause come back in the next video write 61 00:04:29,780 --> 00:04:31,070 out a quick test or two.