-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor bookingRequest into service and improve error handling
- Loading branch information
Showing
5 changed files
with
178 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kabinizer-back-end/kabinizer-api/Dtos/BookingRequest/BookingRequestDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using kabinizer_data.Entities; | ||
|
||
namespace kabinizer_api.Dtos.BookingRequest; | ||
|
||
public class BookingRequestDto( | ||
Guid id, | ||
Guid userId, | ||
Guid periodId, | ||
DateTime createdDate, | ||
Guid createdBy, | ||
DateTime? updatedDate, | ||
Guid? updatedBy, | ||
UserEntity user, | ||
PeriodEntity period) | ||
{ | ||
public Guid Id { get; set; } = id; | ||
public Guid UserId { get; set; } = userId; | ||
public UserEntity User { get; set; } = user; | ||
public PeriodEntity Period { get; set; } = period; | ||
public Guid PeriodId { get; set; } = periodId; | ||
public DateTime CreatedDate { get; set; } = createdDate; | ||
public Guid CreatedBy { get; set; } = createdBy; | ||
public DateTime? UpdatedDate { get; set; } = updatedDate; | ||
public Guid? UpdatedBy { get; set; } = updatedBy; | ||
|
||
public static BookingRequestDto FromModel(BookingRequestEntity? e) | ||
{ | ||
return new BookingRequestDto(e.Id, e.UserId, e.PeriodId, e.CreatedDate, e.CreatedBy, e.UpdatedDate, e.UpdatedBy, | ||
Check warning on line 28 in kabinizer-back-end/kabinizer-api/Dtos/BookingRequest/BookingRequestDto.cs GitHub Actions / build
|
||
e.User, e.Period); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
kabinizer-back-end/kabinizer-api/Services/BookingRequest/BookingRequestService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using kabinizer_api.Dtos.BookingRequest; | ||
using kabinizer_data; | ||
using kabinizer_data.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace kabinizer_api.Services.BookingRequest; | ||
|
||
public abstract class BookingRequestService(EntityContext entityContext, ITokenService tokenService) | ||
{ | ||
private async Task<BookingRequestEntity?> GetBookingRequestById(Guid bookingRequestId) | ||
{ | ||
return await entityContext.BookingRequests | ||
.Include(br => br.User) | ||
.Include(br => br.Period) | ||
.FirstOrDefaultAsync(b => b.Id == bookingRequestId && b.UserId == tokenService.GetUserId()) | ||
?? throw new InvalidOperationException("No matching booking request found."); | ||
} | ||
|
||
public async Task<BookingRequestEntity?> GetBookingRequest(Guid bookingRequestId) | ||
{ | ||
BookingRequestEntity? id = await GetBookingRequestById(bookingRequestId); | ||
if (id == null) | ||
{ | ||
throw new Exception("Booking request does not exist or does not belong to the current user"); | ||
} | ||
|
||
return id; | ||
} | ||
|
||
public async Task<IEnumerable<BookingRequestEntity>> GetBookingRequests() | ||
{ | ||
var bookingRequestEntities = await entityContext.BookingRequests | ||
.Include(br => br.User) | ||
.Include(br => br.Period) | ||
.Where(b => b.UserId == tokenService.GetUserId()) | ||
.ToListAsync(); | ||
if (bookingRequestEntities == null) | ||
{ | ||
throw new Exception("No booking requests found for the current user"); | ||
} | ||
|
||
return bookingRequestEntities; | ||
} | ||
|
||
public async Task<BookingRequestEntity?> AddBookingRequest(CreateBookingRequestDto request) | ||
{ | ||
var period = await entityContext.Periods | ||
.Include(p => p.Draw) | ||
.FirstOrDefaultAsync(p => p.Draw != null && p.Id == request.PeriodId && p.Draw.DeadlineEnd >= DateTime.Now); | ||
|
||
if (period == null) | ||
{ | ||
throw new Exception("Period does not exist, is not part of a draw, or the draw has ended"); | ||
} | ||
|
||
var user = await entityContext.Users.FirstOrDefaultAsync(u => u.Id == tokenService.GetUserId()); | ||
if (user == null) | ||
{ | ||
throw new Exception("User does not exist"); | ||
} | ||
|
||
var bookingRequest = new BookingRequestEntity(tokenService.GetUserId(), request.PeriodId, user, period); | ||
entityContext.BookingRequests.Add(bookingRequest); | ||
await entityContext.SaveChangesAsync(); | ||
return bookingRequest; | ||
} | ||
|
||
public async Task<bool> DeleteBookingRequest(Guid bookingRequestId) | ||
{ | ||
var bookingRequest = await GetBookingRequestById(bookingRequestId); | ||
if (bookingRequest == null) | ||
{ | ||
throw new Exception( | ||
$"Booking request with id {bookingRequestId} does not exist or does not belong to the current user"); | ||
} | ||
|
||
entityContext.BookingRequests.Remove(bookingRequest); | ||
await entityContext.SaveChangesAsync(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters