suppr all 'var' and Models.xxx. And added 's' at the end of all directories endpoints

This commit is contained in:
2025-11-17 20:49:12 +01:00
parent 20bbccf883
commit 6dba61f742
45 changed files with 141 additions and 114 deletions

View File

@@ -0,0 +1,49 @@
using FastEndpoints;
using FastEndpoints.Security;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.User.Request;
using PyroFetes.DTO.User.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class ConnectUserEndpoint(PyroFetesDbContext database) : Endpoint<ConnectUserDto, GetTokenDto>
{
public override void Configure()
{
Post("/api/users/connect");
AllowAnonymous();
}
public override async Task HandleAsync(ConnectUserDto req, CancellationToken ct)
{
User? user = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
if (user == 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())!);
});
GetTokenDto responseDto = new()
{
Token = jwtToken
};
await Send.OkAsync(responseDto, ct);
}
else await Send.UnauthorizedAsync(ct);
}
}

View File

@@ -0,0 +1,46 @@
using FastEndpoints;
using PasswordGenerator;
using PyroFetes.DTO.User.Request;
using PyroFetes.DTO.User.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class CreateUserEndpoint(PyroFetesDbContext database) : Endpoint<CreateUserDto, GetUserDto>
{
public override void Configure()
{
Post("/api/users");
AllowAnonymous();
}
public override async Task HandleAsync(CreateUserDto req, CancellationToken ct)
{
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
User user = new User()
{
Name = req.Name,
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
Salt = salt,
Email = req.Email,
Fonction = req.Fonction
};
database.Users.Add(user);
await database.SaveChangesAsync(ct);
GetUserDto responseDto = new()
{
Id = user.Id,
Name = user.Name,
Password = user.Password,
Salt = user.Salt,
Email = user.Email,
Fonction = user.Fonction
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,34 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class DeleteUserRequest
{
public int Id { get; set; }
}
public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint<DeleteUserRequest>
{
public override void Configure()
{
Delete("/api/users/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(DeleteUserRequest req, CancellationToken ct)
{
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (user == null)
{
await Send.NotFoundAsync(ct);
return;
}
database.Users.Remove(user);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.User.Response;
namespace PyroFetes.Endpoints.Users;
public class GetAllUsersEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetUserDto>>
{
public override void Configure()
{
Get("/api/users");
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetUserDto> users = await database.Users
.Select(users => new GetUserDto()
{
Id = users.Id,
Name = users.Name,
Password = users.Password,
Salt = users.Salt,
Email = users.Email,
Fonction = users.Fonction
})
.ToListAsync(ct);
await Send.OkAsync(users, ct);
}
}

View File

@@ -0,0 +1,43 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.User.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class GetUserRequest
{
public int Id { get; set; }
}
public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint<GetUserRequest, GetUserDto>
{
public override void Configure()
{
Get("/api/users/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
{
User? user = await database.Users
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (user == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetUserDto responseDto = new()
{
Id = user.Id,
Name = user.Name,
Password = user.Password,
Salt = user.Salt,
Email = user.Email,
Fonction = user.Fonction
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,40 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.User.Request;
using PyroFetes.DTO.User.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint<PatchUserPasswordDto, GetUserDto>
{
public override void Configure()
{
Patch("/api/users/{@Id}/Password", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct)
{
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (user == null)
{
await Send.NotFoundAsync(ct);
return;
}
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + user.Salt);
await database.SaveChangesAsync(ct);
GetUserDto responseDto = new()
{
Id = user.Id,
Name = user.Name,
Password = user.Password,
Salt = user.Salt,
Email = user.Email,
Fonction = user.Fonction
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,55 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PasswordGenerator;
using PyroFetes.DTO.User.Request;
using PyroFetes.DTO.User.Response;
using PyroFetes.Models;
namespace PyroFetes.Endpoints.Users;
public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint<UpdateUserDto, GetUserDto>
{
public override void Configure()
{
Put("/api/users/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
{
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
User? ckeckName = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
if (user == null)
{
await Send.NotFoundAsync(ct);
return;
}
if (ckeckName != null)
{
await Send.StringAsync("Ce nom d'utilisateur existe déjà.",409, 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 database.SaveChangesAsync(ct);
GetUserDto responseDto = new()
{
Id = user.Id,
Name = user.Name,
Password = user.Password,
Salt = user.Salt,
Email = user.Email,
Fonction = user.Fonction
};
await Send.OkAsync(responseDto, ct);
}
}