-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservationService.cs
More file actions
318 lines (256 loc) · 11.7 KB
/
ReservationService.cs
File metadata and controls
318 lines (256 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using eduHub.Application.Common;
using eduHub.Application.DTOs.Reservations;
using eduHub.Application.Interfaces.Reservations;
using eduHub.Domain.Entities;
using eduHub.Domain.Enums;
using eduHub.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Npgsql;
namespace eduHub.Infrastructure.Services
{
public class ReservationService : IReservationService
{
private readonly AppDbContext _context;
public ReservationService(AppDbContext context)
{
_context = context;
}
public async Task<ReservationResponseDto?> GetByIdAsync(int id, int currentUserId, bool isAdmin)
{
var query = _context.Reservations.AsNoTracking().Where(r => r.Id == id);
if (!isAdmin)
query = query.Where(r => r.CreatedByUserId == currentUserId);
var reservation = await query.FirstOrDefaultAsync();
if (reservation == null)
return null;
var dto = MapToDto(reservation);
if (!isAdmin)
dto.CreatedByUserId = null;
return dto;
}
public async Task<CursorPageResult<ReservationResponseDto>> SearchAsync(
ReservationQueryParameters queryParams,
int? currentUserId,
bool isAdmin)
{
if (!isAdmin && !currentUserId.HasValue)
throw new UnauthorizedAccessException("Admin access required.");
var pageSize = ClampPageSize(queryParams.PageSize);
var query = _context.Reservations
.AsNoTracking()
.Include(r => r.Room)
.ThenInclude(r => r.Building)
.AsQueryable();
if (queryParams.RoomId.HasValue)
query = query.Where(r => r.RoomId == queryParams.RoomId.Value);
if (queryParams.BuildingId.HasValue)
query = query.Where(r => r.Room != null && r.Room.BuildingId == queryParams.BuildingId.Value);
if (queryParams.StartTimeUtc.HasValue)
{
var startUtc = queryParams.StartTimeUtc.Value.ToUniversalTime();
query = query.Where(r => r.EndTimeUtc >= startUtc);
}
if (queryParams.EndTimeUtc.HasValue)
{
var endUtc = queryParams.EndTimeUtc.Value.ToUniversalTime();
query = query.Where(r => r.StartTimeUtc <= endUtc);
}
if (!isAdmin && currentUserId.HasValue)
query = query.Where(r => r.CreatedByUserId == currentUserId.Value);
var sort = queryParams.Sort?.ToLowerInvariant();
var isDesc = sort == "start_desc";
query = isDesc
? query.OrderByDescending(r => r.StartTimeUtc).ThenByDescending(r => r.Id)
: query.OrderBy(r => r.StartTimeUtc).ThenBy(r => r.Id);
ReservationCursor? cursor = null;
var cursorProvided = !string.IsNullOrWhiteSpace(queryParams.Cursor);
if (cursorProvided && !CursorSerializer.TryDecode(queryParams.Cursor, out cursor))
throw new InvalidOperationException("Invalid cursor.");
if (cursor != null)
{
if (cursor.IsDescending != isDesc)
throw new InvalidOperationException("Cursor sort does not match requested sort.");
query = isDesc
? query.Where(r =>
r.StartTimeUtc < cursor.StartUtc ||
(r.StartTimeUtc == cursor.StartUtc && r.Id < cursor.Id))
: query.Where(r =>
r.StartTimeUtc > cursor.StartUtc ||
(r.StartTimeUtc == cursor.StartUtc && r.Id > cursor.Id));
}
var reservations = await query
.Take(pageSize + 1)
.ToListAsync();
var hasMore = reservations.Count > pageSize;
if (hasMore)
reservations = reservations.Take(pageSize).ToList();
var nextCursor = hasMore
? CursorSerializer.Encode(new ReservationCursor(reservations.Last().StartTimeUtc, reservations.Last().Id, isDesc))
: null;
var dtos = reservations.Select(MapToDto).ToList();
if (!isAdmin)
{
foreach (var dto in dtos)
dto.CreatedByUserId = null;
}
return new CursorPageResult<ReservationResponseDto>
{
Items = dtos,
PageSize = pageSize,
NextCursor = nextCursor,
HasMore = hasMore
};
}
public async Task<ReservationResponseDto> CreateAsync(
ReservationCreateDto dto,
int createdByUserId)
{
var startUtc = dto.StartTimeUtc.ToUniversalTime();
var endUtc = dto.EndTimeUtc.ToUniversalTime();
var roomExists = await _context.Rooms.AnyAsync(r => r.Id == dto.RoomId);
if (!roomExists)
throw new InvalidOperationException("Room does not exist.");
await EnsureNoConflicts(dto.RoomId, startUtc, endUtc, null);
var reservation = new Reservation
{
RoomId = dto.RoomId,
StartTimeUtc = startUtc,
EndTimeUtc = endUtc,
Purpose = dto.Purpose,
Status = ReservationStatus.Pending, // Default status, adjust as needed
CreatedByUserId = createdByUserId,
CreatedAtUtc = DateTimeOffset.UtcNow
};
_context.Reservations.Add(reservation);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { SqlState: "23P01" })
{
throw new InvalidOperationException("The room is already reserved in the given time range.");
}
return MapToDto(reservation);
}
public async Task<ReservationResponseDto> UpdateAsync(
int id,
ReservationUpdateDto dto,
int currentUserId,
bool isAdmin)
{
var reservation = await _context.Reservations.FirstOrDefaultAsync(r =>
r.Id == id && (isAdmin || r.CreatedByUserId == currentUserId));
if (reservation == null)
throw new KeyNotFoundException("Reservation not found.");
var hasNewRoom = dto.RoomId != default && dto.RoomId != reservation.RoomId;
var targetRoomId = hasNewRoom ? dto.RoomId : reservation.RoomId;
var startUtc = dto.StartTimeUtc.ToUniversalTime();
var endUtc = dto.EndTimeUtc.ToUniversalTime();
var roomExists = await _context.Rooms.AnyAsync(r => r.Id == targetRoomId);
if (!roomExists)
throw new InvalidOperationException("Room does not exist.");
await EnsureNoConflicts(targetRoomId, startUtc, endUtc, reservation.Id);
reservation.RoomId = targetRoomId;
reservation.StartTimeUtc = startUtc;
reservation.EndTimeUtc = endUtc;
reservation.Purpose = dto.Purpose;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { SqlState: "23P01" })
{
throw new InvalidOperationException("The room is already reserved in the given time range.");
}
return MapToDto(reservation);
}
public async Task<bool> DeleteAsync(
int id,
int currentUserId,
bool isAdmin)
{
var reservation = await _context.Reservations.FirstOrDefaultAsync(r =>
r.Id == id && (isAdmin || r.CreatedByUserId == currentUserId));
if (reservation == null)
return false;
reservation.IsDeleted = true;
await _context.SaveChangesAsync();
return true;
}
public async Task<ReservationResponseDto> ApproveAsync(int id, bool isAdmin)
{
if (!isAdmin)
throw new UnauthorizedAccessException("Forbidden.");
var reservation = await _context.Reservations
.FirstOrDefaultAsync(r => r.Id == id);
if (reservation == null)
throw new KeyNotFoundException("Reservation not found.");
if (reservation.Status == ReservationStatus.Approved)
return MapToDto(reservation);
if (reservation.Status != ReservationStatus.Pending)
throw new InvalidOperationException("Only pending reservations can be approved.");
reservation.Status = ReservationStatus.Approved;
await _context.SaveChangesAsync();
return MapToDto(reservation);
}
public async Task<ReservationResponseDto> RejectAsync(int id, bool isAdmin)
{
if (!isAdmin)
throw new UnauthorizedAccessException("Forbidden.");
var reservation = await _context.Reservations
.FirstOrDefaultAsync(r => r.Id == id);
if (reservation == null)
throw new KeyNotFoundException("Reservation not found.");
if (reservation.Status == ReservationStatus.Rejected)
return MapToDto(reservation);
if (reservation.Status != ReservationStatus.Pending)
throw new InvalidOperationException("Only pending reservations can be rejected.");
reservation.Status = ReservationStatus.Rejected;
await _context.SaveChangesAsync();
return MapToDto(reservation);
}
private async Task EnsureNoConflicts(
int roomId,
DateTimeOffset startUtc,
DateTimeOffset endUtc,
int? excludeReservationId)
{
if (endUtc <= startUtc)
throw new InvalidOperationException("End time must be after start time.");
// Split query to avoid EF Core translation issues with DateTimeOffset on Sqlite
var query = _context.Reservations
.AsNoTracking()
.Where(r => r.RoomId == roomId &&
(r.Status == ReservationStatus.Pending || r.Status == ReservationStatus.Approved));
if (excludeReservationId.HasValue)
query = query.Where(r => r.Id != excludeReservationId.Value);
var reservations = await query.ToListAsync();
var hasConflict = reservations.Any(r =>
r.StartTimeUtc < endUtc &&
startUtc < r.EndTimeUtc);
if (hasConflict)
throw new InvalidOperationException("The room is already reserved in the given time range.");
}
private static int ClampPageSize(int pageSize)
{
if (pageSize < 1) return 10;
if (pageSize > 100) return 100;
return pageSize;
}
private static ReservationResponseDto MapToDto(Reservation reservation)
{
return new ReservationResponseDto
{
Id = reservation.Id,
RoomId = reservation.RoomId,
Start = reservation.StartTimeUtc,
End = reservation.EndTimeUtc,
Purpose = reservation.Purpose,
Status = reservation.Status.ToString(),
CreatedByUserId = reservation.CreatedByUserId,
CreatedAtUtc = reservation.CreatedAtUtc
};
}
private record ReservationCursor(DateTimeOffset StartUtc, int Id, bool IsDescending);
}
}