For my web app i need to handle access requests. Requests are entered for a monday-sunday schedule specifying hours for each day. This week schedule can repeat up to 52 times. A user can have multiple schedules during a week (mon 8-9 and mon 10-11) There are the following requirements:
- Searchable/filterable
- Detect overlapping requests
I want the database to handle as much of the lifting as possible. Right now the only design I can think of is storing each day’s access as a separate record. Doing this I would pull all accesses for a user and loop to determine if the new request overlaps. This requires code or a stored procedure.
Does anyone have a better database model idea or a clean way to deal with overlaps in code?
If you store each access request in a table with fields with start_time and end_time, then you could use the database’s BETWEEN functionality to determine if a particular access would overlap with one already in the database.
As an example, let’s say that someone had completed an access request for Monday from 9:00AM to 12:00PM.
Then someone else comes and tries to make an access request for Monday from 11:00AM to 3:00PM. To determine if this would conflict with something else, you’d need to look for the following possible conditions:
These can bet translated down to a SQL query which would prevent having to load and iterate over records in the application. As a bonus, you can use transactions to guard against race conditions.