1
1

00:00:01,360  -->  00:00:02,570
<v Jose>Hi and welcome back.</v>
2

2

00:00:02,570  -->  00:00:04,330
In this video we're going to talk about
3

3

00:00:04,330  -->  00:00:07,080
how to display only the alerts that are
4

4

00:00:07,080  -->  00:00:09,500
for the currently logged in user.
5

5

00:00:09,500  -->  00:00:12,623
So that not all users will see all alerts.
6

6

00:00:13,610  -->  00:00:16,630
We're gonna start by going over to the alerts view,
7

7

00:00:16,630  -->  00:00:20,350
and we're gonna make a small change in the index.
8

8

00:00:20,350  -->  00:00:23,480
Instead of retrieving all the alerts in our system,
9

9

00:00:23,480  -->  00:00:25,920
we're only going to retrieve those alerts
10

10

00:00:25,920  -->  00:00:30,183
that have a property that matches session email.
11

11

00:00:31,160  -->  00:00:33,540
So if you remember, whenever the user logs in
12

12

00:00:33,540  -->  00:00:36,733
or registers, we set session email.
13

13

00:00:37,610  -->  00:00:40,110
And we know that this session is related to a
14

14

00:00:40,110  -->  00:00:44,240
particular user because every browser will send us a cookie,
15

15

00:00:44,240  -->  00:00:47,220
a piece of data that tells us which session
16

16

00:00:47,220  -->  00:00:49,830
that browser is related to.
17

17

00:00:49,830  -->  00:00:52,600
So when the user logs in with their browser
18

18

00:00:52,600  -->  00:00:55,070
that cookie will then be related to one
19

19

00:00:55,070  -->  00:00:58,760
specific session which will contain their email.
20

20

00:00:58,760  -->  00:01:02,490
If many users sign up, there'll be many cookies,
21

21

00:01:02,490  -->  00:01:04,020
and when we receive those cookies,
22

22

00:01:04,020  -->  00:01:07,380
we'll be able to determine which session is for who.
23

23

00:01:07,380  -->  00:01:09,980
So that's sort of how the log in works just as a recap.
24

24

00:01:09,980  -->  00:01:11,430
And here what we're gonna do is we're gonna
25

25

00:01:11,430  -->  00:01:14,513
retrieve alerts that match that session's email.
26

26

00:01:15,420  -->  00:01:19,880
So we're say find many by and we're gonna say
27

27

00:01:19,880  -->  00:01:21,237
the attribute is user email,
28

28

00:01:21,237  -->  00:01:23,840
and the value is session email.
29

29

00:01:23,840  -->  00:01:26,440
So what this is gonna do, gonna go to the database
30

30

00:01:26,440  -->  00:01:30,180
and find all the alerts that have a property
31

31

00:01:30,180  -->  00:01:34,830
user_email and that value is session email.
32

32

00:01:34,830  -->  00:01:38,250
So if somebody logs in with Bob@example.com,
33

33

00:01:38,250  -->  00:01:39,560
then we're only gonna show alerts
34

34

00:01:39,560  -->  00:01:41,370
that have a property user email
35

35

00:01:41,370  -->  00:01:43,963
with value Bob@example.com.
36

36

00:01:45,050  -->  00:01:46,750
This does mean two things.
37

37

00:01:46,750  -->  00:01:49,210
The first thing it means is we need to add
38

38

00:01:49,210  -->  00:01:53,068
a property user_email to our alert.
39

39

00:01:53,068  -->  00:01:54,980
And the second thing it means is the user
40

40

00:01:54,980  -->  00:01:58,260
must've logged in or registered in order
41

41

00:01:58,260  -->  00:02:00,863
for session email to have a value.
42

42

00:02:02,130  -->  00:02:05,310
Let's go over to our alert model
43

43

00:02:05,310  -->  00:02:07,530
and add a couple of those things in.
44

44

00:02:07,530  -->  00:02:10,640
In order to add a new property to our alert,
45

45

00:02:10,640  -->  00:02:14,060
all we have to do is say user_email is a string.
46

46

00:02:14,060  -->  00:02:16,030
And we don't have to give it any extra values.
47

47

00:02:16,030  -->  00:02:19,723
That's automatically gonna go into the init method.
48

48

00:02:20,610  -->  00:02:22,830
Then we're gonna do self.user is
49

49

00:02:22,830  -->  00:02:24,380
user.find_by_email(user_email).
50

50

00:02:29,818  -->  00:02:32,730
And of course we do need self,
51

51

00:02:32,730  -->  00:02:35,650
and we do need to import user
52

52

00:02:35,650  -->  00:02:39,303
from models user import user.
53

53

00:02:41,260  -->  00:02:42,620
And we also wanna be saving the
54

54

00:02:42,620  -->  00:02:44,793
user email in the json method.
55

55

00:02:49,120  -->  00:02:51,520
Alright, so that's everything we need from
56

56

00:02:51,520  -->  00:02:55,270
our alert model in order to add this new field.
57

57

00:02:55,270  -->  00:02:56,660
Now we do need to remember to
58

58

00:02:56,660  -->  00:02:59,030
pass in a value to the constructor.
59

59

00:02:59,030  -->  00:03:02,010
So going back to our view here in the
60

60

00:03:02,010  -->  00:03:04,320
alert constructor when we create a new alert,
61

61

00:03:04,320  -->  00:03:08,140
we do have to pass in session email.
62

62

00:03:08,140  -->  00:03:11,410
Note that this automatically means that
63

63

00:03:11,410  -->  00:03:13,370
in order to create a new alert,
64

64

00:03:13,370  -->  00:03:15,750
a user has to have logged in previously,
65

65

00:03:15,750  -->  00:03:17,270
otherwise this won't have a value
66

66

00:03:17,270  -->  00:03:18,683
when they make this request.
67

67

00:03:19,760  -->  00:03:21,300
Later on in the course, we're gonna talk about
68

68

00:03:21,300  -->  00:03:24,140
how we can make sure that users are logged in
69

69

00:03:24,140  -->  00:03:27,140
and how to request that they log in if they haven't already.
70

70

00:03:29,110  -->  00:03:31,030
In the edit alert, we're not going to use
71

71

00:03:31,030  -->  00:03:33,200
session email at all because all that's changing
72

72

00:03:33,200  -->  00:03:34,193
is the price limit.
73

73

00:03:35,080  -->  00:03:37,340
Again in the later video we will require
74

74

00:03:37,340  -->  00:03:40,220
users to log in before they can edit an alert
75

75

00:03:40,220  -->  00:03:42,020
just in case they try editing an alert
76

76

00:03:42,020  -->  00:03:44,330
that's not theirs, but at the moment
77

77

00:03:44,330  -->  00:03:45,680
we're not going to do that.
78

78

00:03:47,440  -->  00:03:51,390
In the delete alert we are going to go ahead and
79

79

00:03:51,390  -->  00:03:54,920
make sure that the alert that is to be deleted
80

80

00:03:54,920  -->  00:03:58,180
is the user's alert and not somebody else's.
81

81

00:03:58,180  -->  00:03:59,070
This is something that you could do
82

82

00:03:59,070  -->  00:04:00,780
in the edit alert as well if you wanted.
83

83

00:04:00,780  -->  00:04:01,960
So I'll just show it to you here,
84

84

00:04:01,960  -->  00:04:04,500
and you can choose what you want to do.
85

85

00:04:04,500  -->  00:04:07,150
The first thing we're gonna do is put the alert
86

86

00:04:07,150  -->  00:04:08,860
into its own variable
87

87

00:04:10,190  -->  00:04:15,190
and then if the alert user email matches sessions email
88

88

00:04:17,300  -->  00:04:20,313
then we're going to do alert.remove from mongo.
89

89

00:04:21,570  -->  00:04:25,290
Okay perfect.
90

90

00:04:25,290  -->  00:04:26,600
That's everything we need in order
91

91

00:04:26,600  -->  00:04:30,680
to have the alerts showing up only per user.
92

92

00:04:30,680  -->  00:04:32,920
There are a couple of things you have to do
93

93

00:04:32,920  -->  00:04:34,760
before you run your app.
94

94

00:04:34,760  -->  00:04:36,430
Well, you can do them after you run your app,
95

95

00:04:36,430  -->  00:04:38,690
but I would recommend you do them before
96

96

00:04:38,690  -->  00:04:40,870
which is you have to go and clear
97

97

00:04:40,870  -->  00:04:42,633
everything from your database.
98

98

00:04:43,500  -->  00:04:45,550
Okay, so I've loaded up mungo DB here.
99

99

00:04:45,550  -->  00:04:48,173
Make sure you're using the right database.
100

100

00:04:49,980  -->  00:04:51,610
And then we're just gonna delete everything
101

101

00:04:51,610  -->  00:04:53,050
from the entire database.
102

102

00:04:53,050  -->  00:04:58,050
DB alerts remove, DB items remove, DB stores remove.
103

103

00:05:00,590  -->  00:05:02,500
Actually we're not gonna remove stores.
104

104

00:05:02,500  -->  00:05:04,420
We do wanna keep that just so that
105

105

00:05:04,420  -->  00:05:06,620
we don't have to delete them again.
106

106

00:05:06,620  -->  00:05:09,630
DB.users.remove.
107

107

00:05:09,630  -->  00:05:12,120
Okay, we've removed everything except the stores
108

108

00:05:12,120  -->  00:05:13,950
just because that is what has the technical
109

109

00:05:13,950  -->  00:05:16,270
data that we want to keep.
110

110

00:05:16,270  -->  00:05:20,570
And now we're ready to go ahead and create a new user
111

111

00:05:20,570  -->  00:05:24,240
and then create a new alert and see if all that works.
112

112

00:05:24,240  -->  00:05:26,303
Let's go ahead and run our app.
113

113

00:05:27,810  -->  00:05:30,140
I've brought up the app here on the right,
114

114

00:05:30,140  -->  00:05:31,623
and we're gonna sign up.
115

115

00:05:34,150  -->  00:05:35,320
There we have our user.
116

116

00:05:35,320  -->  00:05:37,480
Everything worked in there, and then
117

117

00:05:37,480  -->  00:05:40,190
we're gonna go over to the alerts endpoint.
118

118

00:05:40,190  -->  00:05:43,320
And you can see that it didn't fail.
119

119

00:05:43,320  -->  00:05:45,590
We're gonna create a new alert.
120

120

00:05:45,590  -->  00:05:47,100
And I found the item URL there,
121

121

00:05:47,100  -->  00:05:49,600
and then we've got our prize limit.
122

122

00:05:49,600  -->  00:05:51,230
So we're gonna create that
123

123

00:05:51,230  -->  00:05:52,700
and that also worked.
124

124

00:05:52,700  -->  00:05:53,810
What does that tell us?
125

125

00:05:53,810  -->  00:05:57,520
It means that we have session email set.
126

126

00:05:57,520  -->  00:06:02,470
So if we go back to alerts, we have our alert here.
127

127

00:06:02,470  -->  00:06:06,033
If we go and sign up as another user,
128

128

00:06:09,250  -->  00:06:11,865
and then we go over to our alerts,
129

129

00:06:11,865  -->  00:06:13,670
there are no alerts showing up.
130

130

00:06:13,670  -->  00:06:15,060
That's exactly what we wanted.
131

131

00:06:15,060  -->  00:06:18,023
Each user now has their own alerts.
132

132

00:06:19,040  -->  00:06:21,020
Do know that as we're navigating through this site
133

133

00:06:21,020  -->  00:06:24,240
we are strongly missing a navigation bar,
134

134

00:06:24,240  -->  00:06:26,140
something that we can use to move
135

135

00:06:26,140  -->  00:06:27,490
between our different pages.
136

136

00:06:27,490  -->  00:06:28,920
We're gonna be adding that later on,
137

137

00:06:28,920  -->  00:06:31,430
so don't worry if that's not there yet.
138

138

00:06:31,430  -->  00:06:33,260
Bootstrap actually has some very nice ways
139

139

00:06:33,260  -->  00:06:35,670
of adding navigation bars.
140

140

00:06:35,670  -->  00:06:37,040
That's everything for this video though.
141

141

00:06:37,040  -->  00:06:40,040
We have now made it so that the alerts
142

142

00:06:40,040  -->  00:06:42,490
only show up for the user that created them
143

143

00:06:42,490  -->  00:06:43,760
and not for anyone else.
144

144

00:06:43,760  -->  00:06:46,130
And we've also looked at how we can make sure
145

145

00:06:46,130  -->  00:06:48,520
that when we're interacting with a single alert
146

146

00:06:48,520  -->  00:06:50,500
that it is the right user
147

147

00:06:50,500  -->  00:06:52,100
that's making the interaction.
148

148

00:06:52,100  -->  00:06:53,830
And again, you can do this in the edit alert
149

149

00:06:53,830  -->  00:06:55,410
method as well if you wanted.
150

150

00:06:55,410  -->  00:06:57,680
But I just felt that the delete one is the
151

151

00:06:57,680  -->  00:06:59,780
one that's really important because
152

152

00:06:59,780  -->  00:07:01,820
it's the destructive action.
153

153

00:07:01,820  -->  00:07:03,870
This one will also need the user
154

154

00:07:03,870  -->  00:07:06,110
to log in before it runs.
155

155

00:07:06,110  -->  00:07:07,460
Thanks for joining me in this video!
156

156

00:07:07,460  -->  00:07:08,610
Hope you learned something
157

157

00:07:08,610  -->  00:07:10,260
and I'll see you on the next one.
