1
1

00:00:00,810  -->  00:00:02,350
<v Jose>Hi guys, welcome back.</v>
2

2

00:00:02,350  -->  00:00:06,040
In this video we're going to be storing the last price
3

3

00:00:06,040  -->  00:00:09,390
found for an item into a database.
4

4

00:00:09,390  -->  00:00:10,223
The reason for that,
5

5

00:00:10,223  -->  00:00:12,890
is that when a user is looking at their alerts,
6

6

00:00:12,890  -->  00:00:15,670
at the moment they see the alert name,
7

7

00:00:15,670  -->  00:00:17,993
and the item ID and the item URL.
8

8

00:00:18,850  -->  00:00:21,230
But, it would be nice if we could tell them,
9

9

00:00:21,230  -->  00:00:24,860
what the last price was for the item,
10

10

00:00:24,860  -->  00:00:26,400
when they're looking at their screen,
11

11

00:00:26,400  -->  00:00:28,740
without having to, you know, click the item,
12

12

00:00:28,740  -->  00:00:30,940
and find what the price is right now themselves.
13

13

00:00:30,940  -->  00:00:32,660
If a programme can do it for them,
14

14

00:00:32,660  -->  00:00:34,370
that's gonna make their life a little bit easier,
15

15

00:00:34,370  -->  00:00:36,500
it's gonna make things a bit faster.
16

16

00:00:36,500  -->  00:00:41,310
So the plan is to be able to show something like last price,
17

17

00:00:41,310  -->  00:00:46,310
and then have this alert dot item dot price in here.
18

18

00:00:47,230  -->  00:00:48,063
Like that.
19

19

00:00:48,063  -->  00:00:52,453
So we'll say last price, maybe £1000 or £100 or whatever.
20

20

00:00:54,150  -->  00:00:54,983
In order to do that,
21

21

00:00:54,983  -->  00:00:57,670
we do need to store the price on the item.
22

22

00:00:57,670  -->  00:01:00,020
So let's go over to our item model.
23

23

00:01:00,020  -->  00:01:01,490
And, notice that at the moment,
24

24

00:01:01,490  -->  00:01:04,000
we are having this post init,
25

25

00:01:04,000  -->  00:01:06,100
that says self dot price is none.
26

26

00:01:06,100  -->  00:01:09,570
And then, when we call the load price method,
27

27

00:01:09,570  -->  00:01:11,900
we populate self dot price.
28

28

00:01:11,900  -->  00:01:15,270
But, we don't save it to the database,
29

29

00:01:15,270  -->  00:01:17,730
which means that, when we create the item again,
30

30

00:01:17,730  -->  00:01:20,570
such as if we reload the page or whatever,
31

31

00:01:20,570  -->  00:01:23,940
it's not gonna have a price until we call load price again.
32

32

00:01:23,940  -->  00:01:26,390
If we store that in the database,
33

33

00:01:26,390  -->  00:01:30,130
that means that, the latest price that we got,
34

34

00:01:30,130  -->  00:01:32,010
will always be saved there,
35

35

00:01:32,010  -->  00:01:34,010
and when we call load price,
36

36

00:01:34,010  -->  00:01:36,170
what we'll have to do is, we will have to
37

37

00:01:36,170  -->  00:01:39,860
re-save that price into the database,
38

38

00:01:39,860  -->  00:01:41,360
so that the database always has
39

39

00:01:41,360  -->  00:01:43,733
the latest price we retrieved.
40

40

00:01:44,720  -->  00:01:46,210
In this video,
41

41

00:01:46,210  -->  00:01:48,874
we're going to say price is going to be a float,
42

42

00:01:48,874  -->  00:01:50,450
which is going to be a field
43

43

00:01:50,450  -->  00:01:53,170
and the default is going to be none.
44

44

00:01:53,170  -->  00:01:56,420
And, that does exactly the same that, this post init does.
45

45

00:01:56,420  -->  00:01:57,313
It gives us,
46

46

00:01:58,197  -->  00:01:59,510
a float value,
47

47

00:01:59,510  -->  00:02:01,360
but the default is none.
48

48

00:02:01,360  -->  00:02:02,430
There is a difference,
49

49

00:02:02,430  -->  00:02:07,150
which is that now it can be passed in to the init method.
50

50

00:02:07,150  -->  00:02:09,030
Which means that we can take it,
51

51

00:02:09,030  -->  00:02:11,703
and save it here into the JSON method.
52

52

00:02:12,860  -->  00:02:16,030
So that when we load an item from the database,
53

53

00:02:16,030  -->  00:02:18,890
it will automatically have the price,
54

54

00:02:18,890  -->  00:02:20,440
that was saved in the database.
55

55

00:02:22,970  -->  00:02:23,803
Now remember,
56

56

00:02:23,803  -->  00:02:26,090
whenever we call load price,
57

57

00:02:26,090  -->  00:02:29,010
that is going to populate self dot price.
58

58

00:02:29,010  -->  00:02:31,330
But, it's not going to save it to the database
59

59

00:02:31,330  -->  00:02:34,900
unless we call self dot save to Mongo,
60

60

00:02:34,900  -->  00:02:36,500
so we do have to still call that.
61

61

00:02:36,500  -->  00:02:38,300
It's not gonna happen automatically.
62

62

00:02:39,400  -->  00:02:41,120
The last thing we have to do is,
63

63

00:02:41,120  -->  00:02:43,460
when we create a new alert
64

64

00:02:44,430  -->  00:02:45,263
in here,
65

65

00:02:45,263  -->  00:02:47,560
before we save the item to MongoDB,
66

66

00:02:47,560  -->  00:02:49,830
we're going to save item dot load price.
67

67

00:02:49,830  -->  00:02:51,670
And what that's gonna do again is,
68

68

00:02:51,670  -->  00:02:55,530
going to go the site, fetch the price of the item,
69

69

00:02:55,530  -->  00:02:58,000
store it in self dot price, and then,
70

70

00:02:58,000  -->  00:02:59,634
we're gonna save it to MongoDB,
71

71

00:02:59,634  -->  00:03:02,950
and then we're gonna take the user back to our site.
72

72

00:03:02,950  -->  00:03:04,170
Why?
73

73

00:03:04,170  -->  00:03:06,820
So that, when they go to the alerts page,
74

74

00:03:06,820  -->  00:03:09,820
this item will already have a price.
75

75

00:03:09,820  -->  00:03:12,060
Then we won't have had to, you know, call load price on it,
76

76

00:03:12,060  -->  00:03:14,700
using the alert updater, which is the thing,
77

77

00:03:14,700  -->  00:03:17,450
that actually does most of the work in our application.
78

78

00:03:18,750  -->  00:03:21,850
So, now when we create a new item we'll load the price,
79

79

00:03:21,850  -->  00:03:24,350
and then, when the user ever goes back to the alerts,
80

80

00:03:24,350  -->  00:03:27,740
they'll see what the price was when they created the item,
81

81

00:03:27,740  -->  00:03:31,200
or the last time, that we ran the alert updater.
82

82

00:03:31,200  -->  00:03:32,033
And by the way,
83

83

00:03:32,033  -->  00:03:34,840
we are planning on running this alert updater periodically,
84

84

00:03:34,840  -->  00:03:38,000
maybe every few hours, maybe everyday.
85

85

00:03:38,000  -->  00:03:40,033
Just depends on the user, probably.
86

86

00:03:42,000  -->  00:03:43,380
That's it for this video though.
87

87

00:03:43,380  -->  00:03:46,790
We have added the price into our items and,
88

88

00:03:46,790  -->  00:03:48,120
notice how easy it was.
89

89

00:03:48,120  -->  00:03:50,490
We just added a property into the data class,
90

90

00:03:50,490  -->  00:03:52,380
and then saved into to the JSON.
91

91

00:03:52,380  -->  00:03:53,329
And that's it.
92

92

00:03:53,329  -->  00:03:55,170
So, thanks for joining me.
93

93

00:03:55,170  -->  00:03:56,720
I'll see you on the next video.
