Refactor all code
This commit is contained in:
@@ -19,31 +19,30 @@ public class ConnectUserEndpoint(UsersRepository usersRepository) : Endpoint<Con
|
||||
|
||||
public override async Task HandleAsync(ConnectUserDto req, CancellationToken ct)
|
||||
{
|
||||
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
|
||||
if (user == null)
|
||||
if (user is null)
|
||||
{
|
||||
await Send.UnauthorizedAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (BCrypt.Net.BCrypt.Verify(req.Password + user.Salt, user.Password))
|
||||
{
|
||||
string jwtToken = JwtBearer.CreateToken(
|
||||
o =>
|
||||
{
|
||||
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
||||
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||
if (user.Fonction != null) o.User.Roles.Add(user.Fonction);
|
||||
o.User.Claims.Add(("Name", user.Name)!);
|
||||
o.User.Claims.Add(("Id", user.Id.ToString())!);
|
||||
});
|
||||
string jwtToken = JwtBearer.CreateToken(o =>
|
||||
{
|
||||
o.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong";
|
||||
o.ExpireAt = DateTime.UtcNow.AddMinutes(15);
|
||||
if (user.Fonction is not null) o.User.Roles.Add(user.Fonction);
|
||||
o.User.Claims.Add(("Name", user.Name)!);
|
||||
o.User.Claims.Add(("Id", user.Id.ToString())!);
|
||||
});
|
||||
|
||||
GetTokenDto responseDto = new()
|
||||
{
|
||||
Token = jwtToken
|
||||
};
|
||||
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
else await Send.UnauthorizedAsync(ct);
|
||||
|
||||
@@ -8,9 +8,7 @@ using PyroFetes.Specifications.Users;
|
||||
|
||||
namespace PyroFetes.Endpoints.Users;
|
||||
|
||||
public class CreateUserEndpoint(
|
||||
UsersRepository usersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreateUserDto, GetUserDto>
|
||||
public class CreateUserEndpoint(UsersRepository usersRepository) : Endpoint<CreateUserDto, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -20,17 +18,17 @@ public class CreateUserEndpoint(
|
||||
|
||||
public override async Task HandleAsync(CreateUserDto req, CancellationToken ct)
|
||||
{
|
||||
User? ckeckName = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
|
||||
if (ckeckName != null)
|
||||
User? ckeckName = await usersRepository.SingleOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
|
||||
if (ckeckName is not null)
|
||||
{
|
||||
await Send.StringAsync("Ce nom d'utilisateur existe déjà.",409, cancellation: ct);
|
||||
await Send.StringAsync("Ce nom d'utilisateur existe déjà.", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||
|
||||
User user = new User()
|
||||
|
||||
User user = new()
|
||||
{
|
||||
Name = req.Name,
|
||||
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
|
||||
@@ -38,9 +36,8 @@ public class CreateUserEndpoint(
|
||||
Email = req.Email,
|
||||
Fonction = req.Fonction
|
||||
};
|
||||
|
||||
|
||||
await usersRepository.AddAsync(user, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Users;
|
||||
@@ -15,22 +14,21 @@ public class DeleteUserEndpoint(UsersRepository usersRepository) : Endpoint<Dele
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/users/{@Id}", x => new {x.Id});
|
||||
Delete("/users/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteUserRequest req, CancellationToken ct)
|
||||
{
|
||||
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
|
||||
if (user == null)
|
||||
if (user is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await usersRepository.DeleteAsync(user, ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.User.Response;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.User.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
@@ -12,26 +11,24 @@ public class GetUserRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetUserEndpoint(
|
||||
UsersRepository usersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<GetUserRequest, GetUserDto>
|
||||
public class GetUserEndpoint(UsersRepository usersRepository, AutoMapper.IMapper mapper) : Endpoint<GetUserRequest, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/users/{@Id}", x => new {x.Id});
|
||||
Get("/users/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
|
||||
{
|
||||
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
|
||||
if (user == null)
|
||||
if (user is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.User.Request;
|
||||
using PyroFetes.DTO.User.Response;
|
||||
using PyroFetes.Models;
|
||||
@@ -8,9 +7,7 @@ using PyroFetes.Specifications.Users;
|
||||
|
||||
namespace PyroFetes.Endpoints.Users;
|
||||
|
||||
public class PatchUserPasswordEndpoint(
|
||||
UsersRepository usersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<PatchUserPasswordDto, GetUserDto>
|
||||
public class PatchUserPasswordEndpoint(UsersRepository usersRepository,AutoMapper.IMapper mapper) : Endpoint<PatchUserPasswordDto, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -20,9 +17,9 @@ public class PatchUserPasswordEndpoint(
|
||||
|
||||
public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct)
|
||||
{
|
||||
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
|
||||
if (user == null)
|
||||
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
@@ -30,7 +27,7 @@ public class PatchUserPasswordEndpoint(
|
||||
|
||||
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + user.Salt);
|
||||
await usersRepository.UpdateAsync(user, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PasswordGenerator;
|
||||
using PyroFetes.DTO.User.Request;
|
||||
using PyroFetes.DTO.User.Response;
|
||||
@@ -9,43 +8,41 @@ using PyroFetes.Specifications.Users;
|
||||
|
||||
namespace PyroFetes.Endpoints.Users;
|
||||
|
||||
public class UpdateUserEndpoint(
|
||||
UsersRepository usersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<UpdateUserDto, GetUserDto>
|
||||
public class UpdateUserEndpoint(UsersRepository usersRepository) : Endpoint<UpdateUserDto, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/users/{@Id}", x => new {x.Id});
|
||||
Put("/users/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
|
||||
{
|
||||
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
User? ckeckName = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||
User? ckeckName = await usersRepository.SingleOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||
|
||||
if (user == null)
|
||||
if (user is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ckeckName != null && ckeckName.Id != user.Id)
|
||||
|
||||
if (ckeckName is not null && ckeckName.Id != user.Id)
|
||||
{
|
||||
await Send.StringAsync("Ce nom d'utilisateur existe déjà.",409, cancellation: ct);
|
||||
await Send.StringAsync("Ce nom d'utilisateur existe déjà.", 400, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
||||
|
||||
|
||||
user.Name = req.Name;
|
||||
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt);
|
||||
user.Salt = salt;
|
||||
user.Email = req.Email;
|
||||
user.Fonction = req.Fonction;
|
||||
|
||||
|
||||
await usersRepository.UpdateAsync(user, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user