1
00:00:01,210 --> 00:00:06,850
So now we have reservations showing up on the calendar, as they should, and we also have blocks showing

2
00:00:06,850 --> 00:00:13,150
up as checks and we've set up a handler to handle the form post, but we're not doing any anything with

3
00:00:13,150 --> 00:00:13,660
it right now.

4
00:00:13,660 --> 00:00:20,710
So let's get started handling the situations where someone uncheck a an existing block or were they

5
00:00:20,710 --> 00:00:23,440
add a new block and making sure those changes are saved.

6
00:00:23,860 --> 00:00:26,100
And there's a few steps necessary to make this happen.

7
00:00:26,440 --> 00:00:35,650
We're going to have to add some code to our Postgres database functions to add new blocks and to delete

8
00:00:35,650 --> 00:00:36,640
existing blocks.

9
00:00:36,640 --> 00:00:40,920
And we need to deal with the actual posted form data itself.

10
00:00:40,930 --> 00:00:41,770
So let's get started.

11
00:00:42,430 --> 00:00:48,370
So I'm going to go over to my idea and I'm looking at admin post reservations calendar, which handles

12
00:00:48,370 --> 00:00:49,840
a post a reservation calendar.

13
00:00:50,170 --> 00:00:55,300
And so far all we're doing is passing the form, getting the year in the month and putting a message

14
00:00:55,300 --> 00:00:56,860
in the session and then redirecting.

15
00:00:57,100 --> 00:00:59,020
And now we need to process the blocks.

16
00:00:59,620 --> 00:01:05,380
So in order to make that happen, if you think about it, what we're posting here is something for one

17
00:01:05,380 --> 00:01:06,800
room, something for the next room.

18
00:01:06,820 --> 00:01:09,010
In other words, we're handling all of the rooms.

19
00:01:09,430 --> 00:01:12,880
So clearly we need to get the rooms in our handler and we know how to do that.

20
00:01:12,880 --> 00:01:24,970
So we can just say rooms and potentially an error come from Magdeburg, all rooms.

21
00:01:25,870 --> 00:01:26,950
And that gets us our rooms.

22
00:01:26,950 --> 00:01:28,060
So we'll check for an error.

23
00:01:28,090 --> 00:01:36,910
If error is not equal to nil, then helper's dot server error and the error and return.

24
00:01:37,660 --> 00:01:39,610
Otherwise we can loop through our rooms.

25
00:01:40,330 --> 00:01:48,880
So we go for ignore the index and we'll just call the iteration variable X and we'll arrange through

26
00:01:48,880 --> 00:01:49,310
rooms.

27
00:01:51,640 --> 00:01:54,130
Now what are we going to do as we go through these rooms?

28
00:01:54,160 --> 00:01:58,710
Well, let's write some comments and think the process through before we actually do it.

29
00:01:59,200 --> 00:02:03,910
The first thing I'm going to do is I'm going to grab the block map that we stuck into the session back

30
00:02:03,910 --> 00:02:06,850
in the get handler for this page.

31
00:02:07,180 --> 00:02:09,160
So it will say get the block map.

32
00:02:12,260 --> 00:02:20,000
From the session and as you know, that bloc map contains all the necessary or all the blocks that exist

33
00:02:20,060 --> 00:02:24,390
for a given room at the point where the calendar was displayed.

34
00:02:24,530 --> 00:02:29,870
So that's the data as it was before the user clicks submit on the calendar page.

35
00:02:31,010 --> 00:02:36,350
So then we're going to loop through that map loop through the entire map.

36
00:02:37,790 --> 00:02:42,350
And if we have an entry in the map.

37
00:02:50,890 --> 00:03:04,000
That does not exist in our posted data, and if the restriction ID is greater than zero, then it is

38
00:03:04,000 --> 00:03:07,870
a block we need to remove.

39
00:03:08,170 --> 00:03:09,480
So let's think about this for a minute.

40
00:03:09,970 --> 00:03:12,420
So I'm on this page and I'll make sure it's current data.

41
00:03:12,460 --> 00:03:13,560
So I just refreshed it.

42
00:03:14,140 --> 00:03:19,750
We have this eight, for example, the 8th of December for general quarters.

43
00:03:19,750 --> 00:03:20,860
And if I inspect that.

44
00:03:22,720 --> 00:03:25,700
And actually has a value of 19.

45
00:03:26,230 --> 00:03:34,480
Now, if I uncheck this and then submit the data, then this eight is no longer in the posted form data,

46
00:03:35,560 --> 00:03:41,500
but it still exists in our map because that's a copy of the data, as it was before any changes were

47
00:03:41,500 --> 00:03:45,890
made to the map and in the map, it has a value of greater than zero.

48
00:03:46,150 --> 00:03:48,480
So that is something that has to be removed.

49
00:03:48,490 --> 00:03:50,200
So that's what we're going to try to do here.

50
00:03:50,350 --> 00:03:51,220
How are we going to do that?

51
00:03:51,760 --> 00:03:57,340
Well, first of all, we need to check to see if the form posted form data has that field.

52
00:03:57,340 --> 00:04:02,740
And as you know, we created some time ago this function in the form.

53
00:04:02,750 --> 00:04:12,610
So if I can just get the form, form is equal to forms new and that comes from our post form.

54
00:04:13,750 --> 00:04:18,490
So now I have access to the has function in there so we can actually, first of all, get the session.

55
00:04:18,520 --> 00:04:19,210
Let's do that.

56
00:04:20,260 --> 00:04:29,020
So Campath I'll just call it current map for her map, my app dot session dot get and we need to give

57
00:04:29,020 --> 00:04:35,390
it the context, our context and then we need to get the name formatters.

58
00:04:35,470 --> 00:04:45,740
Print F and my string will be block underscore, map underscore and then an int and my int will be exact

59
00:04:45,770 --> 00:04:46,270
ID.

60
00:04:48,190 --> 00:04:54,340
And once we get that of course because we're pulling it from the session, we need to cast it back to

61
00:04:54,340 --> 00:04:56,080
a map string.

62
00:04:57,120 --> 00:05:03,240
And so now I have my map, so then we go through the entire map, we loop through it, just as I say

63
00:05:03,240 --> 00:05:06,330
in my comment, for name value.

64
00:05:07,930 --> 00:05:12,510
And those are just two variable names I've chosen equals range car map.

65
00:05:14,880 --> 00:05:22,080
And then we need to make sure that the value exists in the map and that the value is greater than zero.

66
00:05:22,110 --> 00:05:25,440
So the entry exists in the map and its value is greater than zero.

67
00:05:25,560 --> 00:05:27,900
And if it if that's the case, we have to remove it.

68
00:05:27,910 --> 00:05:30,270
So we'll just check to see if it exists in the map.

69
00:05:30,810 --> 00:05:37,620
So in this situation, the variable, OK, which I'm about to create in a moment, will be false if

70
00:05:37,620 --> 00:05:40,080
the value is not in the map.

71
00:05:41,910 --> 00:05:44,580
So I can check to see if it's in the map this way.

72
00:05:44,580 --> 00:05:53,070
If Val, which is a variable that will have a value if it exists in the map, OK, is equal to the map

73
00:05:54,660 --> 00:05:58,230
and check for the name, which will be the correctly formatted date.

74
00:05:58,950 --> 00:06:05,850
And OK, so in this situation, OK, will be set to false if the value is not in the map.

75
00:06:07,380 --> 00:06:16,050
And then we once we get past this, if we're in a situation where it exists, only pay attention to

76
00:06:16,050 --> 00:06:23,290
values greater than zero because their actual blocks and that are not in the form post.

77
00:06:23,760 --> 00:06:25,560
In other words, they've been unchecked.

78
00:06:27,030 --> 00:06:33,750
The rest are just placeholders for days without bloks.

79
00:06:38,300 --> 00:06:42,860
So we can check to see if it exists, if Val is greater than zero.

80
00:06:46,080 --> 00:06:57,450
And if not, form DOT has and then we check for the name, format, format, dot, sprint f remove block,

81
00:06:57,600 --> 00:07:03,780
which is what it would be named that an int, which is the remedy then the date, which is the name

82
00:07:03,780 --> 00:07:04,760
in this situation.

83
00:07:06,630 --> 00:07:13,170
So we do our substitutions for the remedy and name which we're getting by looping through this crap.

84
00:07:19,430 --> 00:07:25,280
And if that's the case, we need to delete the restriction by Edet now, we don't have that written

85
00:07:25,280 --> 00:07:33,140
yet, but we can see if this works just by saying log print line would delete, block and then pass

86
00:07:33,140 --> 00:07:33,750
in the value.

87
00:07:35,450 --> 00:07:35,920
All right.

88
00:07:35,930 --> 00:07:44,350
In theory, this should give us a log entry every time we uncheck something and then submit the form.

89
00:07:44,360 --> 00:07:45,710
So let's stop our application.

90
00:07:46,820 --> 00:07:47,930
Start our application.

91
00:07:50,670 --> 00:07:55,920
Give myself a few blank lines just so I can see things, and I'll go back here, reload the reservation

92
00:07:55,920 --> 00:08:03,990
calendar, uncheck the 12th four generals quarters and save changes and then look at our log file would

93
00:08:03,990 --> 00:08:05,200
delete block twenty.

94
00:08:05,490 --> 00:08:06,760
That's exactly what I wanted.

95
00:08:06,780 --> 00:08:11,100
I uncheck the 12th and if I inspect that, that should have a value of 20.

96
00:08:13,000 --> 00:08:15,010
And it does so that's going to work.

97
00:08:15,040 --> 00:08:16,800
All right, so that's the first step.

98
00:08:16,810 --> 00:08:23,500
And we'll write the code to delete the restriction in a moment, but we also have to deal with adding

99
00:08:23,500 --> 00:08:24,060
blocks.

100
00:08:24,340 --> 00:08:30,520
So let's go down here outside of this for Loop now, handle new blocks.

101
00:08:33,070 --> 00:08:36,310
Now, in this situation, it's pretty much the same.

102
00:08:36,310 --> 00:08:43,330
All we're going to do in this case is loop through the entire post form and look for any entries that

103
00:08:43,330 --> 00:08:46,150
have the name, starting with Adblock.

104
00:08:46,540 --> 00:08:50,590
So I will just loop through the entire posted form data for name.

105
00:08:51,130 --> 00:08:52,390
And we don't care what the value is.

106
00:08:53,110 --> 00:08:57,760
We're just checking for the name right now equals or is assigned the value of range and will range through

107
00:08:57,760 --> 00:08:59,460
the entire ARG post form.

108
00:08:59,620 --> 00:09:02,860
So we're going to go through every single entry in the post form.

109
00:09:03,010 --> 00:09:05,170
And just to see how this works, let's just log it.

110
00:09:05,440 --> 00:09:12,710
Log print line form has a name and name just to see what we're going to get.

111
00:09:13,000 --> 00:09:14,500
So let's stop our application.

112
00:09:14,520 --> 00:09:15,640
Start our application.

113
00:09:17,990 --> 00:09:22,850
Give yourselves a few blank lines, go back here, make sure we have a current version of the calendar

114
00:09:23,210 --> 00:09:26,450
and I'll hide this because we don't need it anymore and I'll just submit it.

115
00:09:27,990 --> 00:09:33,510
And if we go look at our log file, you'll see that form has name M, that's the month, then it has

116
00:09:33,540 --> 00:09:34,220
all of these.

117
00:09:34,230 --> 00:09:39,050
These are every single value that exists in our posted form data.

118
00:09:39,510 --> 00:09:45,300
And you might have noticed, even though there are a total of 62 days in the calendar, the only things

119
00:09:45,300 --> 00:09:51,420
that get posted when they're checkboxes and the same is true of radio buttons is if the checkbox is

120
00:09:51,420 --> 00:09:54,080
checked, it's included in the posted form data.

121
00:09:54,180 --> 00:09:58,270
If the poster is the checkbook is not checked, it doesn't even make it to the post form.

122
00:09:58,620 --> 00:10:04,360
So in this situation, we can look for anything that has a particular prefix and then deal with it.

123
00:10:04,710 --> 00:10:05,970
So this seems to be working.

124
00:10:05,970 --> 00:10:07,510
We're getting our posted form data.

125
00:10:07,980 --> 00:10:10,320
Let's go in here and do some of the necessary logic.

126
00:10:13,110 --> 00:10:20,400
So we're going to check to see if the prefix of a posted form element starts with Earth, the name of

127
00:10:20,400 --> 00:10:25,030
a posted form element starts with Adblock because that's what we named it in our calendar.

128
00:10:25,110 --> 00:10:32,760
If you look over here, we have Adblock as the first part of the name so we can check for that if strings

129
00:10:32,760 --> 00:10:37,890
and that's the strings package built into the standard library, if it has the prefix and we're looking

130
00:10:37,890 --> 00:10:42,390
for the string called name, which is what we're calling it as we saw this.

131
00:10:42,660 --> 00:10:46,020
And we're looking for the prefix ad underscore block.

132
00:10:47,930 --> 00:10:55,850
So if the posted form item starts with if its name starts with Adblock, then this is something we have

133
00:10:55,850 --> 00:10:56,400
to deal with.

134
00:10:56,810 --> 00:11:01,120
So what we need to figure out from this is what are the various elements of the name?

135
00:11:01,130 --> 00:11:03,950
So let's go back to our calendar and just see what we're looking at here.

136
00:11:03,950 --> 00:11:05,300
So I'll inspect this one.

137
00:11:08,430 --> 00:11:13,740
And it actually has the name in this format, Adblock, which is the prefix we're looking for and that

138
00:11:13,740 --> 00:11:18,460
uses, underscores two separate things, then the remedy, then the date in question.

139
00:11:18,780 --> 00:11:25,950
So we need to split that string on underscores and we can do that really easily exploded, which is

140
00:11:25,950 --> 00:11:28,020
just a variable name I'm choosing because I'm exploding.

141
00:11:28,020 --> 00:11:35,670
A string is a sign the value of strings dot split and we're going to split the string name and we're

142
00:11:35,670 --> 00:11:41,340
going to split it on the underscore and want that gives us is something that we can actually look at

143
00:11:41,340 --> 00:11:42,630
that tells us right here.

144
00:11:43,260 --> 00:11:50,880
It's spits back a slice of strings and we can address individual elements just by using the index in

145
00:11:50,880 --> 00:11:51,400
the slice.

146
00:11:51,750 --> 00:11:59,450
So in this situation, the room ID and I'm going to ignore the error because I'm going to use this or

147
00:11:59,470 --> 00:12:09,540
convey to try to convert an alphanumeric into an integer is equal to stir conv a2a and I'm going to

148
00:12:09,540 --> 00:12:12,960
grab exploded at index to.

149
00:12:15,720 --> 00:12:21,720
And again, look over at our reservation calendar page, we have zero is the first element, one is

150
00:12:21,720 --> 00:12:26,100
the second element, two is the third element, and that's the one we're grabbing as a remedy.

151
00:12:26,550 --> 00:12:27,630
So that gives me the remedy.

152
00:12:30,150 --> 00:12:31,680
And then what do I want to do next?

153
00:12:31,680 --> 00:12:36,980
I want to insert a new block and I have everything I need to do that at this point.

154
00:12:37,830 --> 00:12:48,810
So what I'll do again is just say log print line would insert block for room ID and then I'll give it

155
00:12:48,810 --> 00:12:49,530
the room ID.

156
00:12:52,350 --> 00:12:55,980
For date and the date will be exploded three.

157
00:13:00,880 --> 00:13:02,140
At Index three.

158
00:13:03,400 --> 00:13:06,130
So let's see if this will work, let's stop our application.

159
00:13:09,780 --> 00:13:16,440
And start it and go back here and make sure we have a current version of the page, and I'm going to

160
00:13:16,440 --> 00:13:22,950
say for the 19th of December 2020, I'm going to add a new block for the generals quarters, which is

161
00:13:22,950 --> 00:13:23,730
remedy one.

162
00:13:23,790 --> 00:13:27,510
Let's see if this works and go back and look at our log file.

163
00:13:28,350 --> 00:13:32,760
And it would say would insert it says would insert block for remedy one, which is correct for December

164
00:13:32,760 --> 00:13:33,330
the 19th.

165
00:13:33,510 --> 00:13:34,020
Perfect.

166
00:13:34,350 --> 00:13:40,490
So all we need to do at this point is to add to database functions, one to insert a block and one to

167
00:13:40,490 --> 00:13:46,710
delete a block and then actually call those functions with the necessary information from this handler.

168
00:13:46,890 --> 00:13:50,640
And that should be done and will take care of that in the next lecture.
