1
00:00:00,990 --> 00:00:08,070
So what we want to do this time is we want to modify this function, insert reservation to return not

2
00:00:08,070 --> 00:00:12,180
just the error, but also the ID of the newly inserted record.

3
00:00:12,360 --> 00:00:17,300
And I've already added the returning I.D. skills necessary into the query.

4
00:00:17,760 --> 00:00:23,400
So the first thing I'll do is modify this to say it returns not just an error, but an int and an error.

5
00:00:24,900 --> 00:00:30,390
And then I'll make this return zero, because if there's an error, we don't really care what the ideas.

6
00:00:30,750 --> 00:00:34,580
But here I'll return a variable called New ID, which doesn't exist.

7
00:00:35,100 --> 00:00:38,270
So let's go declare it new ID.

8
00:00:38,280 --> 00:00:42,210
Sorry var new ID is equal isn't it.

9
00:00:43,170 --> 00:00:43,890
So that creates.

10
00:00:44,610 --> 00:00:48,840
Now I want to actually change it from an executive which doesn't insert.

11
00:00:49,080 --> 00:00:54,960
I'm going to change this to a query and I'm going to change the query because actually what we're doing

12
00:00:54,960 --> 00:00:57,310
is returning something from our sequel.

13
00:00:57,330 --> 00:01:00,750
So it is, it's an, it's an insert statement but it's also a query.

14
00:01:01,110 --> 00:01:04,620
So the method that does that returns only an error.

15
00:01:04,620 --> 00:01:06,710
And it's not called exact context.

16
00:01:06,720 --> 00:01:09,360
It's called query row context.

17
00:01:10,560 --> 00:01:15,960
And the only thing I have to do is because I'm actually doing a query row is I now have available the

18
00:01:15,960 --> 00:01:24,090
scan function and I'll scan the one thing being returned, which is the ID into my variable new ID and

19
00:01:24,120 --> 00:01:25,460
that's all I have to do here.

20
00:01:25,650 --> 00:01:32,610
Now I obviously, because I've changed the syntax of this function in postgrads, don't go.

21
00:01:32,830 --> 00:01:38,700
I'll copy the whole thing, go back to my repository and replace the old version with the new version.

22
00:01:39,270 --> 00:01:39,690
Perfect.

23
00:01:40,400 --> 00:01:42,360
So that means my handler is going to change too.

24
00:01:42,690 --> 00:01:45,480
So here I'm no longer getting one value back.

25
00:01:45,720 --> 00:01:52,190
I'm actually getting a new reservation ID and an error potentially there.

26
00:01:52,740 --> 00:01:54,580
So I'm not using that yet, but I will in a moment.

27
00:01:55,200 --> 00:02:01,950
So what I'm going to do with this is I'm going to use this new reservation ID, along with all the other

28
00:02:01,950 --> 00:02:09,300
required information to build up a room restriction model and send that to a functional create in a

29
00:02:09,300 --> 00:02:12,890
moment, which I'll probably call insert room restriction.

30
00:02:12,900 --> 00:02:14,430
So let's build up model first.

31
00:02:14,970 --> 00:02:22,140
We can build the model just by saying restriction or color restriction is going to be assigned the value

32
00:02:22,140 --> 00:02:24,390
of models, not room restriction.

33
00:02:24,690 --> 00:02:29,640
And I'm going to use a feature built into this idea called Fill All Fields, which just saves me some

34
00:02:29,640 --> 00:02:30,000
typing.

35
00:02:30,540 --> 00:02:34,020
I don't need the idea of the the restriction I'm inserting.

36
00:02:34,020 --> 00:02:34,770
I don't know what it is.

37
00:02:34,770 --> 00:02:35,580
I don't care what it is.

38
00:02:35,580 --> 00:02:36,870
I just want to insert something.

39
00:02:37,170 --> 00:02:41,370
I do have a start date and I have a start date in my variable called start date.

40
00:02:42,090 --> 00:02:49,080
And I do have the end date in a variable called End Date and the room ID, I have that to remind and

41
00:02:49,080 --> 00:02:57,450
reservation id I now have because it's the new reservation ID, new reservation ID, new reservation

42
00:02:57,450 --> 00:02:57,870
ID.

43
00:02:57,870 --> 00:02:58,980
I've got to make this in a sign.

44
00:02:58,980 --> 00:02:59,700
That's why it's an error.

45
00:03:00,780 --> 00:03:03,240
I have new reservation ID so I'll put that in here.

46
00:03:03,990 --> 00:03:09,720
The restriction ID, I will make one and I'll have to fill up my database field before I can actually

47
00:03:09,720 --> 00:03:10,320
use that.

48
00:03:10,320 --> 00:03:15,720
I don't have any restriction IDs yet, so I'll just populate that and make that a reservation restriction

49
00:03:15,720 --> 00:03:18,990
idea of type reservation created an updated out.

50
00:03:18,990 --> 00:03:23,910
I'll let that be taken care of at the database level and I don't need these three because these are

51
00:03:23,910 --> 00:03:27,660
just things I might use in the future, but they're not actually part of the database field.

52
00:03:27,900 --> 00:03:29,190
So I now have this restriction.

53
00:03:29,640 --> 00:03:36,240
So let's go over to our postgrads and create a new function called func.

54
00:03:36,240 --> 00:03:44,640
And it has to be on the receiver and have a receiver of M Postgres and I'll call it insert room restriction.

55
00:03:45,120 --> 00:03:51,900
It will take one argument, which I'll call her, and it's a type models room restriction and all it's

56
00:03:51,900 --> 00:03:57,000
going to return is an error or nil if there's no error and I'll just say return nil just so I can get

57
00:03:57,000 --> 00:04:03,630
this up and running then because I've created this, I actually had to close that because I've created

58
00:04:03,630 --> 00:04:04,110
this.

59
00:04:05,130 --> 00:04:12,480
I need to hopefully if I can stop clicking the wrong buttons, copy this whole thing, fixed that.

60
00:04:12,480 --> 00:04:16,170
So it says postgrads DB repo, copy this whole thing.

61
00:04:18,090 --> 00:04:19,440
And put it in my repository.

62
00:04:21,450 --> 00:04:28,860
OK, and now we go back to our handlers and we can actually call that method on our database repository

63
00:04:29,190 --> 00:04:31,620
and it's just as simple as error is assigned.

64
00:04:31,620 --> 00:04:38,720
The value of M dot db dot insert room restriction and pass it are restriction.

65
00:04:39,630 --> 00:04:40,710
And if there's an error.

66
00:04:44,340 --> 00:04:50,070
Paste it in here and you may notice that I accidentally left out the return function in my error check

67
00:04:50,070 --> 00:04:52,110
and I added that before I started this lecture.

68
00:04:52,120 --> 00:04:56,610
So I added it here and I added it here and I added it here.

69
00:04:56,610 --> 00:05:01,470
And if I didn't do that, things would still work, but it wouldn't stop execution at this point.

70
00:05:01,470 --> 00:05:03,660
And I do want things to stop at this point.

71
00:05:03,670 --> 00:05:08,430
I don't want it to say you can't pass the date and try to do everything else because it'll just fail

72
00:05:08,430 --> 00:05:10,230
when it gets down to the database insert part.

73
00:05:10,250 --> 00:05:12,870
So just out of return, as I did in those three places.

74
00:05:13,320 --> 00:05:17,330
Anyway, I have now created my function, which doesn't do anything yet.

75
00:05:17,340 --> 00:05:19,950
Want to add that and I've called my function.

76
00:05:20,280 --> 00:05:27,750
But before I forget, let's go back over to our database and find our bookings database and find the

77
00:05:27,750 --> 00:05:33,900
restrictions and add one restriction and a restriction of type one, which is what will be added in

78
00:05:33,900 --> 00:05:37,500
for us will be a restriction of type reservation.

79
00:05:37,920 --> 00:05:42,080
And I got to put some dates in there, so I'll put in a date and a date.

80
00:05:46,280 --> 00:05:56,210
OK, and save that there, I have one in there now, so let's go back and finish up our actual routine

81
00:05:56,210 --> 00:05:57,620
to insert this restriction.

82
00:05:57,680 --> 00:05:59,090
So this is going to be a statement.

83
00:05:59,150 --> 00:06:05,420
But first of all, it's copy and paste this context stuff so I don't have to retype it all pasted in

84
00:06:05,420 --> 00:06:05,870
here.

85
00:06:06,110 --> 00:06:07,420
And let's read our statement.

86
00:06:07,430 --> 00:06:13,990
Statement is a sign the value of insert into our database table name is removed restrictions.

87
00:06:15,050 --> 00:06:16,560
And what do we want to insert in there?

88
00:06:16,580 --> 00:06:17,960
Let's go look at our structure again.

89
00:06:19,250 --> 00:06:21,770
It's going to be a start date and date remedy.

90
00:06:22,970 --> 00:06:32,480
The start date and date room ID, which is the lowercase art reservation ID, created an updated restriction

91
00:06:33,320 --> 00:06:46,760
rather patient ID and then we'll go tab over created at, updated at and restriction idy.

92
00:06:47,780 --> 00:06:52,120
So have all this information passed to us in the variable called Earth.

93
00:06:53,420 --> 00:06:57,650
So we're going to execute that query and we're going to get a result that I don't care about.

94
00:06:57,920 --> 00:07:00,440
Potentially an error that I do carry about, care about.

95
00:07:00,680 --> 00:07:09,230
And we'll call our database function m dot db dot exact context.

96
00:07:10,100 --> 00:07:13,610
The first parameter is the context that I create at the top of the function.

97
00:07:13,940 --> 00:07:19,460
The second parameter is my query, which is a statement, and then all of the arguments and I'll put

98
00:07:19,460 --> 00:07:22,550
again all of those on their own line just so they're easier to read.

99
00:07:22,880 --> 00:07:26,000
So I left something out of my query here.

100
00:07:26,030 --> 00:07:31,520
I'm glad that I noticed that we insert that and then we have to close the parentheses and on another

101
00:07:31,520 --> 00:07:35,570
line say values and the values are dollar sign one.

102
00:07:35,570 --> 00:07:36,320
And how many are there?

103
00:07:36,320 --> 00:07:41,120
One, two, three, four, five, six, seven dollar sign.

104
00:07:41,120 --> 00:07:42,410
Two dollar sign.

105
00:07:42,410 --> 00:07:48,710
Three dollar sign for dollar sign, five dollar sign, six dollar sign seven.

106
00:07:49,070 --> 00:07:50,590
And the parentheses are closed.

107
00:07:50,600 --> 00:07:51,410
That looks better.

108
00:07:52,220 --> 00:07:54,920
So we want our dot and what's the first thing.

109
00:07:54,920 --> 00:08:08,480
Start date and then our end date and then our dot remedy and then not reservation ID and then our dot

110
00:08:08,480 --> 00:08:09,110
created out.

111
00:08:09,110 --> 00:08:21,420
Now time now for Created a time dot now for updated at and finally our dot restriction ID.

112
00:08:23,470 --> 00:08:26,420
And the closing comment, and that should do it.

113
00:08:26,680 --> 00:08:28,000
We've got a check for our error, though.

114
00:08:28,450 --> 00:08:32,710
If error is not equal to nil, just return the error.

115
00:08:34,510 --> 00:08:37,600
OK, so let's make sure that we have this.

116
00:08:37,600 --> 00:08:39,580
All of this is in our repository.

117
00:08:40,150 --> 00:08:40,840
It is.

118
00:08:41,800 --> 00:08:45,520
There are no errors being displayed on our postgrads.

119
00:08:45,670 --> 00:08:47,430
There are not there is no comment.

120
00:08:47,440 --> 00:08:48,250
Let's add one.

121
00:08:52,800 --> 00:08:57,630
Inserts a room restriction into the database.

122
00:08:58,050 --> 00:09:00,710
OK, all looks good.

123
00:09:03,100 --> 00:09:03,610
Yes.

124
00:09:03,650 --> 00:09:05,390
OK, and let's check our handlers.

125
00:09:06,700 --> 00:09:09,950
We have that we have a value for restriction idee in the database.

126
00:09:09,970 --> 00:09:10,480
Perfect.

127
00:09:10,780 --> 00:09:13,720
Let's stop our data or stop our application and start it up again.

128
00:09:14,620 --> 00:09:16,020
OK, it's not running, so let's run it.

129
00:09:20,550 --> 00:09:23,970
It's running, let's go back to our Web browser.

130
00:09:24,000 --> 00:09:30,360
I'll refresh the page to make sure that I have a valid CSF token and put some values in here.

131
00:09:30,360 --> 00:09:39,490
John Doe start date 2020 zero zero two zero one to 2020 to the past.

132
00:09:39,510 --> 00:09:44,910
I want to do that 2021 02 01 to 2021.

133
00:09:45,060 --> 00:09:49,470
020 for email is John.

134
00:09:52,480 --> 00:09:59,350
At Dotcom and his phone number is five, six seven six seven eight zero nine eight seven.

135
00:09:59,810 --> 00:10:02,080
OK, let's hope that works.

136
00:10:03,370 --> 00:10:04,030
And it did.

137
00:10:04,090 --> 00:10:09,620
Now, let's go look at our database, which is right here because I'm using postal code.

138
00:10:10,060 --> 00:10:11,380
There should be another reservation.

139
00:10:11,440 --> 00:10:12,340
That's a reservation.

140
00:10:12,340 --> 00:10:13,300
That's our reservations.

141
00:10:13,300 --> 00:10:13,900
Restrictions.

142
00:10:13,900 --> 00:10:14,520
Those are fine.

143
00:10:15,130 --> 00:10:15,760
There should now be.

144
00:10:15,760 --> 00:10:16,360
Hey, look at that.

145
00:10:16,720 --> 00:10:20,890
I have to because I did this and before this lecture started and forgot to delete it.

146
00:10:20,890 --> 00:10:21,280
Sorry.

147
00:10:21,670 --> 00:10:22,690
So ignore that one.

148
00:10:22,840 --> 00:10:26,170
But I do have this one, which is the one that I just added.

149
00:10:26,620 --> 00:10:27,190
Perfect.

150
00:10:27,370 --> 00:10:33,250
And let's look under some restrictions and we have the room restriction entered and it's from 2001 to

151
00:10:33,250 --> 00:10:34,330
2004.

152
00:10:34,360 --> 00:10:40,180
Remedy one reservation IDs three and the restriction ID is one and that's perfect.

153
00:10:40,360 --> 00:10:47,710
So now we have a means of inserting not just a reservation, but also the restrictions for that room,

154
00:10:47,710 --> 00:10:50,260
for the dates of the reservation we just saved.

155
00:10:50,560 --> 00:10:52,120
So we're making some progress.

156
00:10:52,570 --> 00:10:55,900
We'll move on in the next lecture and do some more work with the database.
