-
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 + fro…
…nt-end refactor (#114) Also, an overhaul of the front-end booking request code. I started by trying to simplify components here but ended quite similar to what we had before... 🙈 I hope you don't mind @wigsnes ❤️ I just wanted to mention that the save button has been removed. Instead, clicking a period in the calendar books it right away. And it can be toggled back and forth as much as you want. Relying on the state in the back-end instead of local. A loading state has been added to the button as well when toggling its state.
- Loading branch information
Showing
41 changed files
with
1,286 additions
and
1,030 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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
using kabinizer_data.Entities; | ||
|
||
namespace kabinizer_api.Dtos.BookingRequest; | ||
|
||
public class BookingRequestDto | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid UserId { get; set; } | ||
public Guid PeriodId { get; set; } | ||
public DateTime CreatedDate { get; set; } | ||
public Guid CreatedBy { get; set; } | ||
public DateTime? UpdatedDate { get; set; } | ||
public Guid? UpdatedBy { get; set; } | ||
|
||
public static BookingRequestDto FromModel(BookingRequestEntity e) | ||
{ | ||
return new BookingRequestDto | ||
{ | ||
Id = e.Id, | ||
UserId = e.UserId, | ||
PeriodId = e.PeriodId, | ||
CreatedDate = e.CreatedDate, | ||
CreatedBy = e.CreatedBy, | ||
UpdatedDate = e.UpdatedDate, | ||
UpdatedBy = e.UpdatedBy | ||
}; | ||
} | ||
} |
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
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
119 changes: 119 additions & 0 deletions
119
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,119 @@ | ||
using kabinizer_api.Dtos.BookingRequest; | ||
using kabinizer_data; | ||
using kabinizer_data.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace kabinizer_api.Services.BookingRequest; | ||
|
||
public 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 | ||
{ | ||
PeriodId = period.Id, | ||
UserId = user.Id, | ||
CreatedDate = DateTime.Now, | ||
CreatedBy = user.Id, | ||
User = user, | ||
Period = period | ||
}; | ||
entityContext.BookingRequests.Add(bookingRequest); | ||
await entityContext.SaveChangesAsync(); | ||
return bookingRequest; | ||
} | ||
|
||
public async Task DeleteBookingRequest(Guid bookingRequestId) | ||
{ | ||
var bookingRequest = await entityContext.BookingRequests.FirstOrDefaultAsync(b => b.Id == 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(); | ||
} | ||
|
||
public async Task<List<BookingRequestEntity>> GetBookingRequestsByUser(Guid userId) | ||
{ | ||
var bookingRequests = await entityContext.BookingRequests | ||
.Include(br => br.User) | ||
.Include(br => br.Period) | ||
.Where(b => b.UserId == userId) | ||
.ToListAsync(); | ||
if (bookingRequests == null) | ||
{ | ||
throw new Exception("No booking requests found for the user"); | ||
} | ||
|
||
return bookingRequests; | ||
} | ||
|
||
public async Task<List<BookingRequestEntity>> GetBookingRequestsByPeriod(Guid periodId) | ||
{ | ||
var bookingRequests = await entityContext.BookingRequests | ||
.Include(br => br.User) | ||
.Include(br => br.Period) | ||
.Where(b => b.PeriodId == periodId) | ||
.ToListAsync(); | ||
|
||
if (bookingRequests == null) | ||
{ | ||
throw new Exception("No booking requests found for the period"); | ||
} | ||
|
||
return bookingRequests; | ||
} | ||
} |
Oops, something went wrong.