So what we want to do this time is we want to modify this function, insert reservation to return not
just the error, but also the ID of the newly inserted record.
And I've already added the returning I.D. skills necessary into the query.
So the first thing I'll do is modify this to say it returns not just an error, but an int and an error.
And then I'll make this return zero, because if there's an error, we don't really care what the ideas.
But here I'll return a variable called New ID, which doesn't exist.
So let's go declare it new ID.
Sorry var new ID is equal isn't it.
So that creates.
Now I want to actually change it from an executive which doesn't insert.
I'm going to change this to a query and I'm going to change the query because actually what we're doing
is returning something from our sequel.
So it is, it's an, it's an insert statement but it's also a query.
So the method that does that returns only an error.
And it's not called exact context.
It's called query row context.
And the only thing I have to do is because I'm actually doing a query row is I now have available the
scan function and I'll scan the one thing being returned, which is the ID into my variable new ID and
that's all I have to do here.
Now I obviously, because I've changed the syntax of this function in postgrads, don't go.
I'll copy the whole thing, go back to my repository and replace the old version with the new version.
Perfect.
So that means my handler is going to change too.
So here I'm no longer getting one value back.
I'm actually getting a new reservation ID and an error potentially there.
So I'm not using that yet, but I will in a moment.
So what I'm going to do with this is I'm going to use this new reservation ID, along with all the other
required information to build up a room restriction model and send that to a functional create in a
moment, which I'll probably call insert room restriction.
So let's build up model first.
We can build the model just by saying restriction or color restriction is going to be assigned the value
of models, not room restriction.
And I'm going to use a feature built into this idea called Fill All Fields, which just saves me some
typing.
I don't need the idea of the the restriction I'm inserting.
I don't know what it is.
I don't care what it is.
I just want to insert something.
I do have a start date and I have a start date in my variable called start date.
And I do have the end date in a variable called End Date and the room ID, I have that to remind and
reservation id I now have because it's the new reservation ID, new reservation ID, new reservation
ID.
I've got to make this in a sign.
That's why it's an error.
I have new reservation ID so I'll put that in here.
The restriction ID, I will make one and I'll have to fill up my database field before I can actually
use that.
I don't have any restriction IDs yet, so I'll just populate that and make that a reservation restriction
idea of type reservation created an updated out.
I'll let that be taken care of at the database level and I don't need these three because these are
just things I might use in the future, but they're not actually part of the database field.
So I now have this restriction.
So let's go over to our postgrads and create a new function called func.
And it has to be on the receiver and have a receiver of M Postgres and I'll call it insert room restriction.
It will take one argument, which I'll call her, and it's a type models room restriction and all it's
going to return is an error or nil if there's no error and I'll just say return nil just so I can get
this up and running then because I've created this, I actually had to close that because I've created
this.
I need to hopefully if I can stop clicking the wrong buttons, copy this whole thing, fixed that.
So it says postgrads DB repo, copy this whole thing.
And put it in my repository.
OK, and now we go back to our handlers and we can actually call that method on our database repository
and it's just as simple as error is assigned.
The value of M dot db dot insert room restriction and pass it are restriction.
And if there's an error.
Paste it in here and you may notice that I accidentally left out the return function in my error check
and I added that before I started this lecture.
So I added it here and I added it here and I added it here.
And if I didn't do that, things would still work, but it wouldn't stop execution at this point.
And I do want things to stop at this point.
I don't want it to say you can't pass the date and try to do everything else because it'll just fail
when it gets down to the database insert part.
So just out of return, as I did in those three places.
Anyway, I have now created my function, which doesn't do anything yet.
Want to add that and I've called my function.
But before I forget, let's go back over to our database and find our bookings database and find the
restrictions and add one restriction and a restriction of type one, which is what will be added in
for us will be a restriction of type reservation.
And I got to put some dates in there, so I'll put in a date and a date.
OK, and save that there, I have one in there now, so let's go back and finish up our actual routine
to insert this restriction.
So this is going to be a statement.
But first of all, it's copy and paste this context stuff so I don't have to retype it all pasted in
here.
And let's read our statement.
Statement is a sign the value of insert into our database table name is removed restrictions.
And what do we want to insert in there?
Let's go look at our structure again.
It's going to be a start date and date remedy.
The start date and date room ID, which is the lowercase art reservation ID, created an updated restriction
rather patient ID and then we'll go tab over created at, updated at and restriction idy.
So have all this information passed to us in the variable called Earth.
So we're going to execute that query and we're going to get a result that I don't care about.
Potentially an error that I do carry about, care about.
And we'll call our database function m dot db dot exact context.
The first parameter is the context that I create at the top of the function.
The second parameter is my query, which is a statement, and then all of the arguments and I'll put
again all of those on their own line just so they're easier to read.
So I left something out of my query here.
I'm glad that I noticed that we insert that and then we have to close the parentheses and on another
line say values and the values are dollar sign one.
And how many are there?
One, two, three, four, five, six, seven dollar sign.
Two dollar sign.
Three dollar sign for dollar sign, five dollar sign, six dollar sign seven.
And the parentheses are closed.
That looks better.
So we want our dot and what's the first thing.
Start date and then our end date and then our dot remedy and then not reservation ID and then our dot
created out.
Now time now for Created a time dot now for updated at and finally our dot restriction ID.
And the closing comment, and that should do it.
We've got a check for our error, though.
If error is not equal to nil, just return the error.
OK, so let's make sure that we have this.
All of this is in our repository.
It is.
There are no errors being displayed on our postgrads.
There are not there is no comment.
Let's add one.
Inserts a room restriction into the database.
OK, all looks good.
Yes.
OK, and let's check our handlers.
We have that we have a value for restriction idee in the database.
Perfect.
Let's stop our data or stop our application and start it up again.
OK, it's not running, so let's run it.
It's running, let's go back to our Web browser.
I'll refresh the page to make sure that I have a valid CSF token and put some values in here.
John Doe start date 2020 zero zero two zero one to 2020 to the past.
I want to do that 2021 02 01 to 2021.
020 for email is John.
At Dotcom and his phone number is five, six seven six seven eight zero nine eight seven.
OK, let's hope that works.
And it did.
Now, let's go look at our database, which is right here because I'm using postal code.
There should be another reservation.
That's a reservation.
That's our reservations.
Restrictions.
Those are fine.
There should now be.
Hey, look at that.
I have to because I did this and before this lecture started and forgot to delete it.
Sorry.
So ignore that one.
But I do have this one, which is the one that I just added.
Perfect.
And let's look under some restrictions and we have the room restriction entered and it's from 2001 to
2004.
Remedy one reservation IDs three and the restriction ID is one and that's perfect.
So now we have a means of inserting not just a reservation, but also the restrictions for that room,
for the dates of the reservation we just saved.
So we're making some progress.
We'll move on in the next lecture and do some more work with the database.