@@ -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,11 +42,50 @@ 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
47+ } ;
48+ return new CreatedResult ( string . Empty , bookingDto ) ;
49+ }
50+
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
4483 } ;
84+
4585 return new CreatedResult ( string . Empty , bookingDto ) ;
4686 }
4787
88+
4889 public async Task < IActionResult > DeleteBookingAsync ( Guid bookingId , Guid userId )
4990 {
5091 var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
@@ -59,25 +100,46 @@ public async Task<IActionResult> DeleteBookingAsync(Guid bookingId, Guid userId)
59100
60101 public async Task < IActionResult > GetBookingByIdAsync ( Guid bookingId , Guid userId )
61102 {
62-
63103 var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
64104
65105 if ( booking == null )
66106 return new BadRequestObjectResult ( "Bad request: no bookings" ) ;
107+
108+ if ( booking . RestaurantId == Guid . Empty )
109+ {
110+ var restaurantId = await _unitOfWork . TableRepository . GetRestaurantIdByTableIdAsync ( booking . TableId ) ;
111+
112+ booking . RestaurantId = restaurantId ;
113+ await _unitOfWork . BookingRepository . Update ( booking ) ;
114+ }
115+
67116 var bookingDto = new BookingDto
68117 {
69118 Id = booking . Id ,
70119 Date = booking . Date ,
71120 DurationInMinutes = booking . DurationInMinutes ,
72121 AmountOfPeople = booking . AmountOfPeople ,
73- AppUserId = userId
122+ AppUserId = userId ,
123+ RestaurantId = booking . RestaurantId
74124 } ;
75125 return new OkObjectResult ( bookingDto ) ;
76126 }
77127
78128 public async Task < IActionResult > GetAllBookings ( Guid userId )
79129 {
80130 var bookings = await _unitOfWork . BookingRepository . GetAllBookingsForSpecificUserAsync ( userId ) ;
131+
132+ foreach ( var booking in bookings )
133+ {
134+ if ( booking . RestaurantId == Guid . Empty )
135+ {
136+
137+ var restaurantId = await _unitOfWork . TableRepository . GetRestaurantIdByTableIdAsync ( booking . TableId ) ;
138+
139+ booking . RestaurantId = restaurantId ;
140+ await _unitOfWork . BookingRepository . Update ( booking ) ;
141+ }
142+ }
81143
82144 return new OkObjectResult ( bookings ) ;
83145 }
@@ -87,6 +149,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
87149 var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
88150 if ( booking == null )
89151 return new BadRequestObjectResult ( $ "Booking with id { bookingId } doesn't exist.") ;
152+
153+ // TODO: change tableId when user changed amount of people.
90154
91155 var newBooking = new Booking
92156 {
@@ -95,7 +159,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
95159 DurationInMinutes = updateBookingDto . DurationInMinutes ,
96160 AmountOfPeople = updateBookingDto . AmountOfPeople ,
97161 TableId = booking . TableId ,
98- AppUserId = userId
162+ AppUserId = userId ,
163+ RestaurantId = booking . RestaurantId
99164 } ;
100165
101166 await _unitOfWork . BookingRepository . Update ( newBooking ) ;
0 commit comments