Creating login's endpoints
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="FastEndpoints" Version="7.0.1" />
|
||||
<PackageReference Include="FastEndpoints.Security" Version="7.0.1" />
|
||||
<PackageReference Include="FastEndpoints.Swagger" Version="7.0.1" />
|
||||
|
@@ -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<CreateAuthorDto, GetAuthorDto>
|
||||
public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
|
||||
{
|
||||
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);
|
||||
|
@@ -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<DeleteAuthorRequest>
|
||||
public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint<DeleteLoginRequest>
|
||||
{
|
||||
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);
|
||||
|
@@ -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<List<GetAuthorDto>>
|
||||
public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest<List<GetLoginDto>>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@@ -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<GetAuthorRequest, GetAuthorDto>
|
||||
public class GetLoginEndpoint(LibraryDbContext database) : Endpoint<GetLoginRequest, GetLoginDto>
|
||||
{
|
||||
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);
|
||||
|
@@ -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<UpdateAuthorDto, GetAuthorDto>
|
||||
public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint<UpdateLoginDto, GetLoginDto>
|
||||
{
|
||||
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);
|
||||
|
Reference in New Issue
Block a user