1
00:00:00,780 --> 00:00:07,500
So let's write a few routines to add records to or delete records from or get records from the database,

2
00:00:07,800 --> 00:00:10,150
and let's think about the first one we want to do.

3
00:00:10,200 --> 00:00:11,580
So I'm going to start the application.

4
00:00:11,580 --> 00:00:12,370
So I'm running it.

5
00:00:13,620 --> 00:00:15,770
We're now running the application is running.

6
00:00:15,780 --> 00:00:17,490
Let's go to the application.

7
00:00:18,210 --> 00:00:19,740
Localhost 880.

8
00:00:24,570 --> 00:00:26,670
So what are some things I'm going to need to do here?

9
00:00:26,700 --> 00:00:31,810
Well, obviously book now is one that I want to do and I would do my search for availability.

10
00:00:31,830 --> 00:00:32,710
We'll do that one later.

11
00:00:33,060 --> 00:00:34,500
Right now, let's just do a simple one.

12
00:00:34,560 --> 00:00:36,800
Let's go to the Make Reservation page.

13
00:00:37,860 --> 00:00:43,050
Here is a page where I am going to be posting some information and saving it to the database.

14
00:00:43,320 --> 00:00:48,690
And there are things that I know I'm going to have basically when I look at my database structure by

15
00:00:48,690 --> 00:00:49,710
opening the model.

16
00:00:51,510 --> 00:01:00,870
So internal models, models and a reservation, everything down to here, all of this stuff needs to

17
00:01:00,870 --> 00:01:02,350
be saved to the database.

18
00:01:02,400 --> 00:01:08,730
This one room, which is actually a remodel, is not part of the database table for reservation, which

19
00:01:08,730 --> 00:01:09,900
is called reservations.

20
00:01:09,930 --> 00:01:16,710
So I need to be able to post information to my handler, pass it off to a database function and write

21
00:01:16,710 --> 00:01:18,330
that reservation to the database.

22
00:01:18,360 --> 00:01:19,490
So let's do that one first.

23
00:01:19,980 --> 00:01:28,320
So I'm going to go back to my repository package inside DB repoll and open postcards, and I'm also

24
00:01:28,320 --> 00:01:30,000
going to open the repository files.

25
00:01:30,000 --> 00:01:34,080
So let's do this first will leave the all users there will come back and do something with that later

26
00:01:34,080 --> 00:01:34,620
and clean it up.

27
00:01:35,010 --> 00:01:40,890
But let's just write a routine that's going to write a reservation to the database.

28
00:01:41,280 --> 00:01:47,760
So maybe I can just call that insert reservation and that will be a function that's going to take some

29
00:01:47,760 --> 00:01:48,270
arguments.

30
00:01:48,510 --> 00:01:54,030
And I could if I wanted to have one argument for every one of these things, but that would make for

31
00:01:54,030 --> 00:01:55,310
a very long function.

32
00:01:55,320 --> 00:02:00,360
So I'll have to put every one of those in as a parameter, it seems to me would make more sense to pass

33
00:02:00,630 --> 00:02:06,200
a rez of type models reservation and return.

34
00:02:06,570 --> 00:02:10,140
There's maybe one thing I'm going to return here, an error potentially.

35
00:02:10,400 --> 00:02:11,690
OK, so now what?

36
00:02:11,700 --> 00:02:14,030
I've added that to the repository.

37
00:02:14,640 --> 00:02:16,650
Watch what happens when I try to run the application.

38
00:02:16,770 --> 00:02:22,650
So I stop this there the screen and say run and it's going to fail and it's going to fail because this

39
00:02:22,650 --> 00:02:28,710
is an interface and an interface is actually a contract that says, hey, if you're going to implement

40
00:02:28,710 --> 00:02:33,480
this interface, which can go, you do by satisfying all of the necessary functions.

41
00:02:33,840 --> 00:02:41,100
Well, my package, as it says right here in the error message, the type postgrads DB repo doesn't

42
00:02:41,100 --> 00:02:43,970
implement the insert reservation method.

43
00:02:44,040 --> 00:02:46,000
So we have to do that before we can compile it.

44
00:02:46,020 --> 00:02:53,700
So just to make the program compiler able, let's go over here to our functions and say func M Postgrads

45
00:02:53,700 --> 00:03:01,020
Repo Postcards, DB Repo insert reservation and that will take a raise which is of type models thought

46
00:03:01,020 --> 00:03:07,260
reservation and it will return an error or nil if there's no error and we just say return nil.

47
00:03:08,400 --> 00:03:12,690
And now that we've done this, even though we're not doing anything in this function, if I try to run

48
00:03:12,690 --> 00:03:13,620
the program again.

49
00:03:17,510 --> 00:03:20,070
Now it runs with no errors, so there you go.

50
00:03:20,300 --> 00:03:21,910
That is an important thing to know.

51
00:03:21,920 --> 00:03:27,140
You can't put functions in here and expect to have them available if you put it in postgrads.

52
00:03:27,140 --> 00:03:33,530
Don't expect us to have a have this available to your application until you add its description to the

53
00:03:33,530 --> 00:03:34,970
database repo interface.

54
00:03:35,030 --> 00:03:38,210
OK, so what are we going to do in this function?

55
00:03:38,240 --> 00:03:40,700
Well, we're going to have to obviously write a query, right?

56
00:03:40,700 --> 00:03:44,630
So I'll write a query, but I'll call it a statement because I'm inserting something into the database

57
00:03:44,930 --> 00:03:48,290
and that's going to be equal to insert into.

58
00:03:48,470 --> 00:03:50,010
And what table in my answer to get into.

59
00:03:50,030 --> 00:03:51,520
Well, let's have a look at our database structure.

60
00:03:51,920 --> 00:03:59,120
We're inserting it into reservations, resins, derivations, and then describe the field you're going

61
00:03:59,120 --> 00:03:59,510
to enter.

62
00:03:59,510 --> 00:04:08,750
So I'm going to enter first name, last name, email and phone, first name, last name, email, phone,

63
00:04:11,030 --> 00:04:14,690
start date and date, relighting start date.

64
00:04:14,690 --> 00:04:15,770
And I'll just go to the next line.

65
00:04:20,470 --> 00:04:26,470
End date, start date and date remedy created updated.

66
00:04:26,890 --> 00:04:28,210
I got to have a comma here to.

67
00:04:30,860 --> 00:04:42,830
Room I'd created at, updated at and then on the next line, I'll go with values and I'll use my placeholders.

68
00:04:42,830 --> 00:04:44,390
Dollar sign, one dollar sign.

69
00:04:46,820 --> 00:04:47,830
Two, how many are there?

70
00:04:47,870 --> 00:04:54,530
One, two, three, four, five, six, seven, eight, nine dollar sign.

71
00:04:57,530 --> 00:05:05,330
Three dollar sign for dollar sign, five dollar signs, six dollar signs, seven dollar sign, eight

72
00:05:06,050 --> 00:05:09,890
dollar sign nine, and then I have to close the parentheses.

73
00:05:10,080 --> 00:05:12,070
OK, so there's my statement.

74
00:05:12,680 --> 00:05:15,320
Now, how do I actually execute that statement?

75
00:05:15,950 --> 00:05:23,000
Well, last time around when we were inserting things into the database, we used the exact exact function

76
00:05:23,000 --> 00:05:28,130
so I could actually just say if I wanted to m db dot exactly.

77
00:05:28,790 --> 00:05:35,030
And then put in my statement and passing all of the arguments and I'm going to put them on separate

78
00:05:35,030 --> 00:05:36,990
lines just to make this more readable, OK.

79
00:05:37,790 --> 00:05:47,630
So my my first name would be read first name, my last name would be read last name and then Rezo email.

80
00:05:49,010 --> 00:06:02,360
Resident phone rings resident start date resident Endi and resident remedy

81
00:06:04,910 --> 00:06:06,850
and resident created at will.

82
00:06:06,890 --> 00:06:09,290
If you don't want to bother doing that, I can if I want to.

83
00:06:09,290 --> 00:06:12,680
But all I really care about what I'm asserting is the current time and date.

84
00:06:12,680 --> 00:06:17,030
So I can just say time now and time dot now.

85
00:06:17,600 --> 00:06:22,670
Oops, not a nanosecond now and that will take care of it.

86
00:06:23,350 --> 00:06:24,710
What's this underlined for.

87
00:06:24,890 --> 00:06:26,300
No, that's just an irritating thing.

88
00:06:26,300 --> 00:06:30,890
If you don't like that underlining of the school like I don't you can get rid of it by going to goaland

89
00:06:31,220 --> 00:06:40,070
if you're using goaland preferences and look for sequel school and summer here you'll find inspection's

90
00:06:40,610 --> 00:06:49,580
where are inspections and look for school and just uncheck all sequels like that and say apply and OK,

91
00:06:50,870 --> 00:06:53,720
because Goaland is trying to connect to my database itself.

92
00:06:53,720 --> 00:06:54,920
I don't want it too.

93
00:06:55,340 --> 00:06:59,990
So I just say, don't bother telling me about the fact that you can't connect to the database.

94
00:07:00,080 --> 00:07:01,570
That's just an aside.

95
00:07:01,910 --> 00:07:03,050
Now, this will actually work.

96
00:07:03,230 --> 00:07:04,910
I can I can actually do this.

97
00:07:05,150 --> 00:07:09,650
And of course, when I call this, I'm getting a result and an error.

98
00:07:09,680 --> 00:07:16,310
I can ignore the result if I want to and just say error is assigned and then trap for an error.

99
00:07:17,030 --> 00:07:25,550
If error is not equal to nil return error and that will actually work, there's no problem with that

100
00:07:25,550 --> 00:07:26,210
whatsoever.

101
00:07:27,270 --> 00:07:35,670
However, we're in a Web application and Web applications are unpredictable in one sense, for example,

102
00:07:36,120 --> 00:07:40,910
the user might lose his or her connection to the Internet partway through this transaction.

103
00:07:41,580 --> 00:07:43,080
And what happens then?

104
00:07:43,620 --> 00:07:48,420
The user might just close their browser without warning in the midst of something that's taking place

105
00:07:48,420 --> 00:07:49,810
with the database in the background.

106
00:07:50,220 --> 00:07:55,980
So what we can do, what we want to have happen here, as we want to make sure that this transaction

107
00:07:55,980 --> 00:08:01,760
doesn't stay open for the five minute lifetime that we have for transactions if nothing is happening.

108
00:08:02,310 --> 00:08:07,410
So I'm going to take advantage of the context, something that was built in to go right around, go

109
00:08:07,410 --> 00:08:08,610
one point eight, I think it was.

110
00:08:09,300 --> 00:08:10,740
And it's called the context.

111
00:08:10,740 --> 00:08:18,150
And we do that by at the very top of our function doing this context and cancel because I want to cancel

112
00:08:18,150 --> 00:08:19,020
my context.

113
00:08:19,320 --> 00:08:25,710
If something goes wrong, that's a sign, the value of context with time out.

114
00:08:26,930 --> 00:08:33,060
OK, context with time out, and I'm going to pass it, a context that is always available anywhere

115
00:08:33,060 --> 00:08:40,130
in your application called context dot background, and I'm going to give it a timeout and I'm going

116
00:08:40,130 --> 00:08:46,820
to say, if this transaction takes longer than X number of seconds, DI, just cancel it and send a

117
00:08:46,820 --> 00:08:49,100
capsule back and everything will go back to where it should be.

118
00:08:49,580 --> 00:08:55,640
And I think that for our purposes, if you can't insert this transaction in three seconds, something

119
00:08:55,640 --> 00:08:57,520
is seriously wrong with our application.

120
00:08:57,530 --> 00:08:59,950
So I'll say three times time.

121
00:09:00,230 --> 00:09:04,170
Second, which gives this a three second timeout.

122
00:09:04,430 --> 00:09:09,020
OK, so now that this is here, I need to say defer.

123
00:09:09,530 --> 00:09:10,010
Cancel.

124
00:09:12,340 --> 00:09:18,160
That's all I need to do, except I'm not using this right now, because exactly this function is built

125
00:09:18,160 --> 00:09:19,270
into the database package.

126
00:09:19,660 --> 00:09:25,230
It doesn't know anything about context whatsoever, but this one does exact context.

127
00:09:26,920 --> 00:09:31,330
So if I do that, the first argument is actually the context that I declared at the very beginning of

128
00:09:31,330 --> 00:09:31,870
this function.

129
00:09:32,440 --> 00:09:37,360
Now I have a much safer and more robust means of talking to the database.

130
00:09:37,540 --> 00:09:42,640
So with virtually identical to what we did in our first experiments with the database, the only difference

131
00:09:42,640 --> 00:09:47,810
is instead of calling exactly what I call exact context, and that's true for the other methods, for

132
00:09:47,870 --> 00:09:51,170
ones, for updating and deleting, we can use the context there as well.

133
00:09:51,850 --> 00:09:56,170
So I now have this function called insert reservation and I'll better give it a comment.

134
00:09:57,550 --> 00:10:01,690
Inserts a reservation into the database.

135
00:10:02,920 --> 00:10:06,160
We now have a means of storing it and that is extremely helpful.

136
00:10:06,250 --> 00:10:15,550
So we can go back to our handler now and find the handler that does post reservation right here.

137
00:10:16,480 --> 00:10:20,150
And instead of just building this, we're going to have to change our form.

138
00:10:20,170 --> 00:10:22,690
I'm also going to want the start date, the end date.

139
00:10:22,690 --> 00:10:24,790
And what other information am I putting in there?

140
00:10:24,820 --> 00:10:25,870
I can't remember now.

141
00:10:27,670 --> 00:10:29,830
So we have the first name, last name, email, phone.

142
00:10:29,830 --> 00:10:32,140
We need the start date, the end date and the room ID.

143
00:10:32,380 --> 00:10:36,640
The other two are created with our time now, which you shouldn't search the current date and time into

144
00:10:36,640 --> 00:10:36,760
it.

145
00:10:37,090 --> 00:10:38,440
So we'll have to change our form.

146
00:10:38,470 --> 00:10:39,550
We can do that afterwards.

147
00:10:39,550 --> 00:10:40,590
Let's just fix our handler.

148
00:10:41,080 --> 00:10:43,900
So start date and end date, OK?

149
00:10:43,930 --> 00:10:51,310
Now these are a little bit different because we're receiving our date as a string and we need to convert

150
00:10:51,310 --> 00:10:53,680
it into the format that our model expects.

151
00:10:53,710 --> 00:11:02,410
For example, if I try this, if I just say start date is ARG form doget start date, which doesn't

152
00:11:02,410 --> 00:11:02,890
exist yet.

153
00:11:02,890 --> 00:11:05,010
But it will it's going to it's going to die.

154
00:11:05,020 --> 00:11:06,610
It says, well, hang on a second here.

155
00:11:06,820 --> 00:11:12,730
Cannot use our form arget start date, which is type string as tight time dot time.

156
00:11:13,150 --> 00:11:17,770
So what I need to do is get our start date and end date which I can do.

157
00:11:17,770 --> 00:11:24,910
Like this SSD is a sign the value of that and ID for end date is a sign the value of end date.

158
00:11:25,690 --> 00:11:32,260
I can store those strings right here and then I need to cast those strings over to a time.

159
00:11:32,530 --> 00:11:34,360
Time now.

160
00:11:34,360 --> 00:11:38,350
One of the things that I know I'm going to get my date formatted is going to come something like this

161
00:11:38,350 --> 00:11:40,000
20-20 zero one zero one.

162
00:11:40,360 --> 00:11:44,560
That will be the format that I get my date in my starting date.

163
00:11:44,560 --> 00:11:53,530
Am I end it the way that Go handles dates and times is a little unusual and it might seem a little non-intuitive

164
00:11:53,530 --> 00:11:53,950
at first.

165
00:11:53,960 --> 00:11:57,970
Certainly I have to look this up almost every time I go back and work with dates.

166
00:11:58,570 --> 00:12:04,690
So let me go over to my Web browser or I have open this blog post and I'll put a cop up, post a link

167
00:12:04,690 --> 00:12:07,650
to this right on to the resources for this page.

168
00:12:08,410 --> 00:12:15,460
This is how you pass and format dates and times Engo and if you look at it go takes an interesting approach

169
00:12:15,460 --> 00:12:18,910
to parsing strings to time objects and formatting objects at strings.

170
00:12:18,910 --> 00:12:25,240
And you can follow that link and it gives an example there, but it uses a non-standard reference time

171
00:12:25,240 --> 00:12:26,050
engo.

172
00:12:26,230 --> 00:12:33,160
The reference date and time are Monday, January the 2nd, at four minutes and five seconds after three

173
00:12:33,160 --> 00:12:37,450
o'clock in the afternoon Mountain Standard Time in the year 2006.

174
00:12:38,170 --> 00:12:43,960
And the reason they've gone with this is because using the standard method of writing dates in America,

175
00:12:44,140 --> 00:12:53,980
it goes as follows zero one zero two zero three zero four 05 06 for 2006 and minus seven GMT for Mountain

176
00:12:53,980 --> 00:12:54,670
Standard Time.

177
00:12:54,940 --> 00:12:57,520
And apparently that's an easy way to remember dates.

178
00:12:57,520 --> 00:12:58,810
It might be for other people.

179
00:12:58,810 --> 00:12:59,590
It's not for me.

180
00:12:59,620 --> 00:13:03,520
I have to look it up every time, so don't feel bad if you have to look it up as well.

181
00:13:05,590 --> 00:13:11,140
Now, to convert our string to a time, it's a relatively straightforward approach once you know the

182
00:13:11,140 --> 00:13:11,560
format.

183
00:13:11,560 --> 00:13:19,780
So I'm going to copy this format here like this and just paste it in my comment like this so I have

184
00:13:19,780 --> 00:13:21,010
a reference to it, OK?

185
00:13:21,700 --> 00:13:28,870
And I need to do what I need to do is to describe the format that my date is coming in, using this

186
00:13:28,870 --> 00:13:30,010
as a kind of reference.

187
00:13:30,010 --> 00:13:32,560
OK, so I can do it as follows.

188
00:13:32,560 --> 00:13:39,010
What I'm going to do is describe a layout and the layout will be a string that describes my date format

189
00:13:39,400 --> 00:13:42,130
and the format I'm getting my date in.

190
00:13:42,130 --> 00:13:48,940
The string that I'm receiving from my form will be in the form of the year and a half to use 2006 as

191
00:13:48,940 --> 00:13:55,450
a reference here, followed by the month, which is zero one in the reference time, followed by the

192
00:13:55,450 --> 00:13:57,900
date, which is zero two in reference time.

193
00:13:57,910 --> 00:14:00,430
That's the format I'm getting my date in.

194
00:14:00,550 --> 00:14:08,140
OK, now I want to pass SD and ID from what they are into times and I can do that as follows.

195
00:14:08,170 --> 00:14:10,240
I can just say start.

196
00:14:10,470 --> 00:14:22,230
Date and then potentially an error is assigned the value of time, don't pass my layout and the date

197
00:14:22,230 --> 00:14:28,790
I want to pass, which is SD for start date, OK, and I can do the same thing for end date.

198
00:14:30,460 --> 00:14:36,070
But it's not start date, this case, it's ended up passing and then need to for my errors, if error

199
00:14:36,070 --> 00:14:39,040
is not equal to nil, then what do I want to do?

200
00:14:39,040 --> 00:14:43,180
Helper's dot server error and an error.

201
00:14:43,480 --> 00:14:46,480
So I'll just call my server error and the same thing here.

202
00:14:50,110 --> 00:14:58,120
Now that I have the start date and end date, I can actually populate my model with start date and end

203
00:14:58,120 --> 00:14:58,330
date.

204
00:15:00,620 --> 00:15:01,390
Which will be ended.

205
00:15:03,890 --> 00:15:12,350
OK, the last thing I need is my remedy and again, remedy in our model is actually an INT, so I need

206
00:15:12,350 --> 00:15:14,630
to convert what I'm going to get from the form.

207
00:15:14,630 --> 00:15:16,550
And I'm not passing that yet and I'm aware of it.

208
00:15:17,390 --> 00:15:19,790
I need to convert that from a string into an ENT.

209
00:15:19,970 --> 00:15:21,440
That's again, not that difficult.

210
00:15:21,440 --> 00:15:28,880
So I can just say room ID is assigned and er potentially an error is assigned the value of and I use

211
00:15:28,880 --> 00:15:38,480
a built in package as part of the standard library sirc invis sterkel a for alpha to integer and the

212
00:15:38,480 --> 00:15:44,690
string and the string I'll get from my form as adon form dot get room ID.

213
00:15:44,960 --> 00:15:47,420
I'll assume that's going to be the name of the parameter the past.

214
00:15:47,420 --> 00:15:51,980
Just as I'm assuming the name of the parameter I'm going to get for start date is start underscore date

215
00:15:51,980 --> 00:15:53,540
and end date and underscore date.

216
00:15:53,900 --> 00:15:56,990
Once I have this I get an idea from my error.

217
00:15:58,760 --> 00:16:05,120
Once I have this I could use my remedy like this room ID is remedy.

218
00:16:06,440 --> 00:16:06,830
All right.

219
00:16:06,830 --> 00:16:13,970
Now that I've built this reservation model, what I actually want to do is after my form validation,

220
00:16:13,970 --> 00:16:19,220
making sure all of this is accurate, after my form validation, I actually want to write this information

221
00:16:19,220 --> 00:16:27,560
to the database and I can do that by going m db dot insert reservation and passing in my reservation,

222
00:16:27,560 --> 00:16:28,940
which I called reservation.

223
00:16:33,410 --> 00:16:36,110
And of course this handles an error to us.

224
00:16:36,140 --> 00:16:37,550
So I can say error equals.

225
00:16:40,520 --> 00:16:52,130
If error is not equal to nil, then helper's that server error W and hair and that while she said that

226
00:16:52,140 --> 00:16:58,310
to our database, so we can't test this until we actually go update our form, but we can do that and

227
00:16:58,310 --> 00:17:02,750
we'll do it and we'll just put some some default values in there to make sure that it's working the

228
00:17:02,750 --> 00:17:03,320
way we want.

229
00:17:03,740 --> 00:17:06,410
And we'll test that and we'll do that in the next lecture.
