@@ -28,7 +28,9 @@ public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Gu
28
28
DurationInMinutes = request . DurationInMinutes ,
29
29
TableId = table . Id ,
30
30
AppUserId = userId ,
31
- AmountOfPeople = request . AmountOfPeople
31
+ AmountOfPeople = request . AmountOfPeople ,
32
+ Id = Guid . NewGuid ( ) ,
33
+ RestaurantId = table . RestaurantId
32
34
} ;
33
35
34
36
await _unitOfWork . BookingRepository . InsertAsync ( newBooking ) ;
@@ -40,11 +42,50 @@ public async Task<IActionResult> CreateBookingAsync(CreateBookingDto request, Gu
40
42
Date = newBooking . Date ,
41
43
DurationInMinutes = newBooking . DurationInMinutes ,
42
44
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
44
83
} ;
84
+
45
85
return new CreatedResult ( string . Empty , bookingDto ) ;
46
86
}
47
87
88
+
48
89
public async Task < IActionResult > DeleteBookingAsync ( Guid bookingId , Guid userId )
49
90
{
50
91
var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
@@ -59,25 +100,46 @@ public async Task<IActionResult> DeleteBookingAsync(Guid bookingId, Guid userId)
59
100
60
101
public async Task < IActionResult > GetBookingByIdAsync ( Guid bookingId , Guid userId )
61
102
{
62
-
63
103
var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
64
104
65
105
if ( booking == null )
66
106
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
+
67
116
var bookingDto = new BookingDto
68
117
{
69
118
Id = booking . Id ,
70
119
Date = booking . Date ,
71
120
DurationInMinutes = booking . DurationInMinutes ,
72
121
AmountOfPeople = booking . AmountOfPeople ,
73
- AppUserId = userId
122
+ AppUserId = userId ,
123
+ RestaurantId = booking . RestaurantId
74
124
} ;
75
125
return new OkObjectResult ( bookingDto ) ;
76
126
}
77
127
78
128
public async Task < IActionResult > GetAllBookings ( Guid userId )
79
129
{
80
130
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
+ }
81
143
82
144
return new OkObjectResult ( bookings ) ;
83
145
}
@@ -87,6 +149,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
87
149
var booking = await _unitOfWork . BookingRepository . GetBookingByIdForSpecificUserAsync ( bookingId , userId ) ;
88
150
if ( booking == null )
89
151
return new BadRequestObjectResult ( $ "Booking with id { bookingId } doesn't exist.") ;
152
+
153
+ // TODO: change tableId when user changed amount of people.
90
154
91
155
var newBooking = new Booking
92
156
{
@@ -95,7 +159,8 @@ public async Task<IActionResult> UpdateBookingAsync(UpdateBookingDto updateBooki
95
159
DurationInMinutes = updateBookingDto . DurationInMinutes ,
96
160
AmountOfPeople = updateBookingDto . AmountOfPeople ,
97
161
TableId = booking . TableId ,
98
- AppUserId = userId
162
+ AppUserId = userId ,
163
+ RestaurantId = booking . RestaurantId
99
164
} ;
100
165
101
166
await _unitOfWork . BookingRepository . Update ( newBooking ) ;
0 commit comments