So now we have reservations showing up on the calendar, as they should, and we also have blocks showing
up as checks and we've set up a handler to handle the form post, but we're not doing any anything with
it right now.
So let's get started handling the situations where someone uncheck a an existing block or were they
add a new block and making sure those changes are saved.
And there's a few steps necessary to make this happen.
We're going to have to add some code to our Postgres database functions to add new blocks and to delete
existing blocks.
And we need to deal with the actual posted form data itself.
So let's get started.
So I'm going to go over to my idea and I'm looking at admin post reservations calendar, which handles
a post a reservation calendar.
And so far all we're doing is passing the form, getting the year in the month and putting a message
in the session and then redirecting.
And now we need to process the blocks.
So in order to make that happen, if you think about it, what we're posting here is something for one
room, something for the next room.
In other words, we're handling all of the rooms.
So clearly we need to get the rooms in our handler and we know how to do that.
So we can just say rooms and potentially an error come from Magdeburg, all rooms.
And that gets us our rooms.
So we'll check for an error.
If error is not equal to nil, then helper's dot server error and the error and return.
Otherwise we can loop through our rooms.
So we go for ignore the index and we'll just call the iteration variable X and we'll arrange through
rooms.
Now what are we going to do as we go through these rooms?
Well, let's write some comments and think the process through before we actually do it.
The first thing I'm going to do is I'm going to grab the block map that we stuck into the session back
in the get handler for this page.
So it will say get the block map.
From the session and as you know, that bloc map contains all the necessary or all the blocks that exist
for a given room at the point where the calendar was displayed.
So that's the data as it was before the user clicks submit on the calendar page.
So then we're going to loop through that map loop through the entire map.
And if we have an entry in the map.
That does not exist in our posted data, and if the restriction ID is greater than zero, then it is
a block we need to remove.
So let's think about this for a minute.
So I'm on this page and I'll make sure it's current data.
So I just refreshed it.
We have this eight, for example, the 8th of December for general quarters.
And if I inspect that.
And actually has a value of 19.
Now, if I uncheck this and then submit the data, then this eight is no longer in the posted form data,
but it still exists in our map because that's a copy of the data, as it was before any changes were
made to the map and in the map, it has a value of greater than zero.
So that is something that has to be removed.
So that's what we're going to try to do here.
How are we going to do that?
Well, first of all, we need to check to see if the form posted form data has that field.
And as you know, we created some time ago this function in the form.
So if I can just get the form, form is equal to forms new and that comes from our post form.
So now I have access to the has function in there so we can actually, first of all, get the session.
Let's do that.
So Campath I'll just call it current map for her map, my app dot session dot get and we need to give
it the context, our context and then we need to get the name formatters.
Print F and my string will be block underscore, map underscore and then an int and my int will be exact
ID.
And once we get that of course because we're pulling it from the session, we need to cast it back to
a map string.
And so now I have my map, so then we go through the entire map, we loop through it, just as I say
in my comment, for name value.
And those are just two variable names I've chosen equals range car map.
And then we need to make sure that the value exists in the map and that the value is greater than zero.
So the entry exists in the map and its value is greater than zero.
And if it if that's the case, we have to remove it.
So we'll just check to see if it exists in the map.
So in this situation, the variable, OK, which I'm about to create in a moment, will be false if
the value is not in the map.
So I can check to see if it's in the map this way.
If Val, which is a variable that will have a value if it exists in the map, OK, is equal to the map
and check for the name, which will be the correctly formatted date.
And OK, so in this situation, OK, will be set to false if the value is not in the map.
And then we once we get past this, if we're in a situation where it exists, only pay attention to
values greater than zero because their actual blocks and that are not in the form post.
In other words, they've been unchecked.
The rest are just placeholders for days without bloks.
So we can check to see if it exists, if Val is greater than zero.
And if not, form DOT has and then we check for the name, format, format, dot, sprint f remove block,
which is what it would be named that an int, which is the remedy then the date, which is the name
in this situation.
So we do our substitutions for the remedy and name which we're getting by looping through this crap.
And if that's the case, we need to delete the restriction by Edet now, we don't have that written
yet, but we can see if this works just by saying log print line would delete, block and then pass
in the value.
All right.
In theory, this should give us a log entry every time we uncheck something and then submit the form.
So let's stop our application.
Start our application.
Give myself a few blank lines just so I can see things, and I'll go back here, reload the reservation
calendar, uncheck the 12th four generals quarters and save changes and then look at our log file would
delete block twenty.
That's exactly what I wanted.
I uncheck the 12th and if I inspect that, that should have a value of 20.
And it does so that's going to work.
All right, so that's the first step.
And we'll write the code to delete the restriction in a moment, but we also have to deal with adding
blocks.
So let's go down here outside of this for Loop now, handle new blocks.
Now, in this situation, it's pretty much the same.
All we're going to do in this case is loop through the entire post form and look for any entries that
have the name, starting with Adblock.
So I will just loop through the entire posted form data for name.
And we don't care what the value is.
We're just checking for the name right now equals or is assigned the value of range and will range through
the entire ARG post form.
So we're going to go through every single entry in the post form.
And just to see how this works, let's just log it.
Log print line form has a name and name just to see what we're going to get.
So let's stop our application.
Start our application.
Give yourselves a few blank lines, go back here, make sure we have a current version of the calendar
and I'll hide this because we don't need it anymore and I'll just submit it.
And if we go look at our log file, you'll see that form has name M, that's the month, then it has
all of these.
These are every single value that exists in our posted form data.
And you might have noticed, even though there are a total of 62 days in the calendar, the only things
that get posted when they're checkboxes and the same is true of radio buttons is if the checkbox is
checked, it's included in the posted form data.
If the poster is the checkbook is not checked, it doesn't even make it to the post form.
So in this situation, we can look for anything that has a particular prefix and then deal with it.
So this seems to be working.
We're getting our posted form data.
Let's go in here and do some of the necessary logic.
So we're going to check to see if the prefix of a posted form element starts with Earth, the name of
a posted form element starts with Adblock because that's what we named it in our calendar.
If you look over here, we have Adblock as the first part of the name so we can check for that if strings
and that's the strings package built into the standard library, if it has the prefix and we're looking
for the string called name, which is what we're calling it as we saw this.
And we're looking for the prefix ad underscore block.
So if the posted form item starts with if its name starts with Adblock, then this is something we have
to deal with.
So what we need to figure out from this is what are the various elements of the name?
So let's go back to our calendar and just see what we're looking at here.
So I'll inspect this one.
And it actually has the name in this format, Adblock, which is the prefix we're looking for and that
uses, underscores two separate things, then the remedy, then the date in question.
So we need to split that string on underscores and we can do that really easily exploded, which is
just a variable name I'm choosing because I'm exploding.
A string is a sign the value of strings dot split and we're going to split the string name and we're
going to split it on the underscore and want that gives us is something that we can actually look at
that tells us right here.
It's spits back a slice of strings and we can address individual elements just by using the index in
the slice.
So in this situation, the room ID and I'm going to ignore the error because I'm going to use this or
convey to try to convert an alphanumeric into an integer is equal to stir conv a2a and I'm going to
grab exploded at index to.
And again, look over at our reservation calendar page, we have zero is the first element, one is
the second element, two is the third element, and that's the one we're grabbing as a remedy.
So that gives me the remedy.
And then what do I want to do next?
I want to insert a new block and I have everything I need to do that at this point.
So what I'll do again is just say log print line would insert block for room ID and then I'll give it
the room ID.
For date and the date will be exploded three.
At Index three.
So let's see if this will work, let's stop our application.
And start it and go back here and make sure we have a current version of the page, and I'm going to
say for the 19th of December 2020, I'm going to add a new block for the generals quarters, which is
remedy one.
Let's see if this works and go back and look at our log file.
And it would say would insert it says would insert block for remedy one, which is correct for December
the 19th.
Perfect.
So all we need to do at this point is to add to database functions, one to insert a block and one to
delete a block and then actually call those functions with the necessary information from this handler.
And that should be done and will take care of that in the next lecture.