1
00:00:02,000 --> 00:00:05,000
So now that we're storing a newsletter successfully,

2
00:00:05,000 --> 00:00:08,000
let's also store and fetch our comments.

3
00:00:08,000 --> 00:00:12,000
For this I'll go back to the comments API route,

4
00:00:12,000 --> 00:00:16,000
and now here we also wanna connect with MongoDB,

5
00:00:16,000 --> 00:00:21,000
and then simply insert a new comment or fetch all comments.

6
00:00:23,000 --> 00:00:26,000
So either way, no matter what happens,

7
00:00:26,000 --> 00:00:28,000
we wanna connect to MongoDB.

8
00:00:28,000 --> 00:00:33,000
So before we dive into any if block here,

9
00:00:33,000 --> 00:00:34,000
I wanna set up my connection.

10
00:00:34,000 --> 00:00:36,000
Hence, we need to import

11
00:00:38,000 --> 00:00:39,000
MongoClient

12
00:00:40,000 --> 00:00:43,000
from mongodb here,

13
00:00:43,000 --> 00:00:45,000
and then repeat the code from before.

14
00:00:45,000 --> 00:00:47,000
And of course we definitely could also look

15
00:00:47,000 --> 00:00:49,000
into creating some helper functions,

16
00:00:49,000 --> 00:00:52,000
but I will just quickly repeat that.

17
00:00:52,000 --> 00:00:56,000
MongoClient connect, and then grab my connection string

18
00:00:56,000 --> 00:01:00,000
from the newsletter js file.

19
00:01:00,000 --> 00:01:01,000
Paste it in here.

20
00:01:02,000 --> 00:01:04,000
And again I'll use async await.

21
00:01:04,000 --> 00:01:08,000
So add async in front of the handler function,

22
00:01:08,000 --> 00:01:11,000
and await disconnection to get access to the client.

23
00:01:12,000 --> 00:01:17,000
Now already after all these if checks, I will go down there

24
00:01:17,000 --> 00:01:20,000
and already call client close to make sure

25
00:01:20,000 --> 00:01:21,000
that we don't forget this.

26
00:01:21,000 --> 00:01:25,000
That we close the connection once we're done with our logic

27
00:01:25,000 --> 00:01:28,000
no matter what our logic was.

28
00:01:29,000 --> 00:01:30,000
So now that's that.

29
00:01:30,000 --> 00:01:33,000
Now inside of request method POST,

30
00:01:33,000 --> 00:01:37,000
we wanna store our comment here in our database.

31
00:01:37,000 --> 00:01:41,000
And for this I'll create my new comment,

32
00:01:41,000 --> 00:01:44,000
but then thereafter, I will use the client

33
00:01:44,000 --> 00:01:47,000
and the db method to get access to the database

34
00:01:47,000 --> 00:01:49,000
to which we connected.

35
00:01:49,000 --> 00:01:50,000
And for that actually,

36
00:01:50,000 --> 00:01:52,000
I wanna adjust the connection string here,

37
00:01:52,000 --> 00:01:55,000
and not connect to a newsletter database,

38
00:01:55,000 --> 00:01:58,000
but instead to my comments database.

39
00:01:59,000 --> 00:02:04,000
Or, overall, we connect to our events database

40
00:02:04,000 --> 00:02:06,000
for the events application.

41
00:02:06,000 --> 00:02:09,000
And then we have our emails collection

42
00:02:09,000 --> 00:02:11,000
and we have our comments collection.

43
00:02:11,000 --> 00:02:14,000
And I think that makes more sense.

44
00:02:14,000 --> 00:02:16,000
So I'll connect to the events database

45
00:02:16,000 --> 00:02:19,000
in the newsletter js file as well,

46
00:02:19,000 --> 00:02:22,000
and connect to the same database here

47
00:02:22,000 --> 00:02:25,000
in the comments API route, events.

48
00:02:26,000 --> 00:02:29,000
So then it's this database which will work,

49
00:02:29,000 --> 00:02:33,000
and on that database, I then use the emails collection

50
00:02:33,000 --> 00:02:35,000
in the newsletter js file,

51
00:02:35,000 --> 00:02:39,000
or maybe let's now name the collection newsletter.

52
00:02:39,000 --> 00:02:40,000
But that's of course up to you.

53
00:02:41,000 --> 00:02:44,000
And in the comments event id file here,

54
00:02:44,000 --> 00:02:48,000
I'll connect or I'll work with a collection named comments.

55
00:02:50,000 --> 00:02:54,000
And here I now wanna insert one new comment.

56
00:02:54,000 --> 00:02:55,000
Now it is this comment,

57
00:02:55,000 --> 00:02:58,000
but I wanna tweak that comment object.

58
00:02:58,000 --> 00:03:01,000
I don't wanna create my dummy id anymore,

59
00:03:01,000 --> 00:03:04,000
because MongoDB will create a unique id for me.

60
00:03:04,000 --> 00:03:06,000
So I don't need to assign my own id.

61
00:03:07,000 --> 00:03:09,000
But instead I wanna do something

62
00:03:09,000 --> 00:03:11,000
which I haven't done up to this point.

63
00:03:11,000 --> 00:03:14,000
I want to use this event id which we're getting,

64
00:03:14,000 --> 00:03:15,000
which is encoded in the URL.

65
00:03:16,000 --> 00:03:17,000
And I wanna make it clear

66
00:03:17,000 --> 00:03:21,000
to which event my comment is related.

67
00:03:21,000 --> 00:03:22,000
Because in reality,

68
00:03:22,000 --> 00:03:27,000
we would probably manage our events with MongoDB as well.

69
00:03:27,000 --> 00:03:28,000
We're not doing that here,

70
00:03:28,000 --> 00:03:31,000
but in reality we would probably manage them

71
00:03:31,000 --> 00:03:33,000
in the database as well.

72
00:03:33,000 --> 00:03:36,000
So then, storing the id of the related document

73
00:03:36,000 --> 00:03:39,000
in some other collection in this document here

74
00:03:39,000 --> 00:03:41,000
would make a lot of sense.

75
00:03:41,000 --> 00:03:42,000
And that's definitely what I'll do.

76
00:03:42,000 --> 00:03:46,000
I'll store eventId as a extra property

77
00:03:46,000 --> 00:03:50,000
in the new comment to have this reference to the event

78
00:03:50,000 --> 00:03:51,000
to which this comment belongs.

79
00:03:53,000 --> 00:03:56,000
And then it's this new comment which I wanna insert.

80
00:03:56,000 --> 00:04:01,000
So here I'll insert one, and I'll insert this new comment.

81
00:04:01,000 --> 00:04:03,000
And of course await this operation.

82
00:04:04,000 --> 00:04:07,000
Now we do get back some response here by the way.

83
00:04:07,000 --> 00:04:11,000
Some result of that insert one operation,

84
00:04:11,000 --> 00:04:15,000
and I will console log that result for now.

85
00:04:15,000 --> 00:04:18,000
In the effort that's now my code here in the post case

86
00:04:18,000 --> 00:04:20,000
so that we add a new comment.

87
00:04:20,000 --> 00:04:23,000
Before we work on the get case here, let's save that code

88
00:04:23,000 --> 00:04:27,000
and let's see whether it works and makes sense.

89
00:04:27,000 --> 00:04:31,000
For this I'll dive into an individual event,

90
00:04:31,000 --> 00:04:33,000
show my comments, and add a new comment

91
00:04:34,000 --> 00:04:36,000
with some dummy data.

92
00:04:36,000 --> 00:04:39,000
This is a test with MongoDB.

93
00:04:40,000 --> 00:04:42,000
Click Submit.

94
00:04:42,000 --> 00:04:44,000
We still get no feedback here.

95
00:04:44,000 --> 00:04:45,000
We could add it,

96
00:04:45,000 --> 00:04:48,000
but we haven't done anything in that direction.

97
00:04:48,000 --> 00:04:50,000
But if I go the back end, that looks good.

98
00:04:50,000 --> 00:04:52,000
I got a output here.

99
00:04:52,000 --> 00:04:55,000
That's this result which I'm logging.

100
00:04:55,000 --> 00:04:56,000
Whoops.

101
00:04:56,000 --> 00:04:58,000
This result which I'm logging, that's what we see here.

102
00:04:58,000 --> 00:05:02,000
And we see that it's quite a long result.

103
00:05:02,000 --> 00:05:07,000
Quite a detailed output because result in case of insert one

104
00:05:07,000 --> 00:05:10,000
is actually containing full details

105
00:05:10,000 --> 00:05:14,000
about the operation that was performed,

106
00:05:14,000 --> 00:05:18,000
but then also the connection in general and much much more.

107
00:05:20,000 --> 00:05:23,000
Now what we can see if we go to the cluster

108
00:05:23,000 --> 00:05:28,000
is that here if we reload this page to refresh the data,

109
00:05:28,000 --> 00:05:32,000
we got this events database now with a comments collection,

110
00:05:32,000 --> 00:05:36,000
and in there we indeed see that comment which I just added.

111
00:05:36,000 --> 00:05:40,000
So it was stored successfully in the database.

112
00:05:40,000 --> 00:05:43,000
Now what's also interesting on the result

113
00:05:43,000 --> 00:05:47,000
is that on the result we also get back that unique id

114
00:05:47,000 --> 00:05:50,000
which was generated automatically.

115
00:05:50,000 --> 00:05:53,000
That's interesting because that means that

116
00:05:53,000 --> 00:05:56,000
on the new comment object, which we stored in the database,

117
00:05:56,000 --> 00:05:59,000
we can now add a id field

118
00:05:59,000 --> 00:06:02,000
and set this equal to result.insertedId.

119
00:06:04,000 --> 00:06:06,000
So that the new comment which we sent back

120
00:06:06,000 --> 00:06:09,000
to the front end also contains this unique id

121
00:06:09,000 --> 00:06:11,000
that was generated.

122
00:06:11,000 --> 00:06:14,000
I guess that's convenient to have in case we would work

123
00:06:14,000 --> 00:06:18,000
with that inserted comment on the front end right away.

124
00:06:19,000 --> 00:06:21,000
So that's now the post case.

125
00:06:21,000 --> 00:06:24,000
Let's now work on the get method where we get our comments.

