1
00:00:00,550 --> 00:00:02,119
- [Narrator] Hi and
welcome back to the course.

2
00:00:02,119 --> 00:00:06,090
In this video we're going
to look at deleting an item.

3
00:00:06,090 --> 00:00:08,198
We've gone through the most
complex part of this app,

4
00:00:08,198 --> 00:00:09,741
which was authentication.

5
00:00:09,741 --> 00:00:11,821
And now we're just gonna be
adding a few more end points

6
00:00:11,821 --> 00:00:13,583
to make sure that
everything sticks together

7
00:00:13,583 --> 00:00:15,500
and then we'll be done.

8
00:00:16,841 --> 00:00:21,791
To add deletion of items,
you know that the http verb

9
00:00:21,791 --> 00:00:25,810
is delete, so you can
probably guess what the method

10
00:00:25,810 --> 00:00:27,060
is going to be.

11
00:00:31,267 --> 00:00:33,975
And then because this is
inside the item resource,

12
00:00:33,975 --> 00:00:36,345
this has to have a name as well,

13
00:00:36,345 --> 00:00:39,409
and that is what we're
going to be deleting.

14
00:00:39,409 --> 00:00:40,242
Okay.

15
00:00:41,615 --> 00:00:43,897
Now, what's going to
happen now is a bit of

16
00:00:43,897 --> 00:00:46,897
an interesting Python sort of event,

17
00:00:49,201 --> 00:00:51,338
that happens very often
and it's a very bad

18
00:00:51,338 --> 00:00:52,948
problem to debug.

19
00:00:52,948 --> 00:00:54,695
And it's the following.

20
00:00:54,695 --> 00:00:58,278
We're gonna say that
items, our items list,

21
00:00:59,835 --> 00:01:03,835
is a list of the result
of filtering on a lambda

22
00:01:05,992 --> 00:01:10,159
where the name is not equal to
the name that was passed in.

23
00:01:13,426 --> 00:01:14,259
Okay.

24
00:01:15,511 --> 00:01:17,777
So, before we were doing this filtering,

25
00:01:17,777 --> 00:01:20,267
just like that, but we were
looking for one specific

26
00:01:20,267 --> 00:01:23,015
element, now we're looking
for all the elements

27
00:01:23,015 --> 00:01:25,096
except one, and that one

28
00:01:25,096 --> 00:01:26,871
is the one that's going to get deleted.

29
00:01:26,871 --> 00:01:28,358
So what we're gonna do essentially is

30
00:01:28,358 --> 00:01:32,108
overwrite our items list
with a new list that

31
00:01:33,039 --> 00:01:37,206
is all of the elements except
the one that was deleted.

32
00:01:40,408 --> 00:01:43,487
Do you spot any errors here?

33
00:01:43,487 --> 00:01:47,237
Anything that you think
might cause an error?

34
00:01:50,106 --> 00:01:52,648
And to be fair, at first I didn't either.

35
00:01:52,648 --> 00:01:54,815
But Python is quite funny.

36
00:01:57,497 --> 00:02:00,334
Items is being used here,
so what's gonna happen

37
00:02:00,334 --> 00:02:04,002
is that we're gonna filter
and we're gonna use the

38
00:02:04,002 --> 00:02:06,085
items variable to filter.

39
00:02:07,966 --> 00:02:10,985
However, Python's gonna
think that the items variable

40
00:02:10,985 --> 00:02:13,110
is only defined here,

41
00:02:13,110 --> 00:02:15,653
so it cannot possibly use the variable

42
00:02:15,653 --> 00:02:17,771
before it has been defined.

43
00:02:17,771 --> 00:02:20,418
Python's gonna think that we're
using the variable's value

44
00:02:20,418 --> 00:02:22,997
to define the variable's value.

45
00:02:22,997 --> 00:02:24,671
And that's kinda like defining a word

46
00:02:24,671 --> 00:02:27,599
using the same word that
you're trying to define.

47
00:02:27,599 --> 00:02:29,182
You cannot do that.

48
00:02:30,038 --> 00:02:32,794
And that happens because we've
called this variable here

49
00:02:32,794 --> 00:02:34,961
items, so it's gonna think

50
00:02:35,917 --> 00:02:38,450
that we're creating a new variable items,

51
00:02:38,450 --> 00:02:41,647
that is only going to
exist inside this method.

52
00:02:41,647 --> 00:02:44,480
This is gonna be a local variable.

53
00:02:45,377 --> 00:02:46,643
That's normally what happens.

54
00:02:46,643 --> 00:02:47,983
When you create a variable, that variable

55
00:02:47,983 --> 00:02:51,731
is a local variable that
only exists in the method

56
00:02:51,731 --> 00:02:54,710
or the block that you created.

57
00:02:54,710 --> 00:02:56,655
However we don't wanna use that variable,

58
00:02:56,655 --> 00:03:00,309
we wanna use the global items variable.

59
00:03:00,309 --> 00:03:02,085
So then we just have to do that.

60
00:03:02,085 --> 00:03:04,347
See global items, and what that does,

61
00:03:04,347 --> 00:03:07,007
is it says, the items
variable in this block

62
00:03:07,007 --> 00:03:09,340
is the outer items variable.

63
00:03:10,793 --> 00:03:11,985
Okay?

64
00:03:11,985 --> 00:03:13,946
Saved you a lot of pain there.

65
00:03:13,946 --> 00:03:18,113
And then what we're gonna do
is say return item deleted.

66
00:03:21,967 --> 00:03:23,392
And that's it really.

67
00:03:23,392 --> 00:03:25,339
That is going to delete the item,

68
00:03:25,339 --> 00:03:27,685
because it's going to
change the data base into

69
00:03:27,685 --> 00:03:29,935
not have that item anymore.

70
00:03:31,285 --> 00:03:34,903
Now, we could go into
Postman and give it a go.

71
00:03:34,903 --> 00:03:37,208
And remember that when we change the app,

72
00:03:37,208 --> 00:03:41,519
and the app restarts,
or we hand restart it,

73
00:03:41,519 --> 00:03:43,147
but it restarts on its own anyway.

74
00:03:43,147 --> 00:03:44,965
So none of the items are gonna be there.

75
00:03:44,965 --> 00:03:46,466
We can get the items, and you can see

76
00:03:46,466 --> 00:03:48,718
it's empty, so what we're gonna do

77
00:03:48,718 --> 00:03:52,247
is we're gonna post a
new item, a new piano,

78
00:03:52,247 --> 00:03:55,441
and that creates the
piano and then we can get

79
00:03:55,441 --> 00:03:57,474
our list of items and there you can see

80
00:03:57,474 --> 00:03:59,734
the piano is in there and the
last thing we're going to do

81
00:03:59,734 --> 00:04:03,901
to verify that everything works
is we're going to delete it.

82
00:04:05,352 --> 00:04:06,218
Okay?

83
00:04:06,218 --> 00:04:08,031
So what do you think is gonna
happen when we press send?

84
00:04:08,031 --> 00:04:09,864
What's gonna come out?

85
00:04:12,287 --> 00:04:15,697
And you're correct, the
message item deleted comes out.

86
00:04:15,697 --> 00:04:17,805
We have no proof when it has been deleted,

87
00:04:17,805 --> 00:04:21,025
but we have to trust
that it has been deleted

88
00:04:21,025 --> 00:04:25,418
but we can still call the
get items end point to verify

89
00:04:25,418 --> 00:04:28,517
and you can see that it's no longer there.

90
00:04:28,517 --> 00:04:29,977
So that's everything for this video.

91
00:04:29,977 --> 00:04:32,058
We've managed to delete an item,

92
00:04:32,058 --> 00:04:33,556
and I'll see you on the very next one.

