diff --git a/ApiEfCoreLibrary/Endpoints/Author/CreateAuthorEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Author/CreateAuthorEndpoint.cs index 0269eef..c22037b 100644 --- a/ApiEfCoreLibrary/Endpoints/Author/CreateAuthorEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Author/CreateAuthorEndpoint.cs @@ -1,8 +1,8 @@ using ApiEfCoreLibrary.DTO.Author.Request; using ApiEfCoreLibrary.DTO.Author.Response; +using FastEndpoints; namespace ApiEfCoreLibrary.Endpoints.Author; -using FastEndpoints; public class CreateAuthorEndpoint(LibraryDbContext database) : Endpoint { @@ -14,7 +14,7 @@ public class CreateAuthorEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Author? author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (author == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Author/GetAllAuthorsEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Author/GetAllAuthorsEndpoint.cs index abd0710..ac582ae 100644 --- a/ApiEfCoreLibrary/Endpoints/Author/GetAllAuthorsEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Author/GetAllAuthorsEndpoint.cs @@ -15,7 +15,7 @@ public class GetAllAuthorsEndpoint(LibraryDbContext database) : EndpointWithoutR public override async Task HandleAsync(CancellationToken ct) { - var authors = await database.Authors + List authors = await database.Authors .Include(x => x.Books) .Select(author => new GetAuthorDto() { diff --git a/ApiEfCoreLibrary/Endpoints/Author/GetAuthorEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Author/GetAuthorEndpoint.cs index 478941d..e1c879b 100644 --- a/ApiEfCoreLibrary/Endpoints/Author/GetAuthorEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Author/GetAuthorEndpoint.cs @@ -21,7 +21,7 @@ public class GetAuthorEndpoint(LibraryDbContext database) : Endpoint x.Books) .SingleOrDefaultAsync(x => x.Id == req.Id, ct); diff --git a/ApiEfCoreLibrary/Endpoints/Author/UpdateAuthorEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Author/UpdateAuthorEndpoint.cs index e6f5308..0fab5ec 100644 --- a/ApiEfCoreLibrary/Endpoints/Author/UpdateAuthorEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Author/UpdateAuthorEndpoint.cs @@ -16,7 +16,7 @@ public class UpdateAuthorEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Author? author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (author == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Book/CreateBookEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Book/CreateBookEndpoint.cs index 94b19b1..af92df9 100644 --- a/ApiEfCoreLibrary/Endpoints/Book/CreateBookEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Book/CreateBookEndpoint.cs @@ -15,7 +15,7 @@ public class CreateBookEndpoint(LibraryDbContext database) : Endpoint a.Id == req.AuthorId, ct); + Models.Author? authorExists = await database.Authors.FirstOrDefaultAsync(a => a.Id == req.AuthorId, ct); if (authorExists == null) { @@ -23,7 +23,7 @@ public class CreateBookEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Book? book = await database.Books.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (book == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Book/GetAllBooksEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Book/GetAllBooksEndpoint.cs index 6f439ee..b633e7e 100644 --- a/ApiEfCoreLibrary/Endpoints/Book/GetAllBooksEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Book/GetAllBooksEndpoint.cs @@ -13,7 +13,7 @@ public class GetAllBooksEndpoint(LibraryDbContext database) : EndpointWithoutReq public override async Task HandleAsync(CancellationToken ct) { - var books = await database.Books + List books = await database.Books .Include(b => b.Author) .Select(book => new GetBookDto() { diff --git a/ApiEfCoreLibrary/Endpoints/Book/GetBookEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Book/GetBookEndpoint.cs index 888a159..6d988e0 100644 --- a/ApiEfCoreLibrary/Endpoints/Book/GetBookEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Book/GetBookEndpoint.cs @@ -18,7 +18,7 @@ public class GetBookEndpoint(LibraryDbContext database) : Endpoint b.Author).SingleOrDefaultAsync(x => x.Id == req.Id, ct); + Models.Book? book = await database.Books.Include(b => b.Author).SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (book == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Book/UpdateBookEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Book/UpdateBookEndpoint.cs index 144a25f..4b37014 100644 --- a/ApiEfCoreLibrary/Endpoints/Book/UpdateBookEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Book/UpdateBookEndpoint.cs @@ -15,7 +15,7 @@ public class UpdateBookEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Book? book = await database.Books.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (book == null) { @@ -23,7 +23,7 @@ public class UpdateBookEndpoint(LibraryDbContext database) : Endpoint a.Id == req.AuthorId, ct); + Models.Author? authorExists = await database.Authors.FirstOrDefaultAsync(a => a.Id == req.AuthorId, ct); if (authorExists == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Loan/CreateLoanEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Loan/CreateLoanEndpoint.cs index 2e57126..05e6f22 100644 --- a/ApiEfCoreLibrary/Endpoints/Loan/CreateLoanEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Loan/CreateLoanEndpoint.cs @@ -15,7 +15,7 @@ public class CreateLoanEndpoint(LibraryDbContext database) : Endpoint a.Id == req.BookId, ct); + Models.Book? bookExists = await database.Books.FirstOrDefaultAsync(a => a.Id == req.BookId, ct); if (bookExists == null) { @@ -23,7 +23,7 @@ public class CreateLoanEndpoint(LibraryDbContext database) : Endpoint a.Id == req.UserId, ct); + Models.User? userExists = await database.Users.FirstOrDefaultAsync(a => a.Id == req.UserId, ct); if (userExists == null) { @@ -31,7 +31,7 @@ public class CreateLoanEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Loan? loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (loan == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Loan/GetAllLoansEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Loan/GetAllLoansEndpoint.cs index e942f98..1a60c1e 100644 --- a/ApiEfCoreLibrary/Endpoints/Loan/GetAllLoansEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Loan/GetAllLoansEndpoint.cs @@ -13,7 +13,7 @@ public class GetAllLoanEndpoint(LibraryDbContext database) : EndpointWithoutRequ public override async Task HandleAsync(CancellationToken ct) { - var loans = await database.Loans + List loans = await database.Loans .Include(l => l.Book) .ThenInclude(b => b.Author) .Include(l => l.User) diff --git a/ApiEfCoreLibrary/Endpoints/Loan/GetLoanEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Loan/GetLoanEndpoint.cs index 10c92af..69b9c5f 100644 --- a/ApiEfCoreLibrary/Endpoints/Loan/GetLoanEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Loan/GetLoanEndpoint.cs @@ -18,7 +18,7 @@ public class GetLoanEndpoint(LibraryDbContext database) : Endpoint l.Book) + Models.Loan? loan = await database.Loans.Include(l => l.Book) .ThenInclude(b => b.Author) .Include(l => l.User) .SingleOrDefaultAsync(x => x.Id == req.Id, ct); diff --git a/ApiEfCoreLibrary/Endpoints/Loan/PatchLoanEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Loan/PatchLoanEndpoint.cs index 018ea2d..152a20b 100644 --- a/ApiEfCoreLibrary/Endpoints/Loan/PatchLoanEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Loan/PatchLoanEndpoint.cs @@ -1,53 +1,53 @@ -using ApiEfCoreLibrary.DTO.Loan.Request; -using ApiEfCoreLibrary.DTO.Loan.Response; -using FastEndpoints; -using Microsoft.EntityFrameworkCore; - -namespace ApiEfCoreLibrary.Endpoints.Loan; - -public class PatchLoanEndpoint(LibraryDbContext database) : Endpoint -{ - public override void Configure() - { - Patch("/api/loans/{@Id}/EffectiveReturningDate", x => new {x.Id}); - Roles("admin", "librarian"); - } - - public override async Task HandleAsync(PatchLoanDto req, CancellationToken ct) - { - var loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct); - - if (loan == null) - { - await Send.NotFoundAsync(ct); - return; - } - - if (req.EffectiveReturningDate <= DateOnly.FromDateTime(DateTime.Now)) - { - await Send.StringAsync("Erreur de date. La date est inférieure à la date actuelle.", 400); - return; - } - - if (loan.EffectiveReturningDate != null) - { - await Send.StringAsync("Impossible de modifier la date de retour.", 400); - return; - } - - loan.EffectiveReturningDate = req.EffectiveReturningDate; - await database.SaveChangesAsync(ct); - - GetLoanDto responseDto = new() - { - Id = loan.Id, - BookId = loan.BookId, - UserId = loan.UserId, - Date = loan.Date, - PlannedReturningDate = loan.PlannedReturningDate, - EffectiveReturningDate = loan.EffectiveReturningDate - }; - - await Send.OkAsync(responseDto, ct); - } +using ApiEfCoreLibrary.DTO.Loan.Request; +using ApiEfCoreLibrary.DTO.Loan.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace ApiEfCoreLibrary.Endpoints.Loan; + +public class PatchLoanEndpoint(LibraryDbContext database) : Endpoint +{ + public override void Configure() + { + Patch("/api/loans/{@Id}/EffectiveReturningDate", x => new {x.Id}); + Roles("admin", "librarian"); + } + + public override async Task HandleAsync(PatchLoanDto req, CancellationToken ct) + { + Models.Loan? loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (loan == null) + { + await Send.NotFoundAsync(ct); + return; + } + + if (req.EffectiveReturningDate <= DateOnly.FromDateTime(DateTime.Now)) + { + await Send.StringAsync("Erreur de date. La date est inférieure à la date actuelle.", 400); + return; + } + + if (loan.EffectiveReturningDate != null) + { + await Send.StringAsync("Impossible de modifier la date de retour.", 400); + return; + } + + loan.EffectiveReturningDate = req.EffectiveReturningDate; + await database.SaveChangesAsync(ct); + + GetLoanDto responseDto = new() + { + Id = loan.Id, + BookId = loan.BookId, + UserId = loan.UserId, + Date = loan.Date, + PlannedReturningDate = loan.PlannedReturningDate, + EffectiveReturningDate = loan.EffectiveReturningDate + }; + + await Send.OkAsync(responseDto, ct); + } } \ No newline at end of file diff --git a/ApiEfCoreLibrary/Endpoints/Loan/UpdateLoanEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Loan/UpdateLoanEndpoint.cs index 5e49ead..6e47925 100644 --- a/ApiEfCoreLibrary/Endpoints/Loan/UpdateLoanEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Loan/UpdateLoanEndpoint.cs @@ -15,7 +15,7 @@ public class UpdateLoanEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Loan? loan = await database.Loans.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (loan == null) { @@ -23,7 +23,7 @@ public class UpdateLoanEndpoint(LibraryDbContext database) : Endpoint a.Id == req.BookId, ct); + Models.Book? bookExists = await database.Books.FirstOrDefaultAsync(a => a.Id == req.BookId, ct); if (bookExists == null) { @@ -31,7 +31,7 @@ public class UpdateLoanEndpoint(LibraryDbContext database) : Endpoint a.Id == req.UserId, ct); + Models.User? userExists = await database.Users.FirstOrDefaultAsync(a => a.Id == req.UserId, ct); if (userExists == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs index c71ed2e..1ada6f2 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs @@ -17,7 +17,7 @@ public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Login? login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (login == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs index f050fb7..a4fe428 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs @@ -14,7 +14,7 @@ public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutReq public override async Task HandleAsync(CancellationToken ct) { - var logins = await database.Logins + List logins = await database.Logins .Select(login => new GetLoginDto() { Id = login.Id, diff --git a/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs index 9ec4d4b..2810592 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs @@ -18,7 +18,7 @@ public class GetLoginEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); if (login == null) diff --git a/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs index 7a7e9fb..18b25d2 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs @@ -16,7 +16,7 @@ public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.Login? login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (login == null) { diff --git a/ApiEfCoreLibrary/Endpoints/Login/UserLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/UserLoginEndpoint.cs index e743d70..9a22d15 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/UserLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/UserLoginEndpoint.cs @@ -16,7 +16,7 @@ public class UserLoginEndpoint(LibraryDbContext database) : Endpoint x.Username == req.Username, ct); + Models.Login? login = await database.Logins.SingleOrDefaultAsync(x => x.Username == req.Username, ct); if (login == null) { @@ -26,7 +26,7 @@ public class UserLoginEndpoint(LibraryDbContext database) : Endpoint { o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong"; diff --git a/ApiEfCoreLibrary/Endpoints/User/CreateUserEndpoint.cs b/ApiEfCoreLibrary/Endpoints/User/CreateUserEndpoint.cs index e032066..4b11713 100644 --- a/ApiEfCoreLibrary/Endpoints/User/CreateUserEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/User/CreateUserEndpoint.cs @@ -14,7 +14,7 @@ public class CreateUserEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (user == null) { diff --git a/ApiEfCoreLibrary/Endpoints/User/GetAllUsersEndpoint.cs b/ApiEfCoreLibrary/Endpoints/User/GetAllUsersEndpoint.cs index c8a4c8f..cddd6c7 100644 --- a/ApiEfCoreLibrary/Endpoints/User/GetAllUsersEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/User/GetAllUsersEndpoint.cs @@ -14,7 +14,7 @@ public class GetAllUsersEndpoint(LibraryDbContext database) : EndpointWithoutReq public override async Task HandleAsync(CancellationToken ct) { - var user = await database.Users + List user = await database.Users .Include(x => x.Loans)! .ThenInclude(l => l.Book) .ThenInclude(b => b!.Author) diff --git a/ApiEfCoreLibrary/Endpoints/User/GetUserEndpoint.cs b/ApiEfCoreLibrary/Endpoints/User/GetUserEndpoint.cs index 09815dc..5cf4848 100644 --- a/ApiEfCoreLibrary/Endpoints/User/GetUserEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/User/GetUserEndpoint.cs @@ -20,7 +20,7 @@ public class GetUserEndpoint(LibraryDbContext database) : Endpoint x.Loans)! .ThenInclude(l => l.Book) .ThenInclude(b => b!.Author) diff --git a/ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs b/ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs index 27acb5d..27b2e8c 100644 --- a/ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs @@ -15,7 +15,7 @@ public class UpdateUserEndpoint(LibraryDbContext database) : Endpoint x.Id == req.Id, ct); + Models.User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct); if (user == null) {