So this time around, we want to do a couple of things, we want to change this numeric representation
of the month to a textual representation of the month, and I want to get started building the necessary
rows and they'll actually be tables and they'll be tables that are two rows, each one for the indicator
for the current day of the month and one for a checkbox underneath it that will allow me to block a
date or unblock a date or if there's already a reservation to display the reservation information for
those particular dates.
And they will have one column for each of the days of the month.
Let's do this part first where we're changing this one to two in my case in December.
So let's go back to our our code and think about how we might accomplish this in my handler.
Where's my handler?
They're they're OK in my handler.
I know that now will always be set to at least the current month.
It'll actually be set to the first day of the month that's being displayed.
So what I could do is pass that value to my template using my data map.
So let's create a data map here and I'll create it right here and I'll call it data is a sign the value
of make a map string interface.
And I'll put inside data with the label or the index look up of now the value of now and then down here
where I actually render the template, I can pass that data.
OK, so when I restart my application, I know that that will be available at the template, that's
not terribly helpful yet because if I put data that put the value of now from data here, it's going
to put the entire date with the time and all sorts of things that I don't actually want.
What I want is some means of formatting now from the data map so that it shows the textual representation
of the month here and the four digit year here.
So what I probably should do is go to my render package and build a new function and make that available
to my template.
And I'm going to do that.
It's very similar to human date.
I could just say func and I'll call this format date.
It will take two arguments t, which is of type timewarp time and F for format, which is just a string
and that will be the format string and it will return a string.
And all I have to do is say return t dot format with the format string, whatever.
I pass it now I'll take this and put it in my function map.
So it's available to my template and I'll call it format date with a lowercase F.
All right, now let's restart the application.
Stop it, start it, make sure it compiles.
And it does now, if I go back to my.
Calendar page right here at the very top of the content section.
I'll pull that value now out of my data map, which I called now.
That's how I look it up.
And instead of doing this index string that this month, I can just say instead.
Format date now, and I want to show the month of full representation of the month and according to
the format string that Goh uses, he just used January and the same thing here.
Instead of doing it that way, I can say format date now and now.
I want the four digit representation of the year, which is 2006 according to that format stream.
So if I go back here and reload this and if we did everything right, he will show December 2020 and
if I go back November, go forward.
Perfect.
So that's much nicer now.
Now I want to start showing one table or one line that consists of a table for each of the rooms to
allow me to see the reservations and to block and unblock dates as I wish.
And this is going to take a few steps.
But let's get started.
What's the very first thing I know?
I'm going to need to know the number of days in a month.
I'm going to need to know that.
And I'm also going to have to have the rooms so I can show one calendar for each room.
So let's get started back to my handlers over here.
And we are in this handler, yes, this is the correct one admin reservations calendar, so we already
have our data map and we have a string map.
We're also going to need to pass an integer to it.
So we may as well figure out how to get the number of days in a month and then put that in my map.
So this is not quite as easy as you might think.
What I want to do here is get the first and last days of the month, OK?
And I'm going to do that by using a method that's built into the time package or a function called date.
So I will.
And that returns three things.
And I'm going to get the current year and the current month and I'm going to ignore the current day
and I'm going to get that from now using date.
And if you look at what date does what it does, it returns the year, the month and the day, as if
that's all that it does.
So that gives me the current year in the current month, which is great.
Now, I need to make sure I get the current location.
So current location because different locations use different rules about Daylight Savings Time, so
on and so forth.
So what I'm going to do is just say that that's equal to now, location, whatever the current location
is for now.
And I think we set that to time, that UTC, which is just fine for our purposes in this course, if
you need to a specific location, then you would have to, of course, set that up yourself.
But right now, UTC Universal Time will work just perfectly.
Now I need to get the first of the month.
First of month will be a variable that's going to be assigned the value of time dot date and it will
be current year, which I just got.
Current month, which I just got the first day of the month, then hours or days, hours, minutes,
seconds, nanoseconds, which I'll leave at zero for all of them and my current location.
So I just set first of month to whatever the current year is, whatever the current month is, and whatever
the current day is.
Now, I could have used now, which has pretty much the same thing, but I'm just showing you how this
works and this makes it very much a much more readable.
So that's the first of the month.
The last of the month will be assigned the value of 1st of month.
And I'm going to add a date and I'm going to add New Years one month and take one day off, which will
get me to the last day of the current month, which is exactly what I want.
Now.
I want to store that that date, which will be the number of days in the current month.
I want to store that in an intermap and pass that to my template.
So let's create an app map, make a map, string it.
And in that intermap, let's store using this as our lookup days in month.
That's going to be equal to last of month and call in the built in function day, which returns in it.
So if there's 31 days in the month, then days and month in it, Matt will have the value of 31.
If there's twenty eight, it'll have twenty eight.
And then of course we need to pass our intermap
and that will be equal to EMAP.
So we have that.
Those are the days of the month.
We also need to get the rooms and we know how to do that.
That's pretty easy.
So let's just look up our rooms.
Rooms.
Error is a sign the value of M DB all rooms.
We don't have an all rooms function yet.
Well, let's go make one that's pretty easy.
I thought we had an all rooms function, but apparently we don't.
So let's go to postgrads and let's create a funk m postgrads debe repoll and we'll call it Ballroom's
and it takes no parameters and it returns a slice of model's room and potentially an error.
OK, and let's get a context and copy and paste that to save some typing so we need a variable to hold
our result.
So we'll create a ver rum's which will be of type slice of models doldrum and let's write a query.
Query is a sign the value of select Edek room name created at.
There's not much information in a room.
So this is really short and updated at from rooms order by room name.
Yep, that looks right, and I will just quickly get our query and execute it so Rose and potentially
an error as a sign the value of MGB query context.
Of the context of our query, check for an error, if error is not equal to nil return rooms, which
will be empty and the error.
Otherwise, for a deferral close, they want to forget that defer rose close
for rows, next, we loop through whatever we get back from the database.
We create a new variable of our R.M., which will be of type models room and I.
We scan our values into the variable.
We just created rows dot scan and will it will be our emerg ID and then I'll just duplicate that three
more times and change it to remain created and updated at.
Check for our error if error is not equal to nil return rooms, error otherwise appended to the slice.
So rooms equals append rooms room.
And then finally, if we have an error and it's if our error is equal to Rosewarne error.
And air is not equal to nil, then return rooms and air, otherwise return rooms and nil.
OK, so that's created.
Let's copy the whole thing and put it in our test tube repo copy and test repo.
And I'll just paste it here, give it the correct receiver.
Save the variable, the declaration, and delete everything except for the return, which will allow
us to compile this, and then we'll need to add this to our repository.
So let's copy this, open the repository and pasted in here all rooms.
OK, so let's go back to our handlers.
And now we should be able to use that function and we can.
So we've got our rooms and we check for an error if error is not equal to nil.
And I'll just go helper's server error w an error and return.
So now we have our rooms.
Let's go.
Data rooms equals rooms.
So now we've got our rooms past to that and we have our number of days in the month, so we're actually
at the point, I think, where we can start to generate our tables, but we have one problem.
So let's go back and look at this and think about what we want here, what we want on this page right
away.
I want Generals' quarters, then I want to have a row the a table that has two rows, one with the the
top level being the days of the month from one to the last day of the month.
And then underneath I just right now I want to put an empty checkbox and I worry about populating that
with checks when we have a block for a date or populating when we have reservations, whatever.
Right now I just want to get it showing up.
How am I going to do that?
Because as far as I know, there is no functionality built into the go templating system that allows
you to do a simple for loop.
So for one two number of days in month, do the following.
That doesn't exist.
And it seems like a glaring omission.
And I think one of the reasons that they left it out is it's so easy to do it yourself.
So let's go back to our render package and create a new function render package.
I need to build a function that allows me to iterate between two dates.
So let's find our render rendered Dutko, OK?
And I'm going to create a new function right here and I will call this func iterate.
And it will take one parameter, which I'm going to call count, and it's just an ant and it returns
a slice of ants, and because I'm returning a slice, that's something I can iterate over.
And it's really, really easy to do this.
So let's create a variable variety of type int and var items, which is another variable.
This will be a slice of it.
And all I have to do now is write the for loop that I wish I could write on my go template for I equals
zero.
I is less than count.
I plus, plus.
And then items equals append items, hi, and I just return that return items, give this a comment,
iterate returns a slice of INTs starting at one, going to count and count as the variable that I've
posted here.
So if I pass it 10, I'll get a slice of INTs starting at 1:00 and going to ten.
And then of course I need to add this to my funk map so I have access to it.
So I'll create a new function.
I'll call iterate, that's what I'll call it, the goal line template.
And it's just iterate.
And this needs a comma, of course, and I should have access to it now.
So if I go back to my admin reservations calendar page, then I can actually start to build my, my,
my tables the way I want them to look.
And of course, I want them to come after the clear fix.
It's going to be down here.
And I just want to, first of all, go through my rooms.
So let's make it easier to write this by getting our rooms variable out of the data map.
And I called it rooms.
So now I have a variable that holds my rooms.
So down here after the clear fix, I should be able to say Range Rum's.
And put it in there.
OK, so let's just make sure this works to start with.
OK, so I will just put DOT Room name and let's stop the application.
We're not using our iterate function yet.
We're just making sure that everything compiles and then we have data when we think we should have data.
So I should see the rooms here, one name after another, and I do OK, so I have access to that, which
is great.
So I don't want to actually display the name here yet like that.
I want to do it this way.
Let's grab some information I know I'm going to need I'm going to need at some point inside a loop a
remedy.
So I'll save room ID is assigned the value of ID and that's from each individual room.
So now if I need it, I have access to the remedy.
And let's put in our room name as an H four, which isn't too big, but is big enough to be obviously
a title.
OK, and underneath that I actually want to have a table and this will be a table that needs to be visible
in every device.
So I'm going to make it a mobile friendly table by putting this in a div div class equals table responsive.
And this is actually built into bootstrap and it allows you to scroll to the left in the right if the
table is too big for the current viewport.
So if you're looking at this on a phone, you can just drag the whole table to the left and to the right,
and that will be very helpful.
Now I actually put the table inside of that div table class equals table table.
I'm going to make it a table bordered so it has nice lines around all the cells.
And again, that's built into bootstrap and I'll make it a small table, make it as compact as I can.
OK, so there's my table inside of that.
I'm not going to have a ticket because I don't want a had my header is actually going to be a row that
is going to have in each cell for that first row the number the current day of the month.
So starts one, then two, then three right up to thirty one for December for example.
So I'll put a TR in there, but I want that TR to be slightly different than the rest of the table,
which is only one more row, and I'm going to give it the class of table dark, which will give it a
nice dark background.
And inside of that I have to range over my days in the month.
So what do we call days in the month?
Let's go back to our handler.
I think I called it days and months.
So let's find our handlers right here.
And where did I put it?
Yes, I called it days and month and I put that days and month is in my intermap.
So let's go back to our calendar page and let's just pull that out right up here.
And I'll call this dim for days in month is a sign the value of index intermap days in month.
So now I have this all stored in a variable.
So in February of anon leap here, this demand will be equal to twenty eight for every other year.
If it's November, it'll be equal to thirty.
And here's where I can range over the.
And OK, so what's going to be in there?
Well, let's try this, let's try TRD because, you know, it's going to be at a table cell, which
is TRD and I'll centera just to make it look nice.
Class equals text center.
And I'll I put in here is well, what am I going to put in here?
It'll be the current piece of data.
Let's see how that works.
Let's go back and reload this page and see what happens.
And it crashed.
What did it crash with.
Exciting content at demn range.
Can't iterate over thirty one.
Right.
That's what I forgot to do.
So we have that demn we actually need to not iterate over dim instead.
We want to iterate over.
Let me see here.
It Radim, we want to pass it 31 to that function we just wrote and we want to store that value in index
and then from here I'm going to show the index.
So let's start our application.
Because it crashed and go back here and try this again, it looks almost right, except the last time
I checked, there is no zero at the beginning of the month.
There's no zeroth of December.
So what I need to do is to add one value to whatever I get.
OK, that's a good start.
How am I going to do that?
Well, let's go back and find our functions here under repository, under rendered.
And again, this is something you would think would be built into the default package or the standard
library for go templates.
But it's not.
And it's really easy to write.
So we'll create a function here.
Called ad and ad will take to it's A and B of type it and it will return an end and all I have to do
is return A plus B and then add that up here.
OK, so that will give us the ability to add something to it, to add to it together.
So let's go back to our calendar page and see what we're going to do here.
We're going to find that index instead of just returning.
Index will return and index and one which should add one to every day of the month.
OK, the other thing I noticed when I look at this, so these are awfully close together.
So let's put some spacing above the title for each room since we're here anyway, let's do it right
now.
Each four would go class margin top say four.
See what that looks like.
So let's stop our application, start our application.
So don't be aware of that ad function we just put in there and reload this and that looks better.
So there are 31 days in January.
Let's go back to November.
Our December December has 31 and 30 for November, 31 for October.
Perfect.
OK, so that part works.
Now let's go and do the same thing for the second row of our table.
So here is our second row and our second row is going to be another TD.
And inside of that four right now, I'll give it again in the class of Texaco's center class equals
text center.
Let's just put a checkbox in there.
Input type equals checkbox.
And that's all I'm going to do right now just to make sure this looks right.
And it doesn't because I forgot to change over it once again, range index.
And close that range.
Save that, reload it not quite right, but I do wrong.
Oh, the Rangers outside the TDs having a bad day.
I think I need to make a coffee and I'll do that before I start the next lecture.
All right, let's try this one more time, and now I should see a checkbox under every day of the month
that looks better.
So the next step will be to wrap this entire thing in a form tag and create the handler to handle a
post from this your you admin reservations dash calendar.
And when I check different dates, I want that to turn blocks on.
If a date is checked, nobody can book make a reservation for those particular dates.
And if I want to get rid of a block, all I would do is uncheck them and click save.
So I have to create a save button too and we'll take care of that in the next two or three lectures
because there's quite a few steps to make that happen.