1
00:00:00,723 --> 00:00:02,629
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,629 --> 00:00:04,971
In this video, we're going
to do the last method

3
00:00:04,971 --> 00:00:07,251
of our item resource,

4
00:00:07,251 --> 00:00:10,098
and then in the next
one, we do the item list.

5
00:00:10,098 --> 00:00:13,840
The last method is put and
that has to insert an item

6
00:00:13,840 --> 00:00:16,624
or update an item.

7
00:00:16,624 --> 00:00:18,057
So the first thing we have to do is,

8
00:00:18,057 --> 00:00:20,238
just as we've done here,

9
00:00:20,238 --> 00:00:24,580
find if the item already
exists or not, okay?

10
00:00:24,580 --> 00:00:26,108
So let's do that.

11
00:00:26,108 --> 00:00:30,258
Let's say that the item
is not the next item

12
00:00:30,258 --> 00:00:31,452
available in this filter,

13
00:00:31,452 --> 00:00:32,963
which is what we used to be.

14
00:00:32,963 --> 00:00:35,463
Now this is self.find_by_name,

15
00:00:36,501 --> 00:00:38,834
as we have used in the past.

16
00:00:40,266 --> 00:00:44,433
I'm also going to move the
item outside this item here

17
00:00:45,337 --> 00:00:46,578
and put it over there

18
00:00:46,578 --> 00:00:49,249
and you'll see why in just a moment.

19
00:00:49,249 --> 00:00:52,665
This item is a dictionary representing

20
00:00:52,665 --> 00:00:54,466
sort of the updated item,

21
00:00:54,466 --> 00:00:57,096
the item with a new price.

22
00:00:57,096 --> 00:00:59,435
Obviously it cannot have the same name,

23
00:00:59,435 --> 00:01:03,446
so I'm gonna call it updated_item instead.

24
00:01:03,446 --> 00:01:04,279
If it had the same name,

25
00:01:04,279 --> 00:01:05,917
it would just override the previous one

26
00:01:05,917 --> 00:01:07,315
and then that wouldn't make a lot of sense

27
00:01:07,315 --> 00:01:10,019
when it comes to checking if it is none.

28
00:01:10,019 --> 00:01:13,116
It wouldn't be none if it's a dictionary.

29
00:01:13,116 --> 00:01:15,081
Okay so if the item is none,

30
00:01:15,081 --> 00:01:18,201
meaning it wasn't found on the database,

31
00:01:18,201 --> 00:01:20,074
then what we're going to do,

32
00:01:20,074 --> 00:01:24,241
is we're going to say
self.insert the item.

33
00:01:28,720 --> 00:01:32,153
Sorry, the updated_item,
not the item itself.

34
00:01:32,153 --> 00:01:33,918
And if the item was not none,

35
00:01:33,918 --> 00:01:35,972
it means that it exists already.

36
00:01:35,972 --> 00:01:38,889
What we're gonna do is self.update,

37
00:01:40,347 --> 00:01:41,988
the updated_item.

38
00:01:41,988 --> 00:01:45,125
Okay, of course this
self.update doesn't exist,

39
00:01:45,125 --> 00:01:47,785
so we're also going to
have to create that one,

40
00:01:47,785 --> 00:01:50,613
which is another classmethod,

41
00:01:50,613 --> 00:01:54,113
called update(cls, item).

42
00:01:55,181 --> 00:01:57,910
Let's have a wee look
at how that may be done.

43
00:01:57,910 --> 00:02:00,617
Updating in SEQUEL is
going to be very similar

44
00:02:00,617 --> 00:02:02,719
to our other methods,

45
00:02:02,719 --> 00:02:05,973
so I'm just gonna copy this
there so I don't bore you.

46
00:02:05,973 --> 00:02:07,117
And all we're doing, once again,

47
00:02:07,117 --> 00:02:09,856
is setting up a connection,
creating a cursor,

48
00:02:09,856 --> 00:02:12,173
creating a query, and
then running the query

49
00:02:12,173 --> 00:02:13,349
using the cursor,

50
00:02:13,349 --> 00:02:14,887
and finally committing the connection

51
00:02:14,887 --> 00:02:18,912
to save things to disc and
closing that connection.

52
00:02:18,912 --> 00:02:20,445
Okay?

53
00:02:20,445 --> 00:02:24,505
Now the query is no longer
going to be delete from items.

54
00:02:24,505 --> 00:02:27,088
It is going to be update items.

55
00:02:28,400 --> 00:02:31,516
Now we're gonna update the items table,

56
00:02:31,516 --> 00:02:33,378
but we don't want to
update the entire table.

57
00:02:33,378 --> 00:02:35,323
We have to set a column.

58
00:02:35,323 --> 00:02:38,193
We're going to set the
price column to be equal to

59
00:02:38,193 --> 00:02:40,561
one of these parameters.

60
00:02:40,561 --> 00:02:44,318
And remember, this is going
to update the items table.

61
00:02:44,318 --> 00:02:47,811
So it's gonna set every price in the table

62
00:02:47,811 --> 00:02:50,383
to be this parameter,

63
00:02:50,383 --> 00:02:54,550
unless we have a name equals something.

64
00:02:55,491 --> 00:02:58,061
So now it's only gonna update the one

65
00:02:58,061 --> 00:03:02,247
where the name is equal
to the question mark.

66
00:03:02,247 --> 00:03:04,061
Then we're gonna execute the query,

67
00:03:04,061 --> 00:03:07,196
but it's gonna have two
parameters, the price and the name.

68
00:03:07,196 --> 00:03:09,497
And they have to be in
order, remember that.

69
00:03:09,497 --> 00:03:11,908
So item price goes first.

70
00:03:11,908 --> 00:03:14,008
And item name goes after.

71
00:03:14,008 --> 00:03:17,496
Remember this method is
receiving an item parameter,

72
00:03:17,496 --> 00:03:20,829
which is a dictionary of name and price.

73
00:03:23,914 --> 00:03:24,747
Okay.

74
00:03:25,641 --> 00:03:30,382
So now we've got the insertion
or the updates of the item.

75
00:03:30,382 --> 00:03:32,640
And all that's left is making sure

76
00:03:32,640 --> 00:03:36,064
that we are dealing with any
potential unexpected issues

77
00:03:36,064 --> 00:03:38,009
so we're gonna surround these

78
00:03:38,009 --> 00:03:39,926
with try except blocks.

79
00:03:41,722 --> 00:03:44,928
Now we're gonna return a message saying

80
00:03:44,928 --> 00:03:49,095
an error occurred inserting the item.

81
00:03:51,337 --> 00:03:54,420
And we're gonna do the same for this.

82
00:03:56,793 --> 00:04:01,045
But instead of inserting,
it's gonna be update.

83
00:04:01,045 --> 00:04:03,146
Remember to return the error 500.

84
00:04:03,146 --> 00:04:04,795
That's very important.

85
00:04:04,795 --> 00:04:06,815
And at the end, don't return item

86
00:04:06,815 --> 00:04:09,139
because that's the item
we found originally.

87
00:04:09,139 --> 00:04:13,222
What we want to return is
the updated_item, okay?

88
00:04:15,208 --> 00:04:17,347
And that's it, really.

89
00:04:17,347 --> 00:04:20,143
The put method, as you
can see, is fairly simple.

90
00:04:20,143 --> 00:04:23,126
All we are doing is finding
the item in the database

91
00:04:23,126 --> 00:04:26,190
and, if it exists, we're updating it,

92
00:04:26,190 --> 00:04:28,280
and if it doesn't, we're inserting it.

93
00:04:28,280 --> 00:04:31,828
And because we've extracted
the code to do these things

94
00:04:31,828 --> 00:04:33,636
outside of any specific method,

95
00:04:33,636 --> 00:04:35,944
it means it can be re-used.

96
00:04:35,944 --> 00:04:39,063
And that's a really useful thing.

97
00:04:39,063 --> 00:04:40,784
That's everything in this video.

98
00:04:40,784 --> 00:04:43,319
We now can update our items.

99
00:04:43,319 --> 00:04:45,336
You can go ahead in
Postman and try it out,

100
00:04:45,336 --> 00:04:47,013
but believe me, it does work.

101
00:04:47,013 --> 00:04:48,755
It does do what you'd expect.

102
00:04:48,755 --> 00:04:51,358
In the next video, we're
going to go over the item list

103
00:04:51,358 --> 00:04:54,290
and how we can retrieve all
our items from the database

104
00:04:54,290 --> 00:04:56,218
and there we're gonna
perform some final checks

105
00:04:56,218 --> 00:04:57,958
in Postman as well.

106
00:04:57,958 --> 00:04:59,623
I'll see you in the next video.

