Do not allow a user to reserve a car twice to the same period of time

Hello,


I wanna add a validation in order to not allow a user reserve more than one car for the same period of time, for example: 

- 1 reserve: from day 5 starting 5 pm to day 6 ending 5 pm 

and if he tries to add another reservation between this period show a raise exception saying that is not allowed. 

For this i also have a static entity with 6 records: Rejected, Pending, Approved, OnGoing, Closed and Canceled. The validation must have the comparison of the status Approved or OnGoing because the user only can´t do a second reservation for the same period of time if he already have one of this Status (Approved or OnGoing). 

What is missing is the condition of the period of time. 


If anyone can help me, i would be very grateful. 

Hi Pedro Santos,


If you are using a switch statement, try to use your condictions in different places, example :

Condition1 ReservationID = status.Approved 

Condition2 ReservationID = status.OnGoing

Otherwise ReservationID = your other status, 

this way you are going to have more control about what you want to receive or what flow are you going to follow.


Regards.

Solution

Time periods are always fun to analyze.

I assume that the new reservation is in a variable of type Reservation and you want to see if you can save it.

You have NewReservation.StartDateTime and NewReservation.EndDateTime.

You have an entity with several Reservations that you can query.


You can have reservations that overlap in several ways and must be refused. But if one ends the minute the next one starts, it can be valid.

On the query check the easiest way possible: Reservation1 is not before nor after Reservation 2, so it must overlap.

Reservation.StartDateTime<NewReservation.EndDateTime and

Reservation.EndDateTime>NewReservation.StartDateTime

and of course, Reservation.Status = ...

What this query returns is the list of reservations that overlap. If there is any, the reserve is invalid and can't be saved.


Solution

Nuno Reis wrote:

Time periods are always fun to analyze.

I assume that the new reservation is in a variable of type Reservation and you want to see if you can save it.

You have NewReservation.StartDateTime and NewReservation.EndDateTime.

You have an entity with several Reservations that you can query.


You can have reservations that overlap in several ways and must be refused. But if one ends the minute the next one starts, it can be valid.

On the query check the easiest way possible: Reservation1 is not before nor after Reservation 2, so it must overlap.

Reservation.StartDateTime<NewReservation.EndDateTime and

Reservation.EndDateTime>NewReservation.StartDateTime

and of course, Reservation.Status = ...

What this query returns is the list of reservations that overlap. If there is any, the reserve is invalid and can't be saved.


Thank you so much.