Skip to content

Commit 271703e

Browse files
committed
change booking response
1 parent abff2f1 commit 271703e

File tree

11 files changed

+482
-22
lines changed

11 files changed

+482
-22
lines changed

TableBooking.Api/Services/BookingService.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Gu
2828
DurationInMinutes = request.DurationInMinutes,
2929
TableId = table.Id,
3030
AppUserId = userId,
31-
AmountOfPeople = request.AmountOfPeople
31+
AmountOfPeople = request.AmountOfPeople,
32+
Id = Guid.NewGuid(),
33+
RestaurantId = table.RestaurantId
3234
};
3335

3436
await _unitOfWork.BookingRepository.InsertAsync(newBooking);
@@ -40,7 +42,8 @@ public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Gu
4042
Date = newBooking.Date,
4143
DurationInMinutes = newBooking.DurationInMinutes,
4244
AmountOfPeople = newBooking.AmountOfPeople,
43-
AppUserId = userId
45+
AppUserId = userId,
46+
RestaurantId = newBooking.RestaurantId
4447
};
4548
return new CreatedResult(string.Empty, bookingDto);
4649
}
@@ -59,25 +62,46 @@ public async Task<IActionResult> DeleteBookingAsync(Guid bookingId, Guid userId)
5962

6063
public async Task<IActionResult> GetBookingByIdAsync(Guid bookingId, Guid userId)
6164
{
62-
6365
var booking = await _unitOfWork.BookingRepository.GetBookingByIdForSpecificUserAsync(bookingId, userId);
6466

6567
if (booking == null)
6668
return new BadRequestObjectResult("Bad request: no bookings");
69+
70+
if (booking.RestaurantId == Guid.Empty)
71+
{
72+
var restaurantId= await _unitOfWork.TableRepository.GetRestaurantIdByTableIdAsync(booking.TableId);
73+
74+
booking.RestaurantId = restaurantId;
75+
await _unitOfWork.BookingRepository.Update(booking);
76+
}
77+
6778
var bookingDto = new BookingDto
6879
{
6980
Id = booking.Id,
7081
Date = booking.Date,
7182
DurationInMinutes = booking.DurationInMinutes,
7283
AmountOfPeople = booking.AmountOfPeople,
73-
AppUserId = userId
84+
AppUserId = userId,
85+
RestaurantId = booking.RestaurantId
7486
};
7587
return new OkObjectResult(bookingDto);
7688
}
7789

7890
public async Task<IActionResult> GetAllBookings(Guid userId)
7991
{
8092
var bookings = await _unitOfWork.BookingRepository.GetAllBookingsForSpecificUserAsync(userId);
93+
94+
foreach (var booking in bookings)
95+
{
96+
if (booking.RestaurantId == Guid.Empty)
97+
{
98+
99+
var restaurantId= await _unitOfWork.TableRepository.GetRestaurantIdByTableIdAsync(booking.TableId);
100+
101+
booking.RestaurantId = restaurantId;
102+
await _unitOfWork.BookingRepository.Update(booking);
103+
}
104+
}
81105

82106
return new OkObjectResult(bookings);
83107
}
@@ -87,6 +111,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
87111
var booking = await _unitOfWork.BookingRepository.GetBookingByIdForSpecificUserAsync(bookingId, userId);
88112
if (booking == null)
89113
return new BadRequestObjectResult($"Booking with id {bookingId} doesn't exist.");
114+
115+
// TODO: change tableId when user changed amount of people.
90116

91117
var newBooking = new Booking
92118
{
@@ -95,7 +121,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
95121
DurationInMinutes = updateBookingDto.DurationInMinutes,
96122
AmountOfPeople = updateBookingDto.AmountOfPeople,
97123
TableId = booking.TableId,
98-
AppUserId = userId
124+
AppUserId = userId,
125+
RestaurantId = booking.RestaurantId
99126
};
100127

101128
await _unitOfWork.BookingRepository.Update(newBooking);

TableBooking.Logic/Interfaces/ITableRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public interface ITableRepository : IGenericRepository<Table>
66
{
77
Task<IEnumerable<Table>> GetTablesByRestaurantIdAsync(Guid restaurantId);
88
Task<Table> GetTableByTableIdAsync(Guid tableId);
9+
Task<Guid> GetRestaurantIdByTableIdAsync(Guid tableId);
910
}

TableBooking.Logic/Repositories/GenericRepository.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ public async Task Update(T entity)
5454

5555
if (existingEntity != null)
5656
{
57-
_context.Entry(existingEntity).State = EntityState.Detached;
57+
_context.Entry(existingEntity).CurrentValues.SetValues(entity);
58+
}
59+
else
60+
{
61+
ObjectSet.Add(entity);
5862
}
5963

60-
ObjectSet.Update(entity);
64+
await _context.SaveChangesAsync();
6165
}
6266

6367
private object[] GetKeyValues(T entity)

TableBooking.Logic/Repositories/TableRepository.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public async Task<IEnumerable<Table>> GetTablesByRestaurantIdAsync(Guid restaura
1919

2020
public async Task<Table> GetTableByTableIdAsync(Guid tableId)
2121
{
22-
return (await ObjectSet.FirstOrDefaultAsync(t => t.Id == tableId))!;
23-
}
22+
return (await ObjectSet.FirstOrDefaultAsync(t => t.Id == tableId))!;
23+
}
24+
25+
public async Task<Guid> GetRestaurantIdByTableIdAsync(Guid tableId)
26+
{
27+
var table = await ObjectSet.FirstOrDefaultAsync(t => t.Id == tableId);
28+
29+
return table?.RestaurantId ?? Guid.Empty;
30+
}
2431
}

TableBooking.Model/Dtos/BookingDtos/BookingDto.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public class BookingDto
77
public int DurationInMinutes { get; set; }
88
public int AmountOfPeople { get; set; }
99
public Guid AppUserId { get; set; }
10+
public Guid RestaurantId { get; set; }
1011
}

0 commit comments

Comments
 (0)