Skip to content

Commit 084466d

Browse files
committed
new endpoint for automatic booking table
1 parent 271703e commit 084466d

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

TableBooking.Api/Controllers/BookingController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public async Task<IActionResult> CreateUserBooking([FromBody] CreateBookingDto b
5050

5151
return await _bookingService.CreateBookingAsync(bookingToCreateDto, userId, tableId);
5252
}
53+
54+
[HttpPost("CreateBookingAutomatically/{restaurantId}")]
55+
public async Task<IActionResult> CreateUserBookingAutomaticByRestaurantId([FromBody] CreateBookingDto bookingToCreateDto, Guid restaurantId)
56+
{
57+
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new InvalidOperationException("User ID not found in claims."));
58+
59+
return await _bookingService.CreateAutomaticBookingByRestaurantIdAsync(bookingToCreateDto, userId, restaurantId);
60+
}
5361

5462
[HttpPut("UpdateBooking/{bookingId}")]
5563
public async Task<IActionResult> UpdateUserBooking([FromBody] UpdateBookingDto updateBookingDto, Guid bookingId)

TableBooking.Api/Interfaces/IBookingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IBookingService
88
public Task<IActionResult> GetAllBookings(Guid userId);
99
public Task<IActionResult> GetBookingByIdAsync(Guid bookingId, Guid userId);
1010
public Task<IActionResult> CreateBookingAsync(CreateBookingDto createBookingDto, Guid userId, Guid tableId);
11+
public Task<IActionResult> CreateAutomaticBookingByRestaurantIdAsync(CreateBookingDto createBookingDto, Guid userId, Guid restaurantId);
1112
public Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBookingDto, Guid userId, Guid bookingId);
1213
public Task<IActionResult> DeleteBookingAsync(Guid bookingId, Guid userId);
1314
}

TableBooking.Api/Services/BookingService.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Gu
4848
return new CreatedResult(string.Empty, bookingDto);
4949
}
5050

51+
public async Task<IActionResult> CreateAutomaticBookingByRestaurantIdAsync(CreateBookingDto createBookingDto, Guid userId, Guid restaurantId)
52+
{
53+
// TODO: Assign 17:30 and not like 17:36:35Z ...
54+
var availableTable = await _unitOfWork.TableRepository.GetAvailableTableAsync(restaurantId, createBookingDto.AmountOfPeople, createBookingDto.Date);
55+
56+
if (availableTable == null)
57+
{
58+
return new BadRequestObjectResult($"No available table for {createBookingDto.AmountOfPeople} people at restaurant {restaurantId}");
59+
}
60+
61+
var newBooking = new Booking
62+
{
63+
Date = createBookingDto.Date,
64+
DurationInMinutes = createBookingDto.DurationInMinutes,
65+
TableId = availableTable.Id,
66+
AppUserId = userId,
67+
AmountOfPeople = createBookingDto.AmountOfPeople,
68+
Id = Guid.NewGuid(),
69+
RestaurantId = availableTable.RestaurantId
70+
};
71+
72+
await _unitOfWork.BookingRepository.InsertAsync(newBooking);
73+
await _unitOfWork.SaveChangesAsync();
74+
75+
var bookingDto = new BookingDto
76+
{
77+
Id = newBooking.Id,
78+
Date = newBooking.Date,
79+
DurationInMinutes = newBooking.DurationInMinutes,
80+
AmountOfPeople = newBooking.AmountOfPeople,
81+
AppUserId = userId,
82+
RestaurantId = newBooking.RestaurantId
83+
};
84+
85+
return new CreatedResult(string.Empty, bookingDto);
86+
}
87+
88+
5189
public async Task<IActionResult> DeleteBookingAsync(Guid bookingId, Guid userId)
5290
{
5391
var booking = await _unitOfWork.BookingRepository.GetBookingByIdForSpecificUserAsync(bookingId, userId);

TableBooking.Logic/Interfaces/ITableRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public interface ITableRepository : IGenericRepository<Table>
66
{
77
Task<IEnumerable<Table>> GetTablesByRestaurantIdAsync(Guid restaurantId);
8+
Task<Table?> GetAvailableTableAsync(Guid restaurantId, int amountOfPeople, DateTime bookingDate);
89
Task<Table> GetTableByTableIdAsync(Guid tableId);
910
Task<Guid> GetRestaurantIdByTableIdAsync(Guid tableId);
1011
}

TableBooking.Logic/Repositories/TableRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public async Task<IEnumerable<Table>> GetTablesByRestaurantIdAsync(Guid restaura
1717
.ToListAsync();
1818
}
1919

20+
public async Task<Table?> GetAvailableTableAsync(Guid restaurantId, int amountOfPeople, DateTime bookingDate)
21+
{
22+
return await ObjectSet.Where(t =>
23+
t.Bookings != null && t.RestaurantId == restaurantId && t.NumberOfSeats >= amountOfPeople && t.Bookings.All(b => b.Date != bookingDate)).FirstOrDefaultAsync();
24+
}
25+
2026
public async Task<Table> GetTableByTableIdAsync(Guid tableId)
2127
{
2228
return (await ObjectSet.FirstOrDefaultAsync(t => t.Id == tableId))!;

0 commit comments

Comments
 (0)