@@ -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 ) ;
0 commit comments