Great.
So now let's move on and configure our booking service to actually come to the point where we have that
find bookings method right here, which is not only returning a imdb.com all the time, but instead
goes into the database and checks for the flight ID, for example.
So here inside of the booking service, we for sure, first of all, start with the TDD approach and
go into the book because our book method is actually right now not doing anything right.
So let's go ahead and do that.
Let's try to book a flight.
So var flight and save it into the database or update it in the database, right?
So just think of like we have a flight booking inside of the database or flight and someone books a
flight, for example.
Then we need to update the values for sure.
So entities dot flights, dot find book DTO dot flight ID.
So first of all, we try to find a flight by a flight ID inside of our flights database so you can see
that flights is not null.
Here an object reference is required and this is why we create a property.
So right down prop and it's of type entities and we also call it entities.
There we go.
Now that error here should disappear.
There we go.
That's nice.
And now the next error is that we don't have a flight ID right here.
So let's go to the book DTO and add it.
There we go, prop int Oh good, let's make it good for sure.
Good flight ID and we set the flight ID property to be the value of the flight id submitted inside of
the constructor.
There we go.
Flight ID.
Okay, so now we are at a green state again.
So what we have now is he goes to the database, goes into the flights table and searches for a flight
which has the actual flight ID of the data we are receiving here.
And then we might have a flight.
Now, next up, we want to book the flight.
So let's take that flight and let's actually book it.
And we submit book DTO dot passenger email.
So we submit the email and the book DTO dot number of seats.
So again, we are now at a red state just in a couple of seconds because he's just trying to set the
values.
Okay, there we go.
So passenger email and number of seats and book DTO, which is prop string passenger email.
And prop int number of seats.
Let's assign the property values passenger email equals to passenger email.
Number of seats equals to number of seats.
There we go.
Very nice.
Now we have the properties that we need.
So let's continue.
After booking the flight, let's simply call entities dot save changes.
That's very important.
If you change the database, add something, update something, remove something and all of that.
You need to save the changes.
Really, this is like in Git.
If you're just making changes, you have to commit them.
Great.
So here we now book a flight.
Let's check what our flight book method is doing.
F12 You can see it adds something to the booking list.
Okay, let's switch back to flight application specifications again.
Now let's take a look at flight bookings.
Instead of always returning that array, let's remove it and let's return from our entities object.
We go to the flights.
So we go to the actual entities flight data, let's say database.
We go to the database flights and we're going to find a specific flight by ID, So flight ID, and if
we get a flight, we want to go on that flight object called the booking list.
And in that booking list, we're going to select a booking.
We just fixed that issue in a second here.
Booking where the booking goes to make sure that you create a variable here.
So not writing in capital letters, write it in lowercase, right?
Because it's a new variable and it's a new booking, a new booking remodel.
And that remodel should contain that booking dot passenger.
Passenger email and that booking dot number of seats.
Okay.
So this is what we have to do.
And you can see that select showing an error.
And as I have said earlier already, this is called Linq.
So language integrated query.
This is how you can, for example, query a database.
And if we want to use that, we for sure have to use using System.linq.
Now we can use select.
So return entity flights.
Let me just explain you one more time.
What exactly is going on here?
But before that, let me just the value because we didn't name it passenger email, we called it email.
All right.
So what happens here in that scenario?
We want to find a booking by a flight ID So we go into our entities.
Let's check what our entities is, right?
Our entities is actually our database set, including flights.
So what we do is we go into our database, into our flights inside of the database.
Then we try to find a specific flight.
If we have that flight, we go into the booking list of that flight and inside of that booking list
we are trying to select or to find a booking with the values of the email and number of seats which
we have found.
And if we have found the flight, we go into the booking list of that flight.
So which bookings are existing for that flight?
And then we just create a new collection with booking remodel data where we include the information
about email and number of seats.
Let's make that an example.
If we have one flight inside of our database with hundreds bookings with 100 bookings, right, then
we will in the end return an array of type booking remodel with 100 values where the values are set
like this.
So because we are like making a cast here, something like that, it's a little bit difficult to explain
if you have no clue about Linq because the booking list is of type booking.
But here we want to return booking remodel and this is why we select all bookings.
And then for each of those bookings we create a new booking remodel and set the values of the booking
for the booking remodel.
Okay, so this is the easiest way to explain it.
I know it's a little bit confusing, but as I already said, if you want to use databases in C-sharp,
you have to learn Linq.
And now let's perform our final test.
So go ahead and open the Test Explorer and rerun that specific test books flights.
And let's see if we can now make it pass.
And it should be because we are now really having some actual algorithms which query the database with
the variables which we are providing.
Okay.
And the error that we are getting right here is saying that we have a null object reference and that's
because we want to use entities, but entities is null.
So instead of the constructor we set entities to be entities.
That's all we have to do.
Let's again run the test.
So open up the test Explorer, Explorer and let's run the test.
Awesome.
And now it's working.
And again, like if such things happen, I don't cut them out because it's really important that you
understand that you can actually use TDD to fix issues, Right?
And that was one of the perfect examples.
Again, great.
So right now our test here is way more trustworthy and it's using an actual implementation which includes
the database that we have just configured.