So the last thing we have to do is to actually write the database handlers to insert or delete a block,
so the delete block is right here in my handler's dacko and the insert block would be right here.
So let's go create those database functions.
So I'm over in PostgreSQL and I'll create a new function and I'll call the first one with a receiver
of type postgrads DB repo.
I'll call it insert block.
For room, and it will take two parameters and ID, which is the remedy of type INT and it will take
the start date, which is of type time, time, and it will return nothing more than an error or nil
if there is no error.
So as I always do, I'll copy the context bit so I don't have to rewrite that pasted in here and write
McQueary.
And McQueary is quite straightforward.
It is insert into room restrictions and what are we going to insert?
We are going to insert the start date, the end date, which will always be one day long, the start
date to end date because it's a block the remedy, the restriction ID, which will be to in this case
and.
The crater that created an updated it created a.
Updated at values, and these will just be placeholders and there are six of them, I think, dollar
and one dollar sign, two dollar sign, three dollar sign for dollar sign five and dollar signs six.
So that's our query.
Now let's execute our query.
And I don't care about the first result, but I do care about the error.
That's a sign the value of IMDB and exact context handled the context, handed the query and now handed
our six parameters.
And they are start date and then end date, which is just one day longer than start date.
So I can just say start date and date.
No years, no months.
One day.
The next thing is the remedy, which is just ID.
Then the restriction is which will be two, because that's a block and then just timed out now and timed
out now, check for an error.
If error is not equal to nil, a Sloggett log print line error and return the error, otherwise return
null.
So that's our first one.
Let's give it a comment.
Inserts a room restriction and lets save some typing will copy the whole thing, because the delete
is almost the same.
All the changes is the query in the parameters.
The parameters will just be an ID, which is the restriction ID.
I'll name the function, delete block by ID and change my comment and say deletes a room restriction.
And let's change our query to.
Delete from room restrictions where ID equals dollar, sign one and all we need to pass in here is one
parameter, which is our ID, and that should do it.
So let's copy both of these and put them in our test repo so we'll be actually able to write tests,
make them absolutely simple, just have returned nil for both of them and change the receiver to test
DB repo.
And the last step for this part is to copy the function entirely, put it in our repository and do the
same thing for the other one.
Let's make sure it compiles.
It does good sound.
Let's go update our handlers, so find our handlers and here we instead of the comment.
Let's actually do the deletion, so error is a the value of IMDB repo or not repo and what am I doing
here?
Delete block by ID and the ID I am going to pass in there is value.
So that should work and check for an error if error is not equal to nil.
And what I'll do here is just log the error.
OK.
And down here, instead of inserting or instead of logging, let's do the actual insertion.
So that would be error is a sign the value of IMDB dot insert block forum and we need the room ID,
which is room ID, and we also need to have the start date.
Well, I don't have that yet, but I can get it easily enough just by using the name of the that, using
the name and passing that into a time.
So time and I'll ignore the error is a sign the value of time pass and the layout would be 2006 zero
one two for a four digit year, a two digit month and a one or two digit day.
And what I'm passing is exploded three, OK, and now I can put the time in there and check for an error.
So I'll copy this pasted in here.
It's not going to work.
Type it out.
And if error is not equal to nil, we'll just log the error log print line error.
OK, so let's try starting our application and see what we got wrong.
So far, so good.
Let's go to the calendar.
OK, I'll make sure this is absolutely current, let's try deleting the one in the 12th, OK, that
looks like it worked.
No errors.
Let's try adding the 12th and 13th.
Good analysts said the 12th, 13th and 14th here.
And let's delete the 13th, so far so good, let's try it on a month, that is not the current month,
so I'll put the 19th, 20th and 21st and the first.
Perfect.
All right, so a reservation calendar appears to be functional at this point, and I don't think there's
a whole lot left to do except I think if I look at an actual reservation and try to market as processed,
it's probably not going to redirect properly.
Let's make sure I think this is one thing we overlooked.
And it says Page not found, it tries to take us through reservations, Carol, so we need to make one
more change and that is just doing the redirect when you make a reservation as processed after getting
there from the calendar.
So that's the last step and will take care of that in the next lecture.