Creating login's endpoints

This commit is contained in:
2025-10-13 14:52:12 +02:00
parent 08b862318d
commit 7a3fb74831
6 changed files with 74 additions and 84 deletions

View File

@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FastEndpoints" Version="7.0.1" /> <PackageReference Include="FastEndpoints" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Security" Version="7.0.1" /> <PackageReference Include="FastEndpoints.Security" Version="7.0.1" />
<PackageReference Include="FastEndpoints.Swagger" Version="7.0.1" /> <PackageReference Include="FastEndpoints.Swagger" Version="7.0.1" />

View File

@@ -1,35 +1,40 @@
using ApiEfCoreLibrary.DTO.Author.Request; using ApiEfCoreLibrary.DTO.Login.Request;
using ApiEfCoreLibrary.DTO.Author.Response; using ApiEfCoreLibrary.DTO.Login.Response;
using BCrypt.Net;
namespace ApiEfCoreLibrary.Endpoints.Author; namespace ApiEfCoreLibrary.Endpoints.Login;
using FastEndpoints; using FastEndpoints;
public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint<CreateAuthorDto, GetAuthorDto> public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
{ {
public override void Configure() public override void Configure()
{ {
Post("/api/authors"); Post("/api/logins");
AllowAnonymous(); 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, Username = req.Username,
FirstName = req.FirstName 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); await database.SaveChangesAsync(ct);
// Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400); // Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400);
GetAuthorDto responseDto = new() GetLoginDto responseDto = new()
{ {
Id = author.Id, Id = login.Id,
Name = author.Name, Username = login.Username,
FirstName = author.FirstName FullName = login.FullName,
Password = login.Password,
Salt = login.Salt
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);

View File

@@ -1,34 +1,34 @@
using ApiEfCoreLibrary.DTO.Author.Request; using ApiEfCoreLibrary.DTO.Login.Request;
using ApiEfCoreLibrary.DTO.Author.Response; using ApiEfCoreLibrary.DTO.Login.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author; namespace ApiEfCoreLibrary.Endpoints.Login;
public class DeleteAuthorRequest public class DeleteLoginRequest
{ {
public int Id { get; set; } public int Id { get; set; }
} }
public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint<DeleteAuthorRequest> public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint<DeleteLoginRequest>
{ {
public override void Configure() public override void Configure()
{ {
Delete("/api/authors/{@Id}", x => new {x.Id}); Delete("/api/logins/{@Id}", x => new {x.Id});
AllowAnonymous(); 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); await Send.NotFoundAsync(ct);
return; return;
} }
database.Authors.Remove(author); database.Logins.Remove(login);
await database.SaveChangesAsync(ct); await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);

View File

@@ -1,40 +1,31 @@
using ApiEfCoreLibrary.DTO.Author.Response; using ApiEfCoreLibrary.DTO.Login.Response;
using ApiEfCoreLibrary.DTO.Book.Response; using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author; namespace ApiEfCoreLibrary.Endpoints.Login;
public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest<List<GetAuthorDto>> public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest<List<GetLoginDto>>
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/authors"); Get("/api/logins");
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
var authors = await database.Authors var logins = await database.Logins
.Include(x => x.Books) .Select(login => new GetLoginDto()
.Select(author => new GetAuthorDto()
{ {
Id = author.Id, Id = login.Id,
Name = author.Name, Username = login.Username,
FirstName = author.FirstName, FullName = login.FullName,
Books = author.Books.Select(book => new GetBookDto Password = login.Password,
{ Salt = login.Salt
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
BookAuthorName = book.Author.Name,
BookAuthorFirstName = book.Author.FirstName,
ReleaseYear = book.ReleaseYear,
Isbn = book.Isbn
}).ToList()
}) })
.ToListAsync(ct); .ToListAsync(ct);
await Send.OkAsync(authors, ct); await Send.OkAsync(logins, ct);
} }
} }

View File

@@ -1,51 +1,40 @@
using ApiEfCoreLibrary.DTO.Author.Request; using ApiEfCoreLibrary.DTO.Login.Response;
using ApiEfCoreLibrary.DTO.Author.Response;
using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author; namespace ApiEfCoreLibrary.Endpoints.Login;
public class GetAuthorRequest public class GetLoginRequest
{ {
public int Id { get; set; } public int Id { get; set; }
} }
public class GetLoginEndpoint(LibraryDbContext database) : Endpoint<GetAuthorRequest, GetAuthorDto> public class GetLoginEndpoint(LibraryDbContext database) : Endpoint<GetLoginRequest, GetLoginDto>
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/authors/{@Id}", x => new {x.Id}); Get("/api/logins/{@Id}", x => new {x.Id});
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(GetAuthorRequest req, CancellationToken ct) public override async Task HandleAsync(GetLoginRequest req, CancellationToken ct)
{ {
var author = await database.Authors var login = await database.Logins
.Include(x => x.Books)
.SingleOrDefaultAsync(x => x.Id == req.Id, ct); .SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (author == null) if (login == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
GetAuthorDto responseDto = new() GetLoginDto responseDto = new()
{ {
Id = author.Id, Id = login.Id,
Name = author.Name, Username = login.Username,
FirstName = author.FirstName, FullName = login.FullName,
Books = author.Books.Select(book => new GetBookDto Password = login.Password,
{ Salt = login.Salt
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
BookAuthorName = book.Author.Name,
BookAuthorFirstName = book.Author.FirstName,
ReleaseYear = book.ReleaseYear,
Isbn = book.Isbn
}).ToList()
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);

View File

@@ -1,38 +1,42 @@
using ApiEfCoreLibrary.DTO.Author.Request; using ApiEfCoreLibrary.DTO.Login.Request;
using ApiEfCoreLibrary.DTO.Author.Response; using ApiEfCoreLibrary.DTO.Login.Response;
using ApiEfCoreLibrary.DTO.Book.Response; using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints; using FastEndpoints;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author; namespace ApiEfCoreLibrary.Endpoints.Login;
public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint<UpdateAuthorDto, GetAuthorDto> public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint<UpdateLoginDto, GetLoginDto>
{ {
public override void Configure() public override void Configure()
{ {
Put("/api/authors/{@Id}", x => new {x.Id}); Put("/api/logins/{@Id}", x => new {x.Id});
AllowAnonymous(); 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); await Send.NotFoundAsync(ct);
return; return;
} }
author.Name = req.Name; login.Username = req.Username;
author.FirstName = req.FirstName; 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); await database.SaveChangesAsync(ct);
GetAuthorDto responseDto = new() GetLoginDto responseDto = new()
{ {
Id = author.Id, Id = login.Id,
Name = author.Name, Username = login.Username,
FirstName = author.FirstName, FullName = login.FullName,
Password = login.Password,
Salt = login.Salt
}; };
await Send.OkAsync(responseDto, ct); await Send.OkAsync(responseDto, ct);