diff --git a/ApiEfCoreLibrary/ApiEfCoreLibrary.csproj b/ApiEfCoreLibrary/ApiEfCoreLibrary.csproj index 8f09324..ed5ce50 100644 --- a/ApiEfCoreLibrary/ApiEfCoreLibrary.csproj +++ b/ApiEfCoreLibrary/ApiEfCoreLibrary.csproj @@ -7,6 +7,7 @@ + diff --git a/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs index cae2199..2a83ce6 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/CreateLoginEndpoint.cs @@ -1,35 +1,40 @@ -using ApiEfCoreLibrary.DTO.Author.Request; -using ApiEfCoreLibrary.DTO.Author.Response; +using ApiEfCoreLibrary.DTO.Login.Request; +using ApiEfCoreLibrary.DTO.Login.Response; +using BCrypt.Net; -namespace ApiEfCoreLibrary.Endpoints.Author; +namespace ApiEfCoreLibrary.Endpoints.Login; using FastEndpoints; -public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint +public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint { public override void Configure() { - Post("/api/authors"); + Post("/api/logins"); AllowAnonymous(); } - public override async Task HandleAsync(CreateAuthorDto req, CancellationToken ct) + public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct) { - var author = new Models.Author() + var login = new Models.Login() { - Name = req.Name, - FirstName = req.FirstName + Username = req.Username, + FullName = req.FullName, + Password = BCrypt.Net.BCrypt.HashPassword(req.Password + req.Salt), + Salt = BCrypt.Net.BCrypt.GenerateSalt(24) }; - database.Authors.Add(author); + database.Logins.Add(login); await database.SaveChangesAsync(ct); // Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400); - GetAuthorDto responseDto = new() + GetLoginDto responseDto = new() { - Id = author.Id, - Name = author.Name, - FirstName = author.FirstName + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt }; await Send.OkAsync(responseDto, ct); diff --git a/ApiEfCoreLibrary/Endpoints/Login/DeleteLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/DeleteLoginEndpoint.cs index 0774f7a..cd80b49 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/DeleteLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/DeleteLoginEndpoint.cs @@ -1,34 +1,34 @@ -using ApiEfCoreLibrary.DTO.Author.Request; -using ApiEfCoreLibrary.DTO.Author.Response; +using ApiEfCoreLibrary.DTO.Login.Request; +using ApiEfCoreLibrary.DTO.Login.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -namespace ApiEfCoreLibrary.Endpoints.Author; +namespace ApiEfCoreLibrary.Endpoints.Login; -public class DeleteAuthorRequest +public class DeleteLoginRequest { public int Id { get; set; } } -public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint +public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint { public override void Configure() { - Delete("/api/authors/{@Id}", x => new {x.Id}); + Delete("/api/logins/{@Id}", x => new {x.Id}); AllowAnonymous(); } - public override async Task HandleAsync(DeleteAuthorRequest req, CancellationToken ct) + public override async Task HandleAsync(DeleteLoginRequest req, CancellationToken ct) { - var author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + var login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct); - if (author == null) + if (login == null) { await Send.NotFoundAsync(ct); return; } - database.Authors.Remove(author); + database.Logins.Remove(login); await database.SaveChangesAsync(ct); await Send.NoContentAsync(ct); diff --git a/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs index 9d47366..dd43175 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/GetAllLoginEndpoint.cs @@ -1,40 +1,31 @@ -using ApiEfCoreLibrary.DTO.Author.Response; +using ApiEfCoreLibrary.DTO.Login.Response; using ApiEfCoreLibrary.DTO.Book.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -namespace ApiEfCoreLibrary.Endpoints.Author; +namespace ApiEfCoreLibrary.Endpoints.Login; -public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest> +public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest> { public override void Configure() { - Get("/api/authors"); + Get("/api/logins"); AllowAnonymous(); } public override async Task HandleAsync(CancellationToken ct) { - var authors = await database.Authors - .Include(x => x.Books) - .Select(author => new GetAuthorDto() + var logins = await database.Logins + .Select(login => new GetLoginDto() { - Id = author.Id, - Name = author.Name, - FirstName = author.FirstName, - Books = author.Books.Select(book => new GetBookDto - { - Id = book.Id, - Title = book.Title, - AuthorId = book.AuthorId, - BookAuthorName = book.Author.Name, - BookAuthorFirstName = book.Author.FirstName, - ReleaseYear = book.ReleaseYear, - Isbn = book.Isbn - }).ToList() + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt }) .ToListAsync(ct); - await Send.OkAsync(authors, ct); + await Send.OkAsync(logins, ct); } } \ No newline at end of file diff --git a/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs index 3a6fc46..3e7125f 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/GetLoginEndpoint.cs @@ -1,51 +1,40 @@ -using ApiEfCoreLibrary.DTO.Author.Request; -using ApiEfCoreLibrary.DTO.Author.Response; -using ApiEfCoreLibrary.DTO.Book.Response; +using ApiEfCoreLibrary.DTO.Login.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -namespace ApiEfCoreLibrary.Endpoints.Author; +namespace ApiEfCoreLibrary.Endpoints.Login; -public class GetAuthorRequest +public class GetLoginRequest { public int Id { get; set; } } -public class GetLoginEndpoint(LibraryDbContext database) : Endpoint +public class GetLoginEndpoint(LibraryDbContext database) : Endpoint { public override void Configure() { - Get("/api/authors/{@Id}", x => new {x.Id}); + Get("/api/logins/{@Id}", x => new {x.Id}); AllowAnonymous(); } - public override async Task HandleAsync(GetAuthorRequest req, CancellationToken ct) + public override async Task HandleAsync(GetLoginRequest req, CancellationToken ct) { - var author = await database.Authors - .Include(x => x.Books) + var login = await database.Logins .SingleOrDefaultAsync(x => x.Id == req.Id, ct); - if (author == null) + if (login == null) { await Send.NotFoundAsync(ct); return; } - GetAuthorDto responseDto = new() + GetLoginDto responseDto = new() { - Id = author.Id, - Name = author.Name, - FirstName = author.FirstName, - Books = author.Books.Select(book => new GetBookDto - { - Id = book.Id, - Title = book.Title, - AuthorId = book.AuthorId, - BookAuthorName = book.Author.Name, - BookAuthorFirstName = book.Author.FirstName, - ReleaseYear = book.ReleaseYear, - Isbn = book.Isbn - }).ToList() + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt }; await Send.OkAsync(responseDto, ct); diff --git a/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs b/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs index 7ea0feb..17a3010 100644 --- a/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs +++ b/ApiEfCoreLibrary/Endpoints/Login/UpdateLoginEndpoint.cs @@ -1,38 +1,42 @@ -using ApiEfCoreLibrary.DTO.Author.Request; -using ApiEfCoreLibrary.DTO.Author.Response; +using ApiEfCoreLibrary.DTO.Login.Request; +using ApiEfCoreLibrary.DTO.Login.Response; using ApiEfCoreLibrary.DTO.Book.Response; using FastEndpoints; using Microsoft.EntityFrameworkCore; -namespace ApiEfCoreLibrary.Endpoints.Author; +namespace ApiEfCoreLibrary.Endpoints.Login; -public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint +public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint { public override void Configure() { - Put("/api/authors/{@Id}", x => new {x.Id}); + Put("/api/logins/{@Id}", x => new {x.Id}); AllowAnonymous(); } - public override async Task HandleAsync(UpdateAuthorDto req, CancellationToken ct) + public override async Task HandleAsync(UpdateLoginDto req, CancellationToken ct) { - var author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + var login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct); - if (author == null) + if (login == null) { await Send.NotFoundAsync(ct); return; } - author.Name = req.Name; - author.FirstName = req.FirstName; + login.Username = req.Username; + login.FullName = req.FullName; + login.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + req.Salt); + login.Salt = BCrypt.Net.BCrypt.GenerateSalt(24); await database.SaveChangesAsync(ct); - GetAuthorDto responseDto = new() + GetLoginDto responseDto = new() { - Id = author.Id, - Name = author.Name, - FirstName = author.FirstName, + Id = login.Id, + Username = login.Username, + FullName = login.FullName, + Password = login.Password, + Salt = login.Salt }; await Send.OkAsync(responseDto, ct);