So now we have to database functions that allow us to check for availability forums.
We have one that allows us to search for all rooms, and we have one that allows us to search for rooms
by I.D., which is right up here.
So let's think about what we want to do.
First of all, let's make sure our application is running.
And I do that by bringing in my terminal.
It's not.
So let's run it and then we fire up our favorite Web browser, whatever that may be.
I'll use edged this time.
I haven't used any edge in a while and I will go to localhost Eighty-eight.
And here we are, so there are two places where we can check for availability, and the first one is
at the bottom of this screen, make reservation.
Now, that takes us to this search screen search for availability.
And here I choose a start and an end or an arrival and a departure, and that's at the universal search
dash availability.
So let's go back to our code and see if we have a route set up for that.
And it looks like we do.
We have post availability.
So let's look at that right now.
All this does is grab the start from the forum post and grab the end from the forum post and store them
here.
And those, of course, are string's.
Anything you grab from a forum post will be in the form of a string and then it just writes it to the
browser window.
That's not terribly helpful.
What I want to do is I want to call the database function that searches for availability for all rooms.
And if you look at that, that requires start and end in the form of time dot time.
So I need to take these two variables, start an end and convert them from strings to time dot time.
Well, we did that once already.
Let's just look for the word layout, because I know that's part of it.
Yes.
Right here.
This will actually take two strings.
I only need these these ones actually, this will take our strings of dates are dates that are in the
form of string and convert them into times.
OK, so let's go back and find our post availability again, wherever that is post availability.
Here it is.
Now, let's just paste that code in there.
And I needed to make one change because I'm not calling it SD and I'm calling it start and I'm calling
it end.
OK, so now I have my two dates in time.
Time format so I can actually call my function that allows me to search for availability for all rooms.
Let's see what that returns.
So it takes a start, a time to start in and as time, time.
And it returns a slice of model's room and potentially an error.
OK, so let's get that.
We'll call our variable.
We're going to store the slice in Rum's, which makes sense because it's a slice of rooms and er will
be our as usual.
That's going to be equal to m dot db m dot db dot search availability for all rooms and we'll pass at
our start date and our end date and then we'll check for an error.
So let's check for an error.
If error is not equal to nil then I'll just go helper's dot server error w and the error and we'll return.
Now let's see what's in that room's variable, so I'll just print it out to the screen for right now
to the terminal.
So I will say for I don't care about the index I is equal to range rooms and I'll just say app dot info
log sorry m dot app dot info log dot print line and we'll say room.
And I thought I'd I'd try that again, I thought I'd, I thought, remain OK and everything else just
happened the way that it does.
So this will see if we're actually getting useful information from our database.
So let's stop this.
Start this again.
Go back to this page Reloaded, and before I do this, there's one thing I need to make sure I need
to be able to search for dates where there is availability and let's make sure what we have in our database
right now.
So we go back to our database and look in post Toco and there it is, room restrictions.
Make sure it's the most recent version.
It is.
So there's only one restriction for February the 1st, 2021 to February the 4th, 2021.
So as long as I search outside of those date ranges, I'll get at least one value.
If I search in that date range, I will get only one value so I can pretty much choose any dates I want.
All right.
So let's choose some dates here and here and see what that gives us is going to print the start and
end date to the screen.
Yes.
And on my terminal where I started it up, it says there are two rooms available that is perfect.
So it looks like when there is availability, the actual rooms variable will have at least one entry.
When there is no availability, there will be nothing in that slice.
It'll be an empty slice.
So I can actually just check the length of the slice.
If Len and the slices named rooms is equal to zero, then no availability.
Otherwise there is availability.
So it does handle this case.
First, I'm going to go back to my database and I'm going to enter another restriction here for Remedy
two, because I only have two rooms.
I'd one in 92 and I'll enter it with exactly the same start and end a 20 21 02 01 to twenty twenty one
zero two zero four to get them exactly the same room it will be to because I want both rooms to be blocked
off.
I'll leave the reservation id empty because there is no reservation to put here so it'll be stored as
no.
The restriction idea will be two which is an owner closing and I'll just put any date in here.
Twenty, twenty, eleven, eighteen, twenty, twenty eleven eighteen.
And save this.
All right.
So now there should be no availability provided I choose dates that are either equal to or inside this
date range, and that's February the 1st to February the 4th of 2021.
So let me go back to my browser, go back a screen, reload this to make sure it's current and I will
choose some availability that's in that date range.
So I will choose.
That's odd, shouldn't be doing that autofill there, but I know it doesn't matter, I will choose.
I need to get rid of that.
Go there.
I need to choose January February.
So I'll choose February the 2nd, 2021 to February the 3rd, 2021, and search and see what I get.
So there's my dates and down here in my terminal, if I bring that back up.
I don't know if that's the last time or this time, let's find out, General, one major general.
So that's 12, 22.
I guess I didn't do it.
OK, so.
That's exactly right, because there is no available to be printed out, therefore there's no availability.
Let's make sure I'll just say emerg app dot info log dot print line, no availability.
So I'm going to start my application just because I want to make sure this is right.
Started up again.
Clear the screen.
Start the application.
All right, so there's nothing being printed in the terminal right now.
Let's go back again and go back one screen and just resubmit this form.
So I want to get rid of that.
Go back to February the 2nd to February the 3rd.
OK, search.
And now I should have the no availability message.
That's exactly what I wanted.
So that's working.
All right.
Now, what do I want to have happen when I'm on this screen here and I search for availability and there
is no availability?
I certainly don't want just to start and end date printed to the browser window.
That's not helpful at all.
What I actually want to have happen is I want the user to be taken back to this screen with an error
message, and we can do that.
We already know how to do that, in fact, so I can get rid of this because it's just printing to the
terminal.
That's not helpful.
And I don't want to just print that.
Instead, I want to generate an error message, so I'll store that in the session Emden app session
dot put and it requires the context which I can get from ARG context.
And it requires a key, and when we want an error message to be displayed, we call that error and then
we want the value, just the message that's displayed.
So I'll just put no availability.
So I saw that in the session.
Now I need to redirect them, export redirect and it requires w are you url and Chote.
So W are the you are always search.
Available Bill Bill T and my code has to be in the 300 range HDP status, see whether that's in the
300 range.
And then, of course, I want to return.
I want everything else to stop at this point.
So let's see if that works, so I'll stop my application.
Clear the screen, run my application.
It runs go back here, reload the screen to make sure it's current, and I'll choose once again February
the 2nd to February the 3rd.
And hopefully when I click this, it will take me back to the screen with an error message, and it
does perfect.
But if I choose dates that are available and it doesn't matter what they are, it should just print
the start date an ending to the screen and it does.
OK, so that works.
So obviously, if there is availability, I don't want to be just displaying the screen.
I want to take them to a page where they say where it says to them, hey, these rooms are available
for your chosen dates.
So to do that, I need to have another page.
So I'll go copy the contents of the about page and I will open the templates or create a new file in
the templates folder, which I will call Choose Room Page Template.
And I will add that to get and I'll just paste in the about page and see what I want to do here.
What I'm going to do is change the title to choose a room and then I want to range through whatever
rooms are available.
So whatever I got for this variable rooms in my handler, I want to pass that to a template and display
those.
So let's do the template first, then we'll pass the data there.
So I'm going to assume I'm going to use that data a member of my template data.
So if you look at template data, it has this data field, remember, and that can hold whatever we
want to put into that.
So I'm going to assume that I'm going to get a value, a variable that I'll call rooms and I will pull
that from the index of data, which is passed to every single template, and we'll have to populate
it.
And I'll call it rooms.
And then I'll just for the sake of of making sure it all works, I'll arrange through rooms and that
requires an end and I'll just print the room name for now.
So.
Room name, and then I'll put a blank line or a line break after that.
OK, so the template is ready.
Let's go back to our handlers and how are we going to pass that data there?
Well, I know that if this fails, if I have no availability, I'm being taken back so I can just do
it below the if statement.
So let's create a variable called data.
That's going to be a map.
Make a map of string interface.
Which is what that data format or data variable has to be in template data, and then I'll store my
rooms in their data rooms, plural, because that's what I'm calling it in the template is equal to
rooms.
And now I need to render that template and past data.
I've done that many times.
I'll just find one where we do it right here.
We'll copy this and come back up to my line up here.
Get rid of this because we're not writing to the browser window anymore.
Just text.
Instead, we're going to render the template we just created, which I will call, which I called Choose
Room Page.
Let's make sure we've got this right so we create a data variable.
That's a map of string interface.
We store the rooms in that map and then we passed the data to the template choose room.
And the template is in fact called Choose Room.
This should work.
So start the application.
Start the application.
We go back to our browser, back on screen, search for availability for any two dates that are any
dates, as long as they're not the ones that are blocked.
This should work fine search.
And there it lists the rooms.
OK, we don't want to just list the rooms on this template.
We actually want to make those you URLs so I can pick on pick my room and I might put a picture next
to it or something like that.
But I'm not going to.
It's something you can do on your own if you wish.
What I can do instead is actually just make these links.
So I click on them and then it takes me to the reservation screen.
But what information do I need on that reservation screen?
Well, let's look at it.
I'll open a new tab and go to localhost 1880 Make Dasch reservation.
So this is a form.
And if you recall, we gave it the start date and the end date just as temporary fields.
And that's not helpful.
I need to actually store or have the start date and end date not editable on the screen, because I
don't want the the user to say, oh, I, I want a different date and just arbitrarily change it because
of my reservation.
When I'm making it, I don't check availability at all.
I'm assuming that's done before I ever get to this screen.
So I don't want these to be displayed here.
Maybe I'll put them at the top reservation details arrival and departure and the room they're staying
in.
So this is going to have to change.
More importantly, I need to store that start date and date and remedy somewhere before I actually display
this page.
And we can just do that quite easily by putting it in session.
So let's go back here and let's store some information in the session before we render the template.
So I want to put the start date and the end date, and I want to put a time dot time in there.
So can I just store a time dot time in my session?
Chances are I can't.
But what I can do if I without actually declaring it, if I go back to my main function, I am in fact
storing a reservation model type as something I can put in the session.
So maybe it makes more sense to create an empty reservation variable, populate the start date and end
date and remedy in that, and store that reservation model in the session.
Let's do that.
So back to my handlers.
I will create a new reservation.
I'll call it Rez and that's a sign the Value of Models Reservation.
And I will say Phil Oldfield's, which is built into goaland.
If not, you're going to if it's not in the one you're using, I have to do it manually.
I don't care about the ID.
I don't have one of those yet.
I don't care about the first name or last name or email at all.
I do care about these three things.
Those are the only ones I care about, the start date, the end date and the remedy, and I already
have all of that information stored somewhere.
So what I can do here is store the start date and end date and then leave the room it empty and then
populate that when they click on it.
But I need to store the start date and end date so I can even get rid of this right now.
This will be a start date.
This will be the end date.
And I'll just stick that reservation right in the session.
So I will say m dot app dot session dot put our context and I'll call this reservation and I will put
in the value.
Now that is stored in the session.
So I have that available, that information available to me.
OK, so when I render this page then I know that I've already stored the necessary information in the
session.
I have to start that mandate.
That's all I care about.
So let's make sure everything compiles.
Run this.
Go back to our browser, which was EDG, I believe, yes, it was.
OK, let's start right from the beginning.
Search for some dates where I know I'm going to get some availability.
So the 18th to the 19th search and there they are.
So at this point, I now have the start date and the end date saved.
I can use that information if I need to, and I'm going to need to because I may need to make this clickable.
So what I want to have happen next is when I click on a link here, it pulls my my reservation with
the start date and end date out of the session and then assigns the idea of the room that I just clicked
on to that reservation variable, puts it back in the session, updated with the new information and
then display the make reservation screen.
All right.
We'll do that in the next lecture.
We're getting close to being able to actually book a room now.