From 165c9b9322f8df078bb29839eee428992acd14ab Mon Sep 17 00:00:00 2001 From: Cristiano Date: Thu, 20 Nov 2025 15:38:53 +0100 Subject: [PATCH] Refactored User --- .../Endpoints/Users/ConnectUserEndpoint.cs | 6 +++-- .../Endpoints/Users/CreateUserEndpoint.cs | 21 +++++----------- .../Endpoints/Users/DeleteUserEndpoint.cs | 9 +++---- .../Endpoints/Users/GetAllUsersEndpoint.cs | 17 +++---------- PyroFetes/Endpoints/Users/GetUserEndpoint.cs | 21 ++++++---------- .../Users/PatchUserPasswordEndpoint.cs | 24 ++++++++----------- .../Endpoints/Users/UpdateUserEndpoint.cs | 23 +++++++----------- PyroFetes/Program.cs | 1 + PyroFetes/Repositories/UsersRepository.cs | 5 ++++ .../Specifications/Users/GetUserByIdSpec.cs | 13 ++++++++++ .../Specifications/Users/GetUserByNameSpec.cs | 13 ++++++++++ 11 files changed, 76 insertions(+), 77 deletions(-) create mode 100644 PyroFetes/Repositories/UsersRepository.cs create mode 100644 PyroFetes/Specifications/Users/GetUserByIdSpec.cs create mode 100644 PyroFetes/Specifications/Users/GetUserByNameSpec.cs diff --git a/PyroFetes/Endpoints/Users/ConnectUserEndpoint.cs b/PyroFetes/Endpoints/Users/ConnectUserEndpoint.cs index 7ddff69..916f41e 100644 --- a/PyroFetes/Endpoints/Users/ConnectUserEndpoint.cs +++ b/PyroFetes/Endpoints/Users/ConnectUserEndpoint.cs @@ -4,10 +4,12 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.User.Request; using PyroFetes.DTO.User.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Users; namespace PyroFetes.Endpoints.Users; -public class ConnectUserEndpoint(PyroFetesDbContext database) : Endpoint +public class ConnectUserEndpoint(UsersRepository usersRepository) : Endpoint { public override void Configure() { @@ -17,7 +19,7 @@ public class ConnectUserEndpoint(PyroFetesDbContext database) : Endpoint x.Name == req.Name, ct); + User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct); if (user == null) { diff --git a/PyroFetes/Endpoints/Users/CreateUserEndpoint.cs b/PyroFetes/Endpoints/Users/CreateUserEndpoint.cs index a923b4e..1c2d261 100644 --- a/PyroFetes/Endpoints/Users/CreateUserEndpoint.cs +++ b/PyroFetes/Endpoints/Users/CreateUserEndpoint.cs @@ -3,10 +3,13 @@ using PasswordGenerator; using PyroFetes.DTO.User.Request; using PyroFetes.DTO.User.Response; using PyroFetes.Models; +using PyroFetes.Repositories; namespace PyroFetes.Endpoints.Users; -public class CreateUserEndpoint(PyroFetesDbContext database) : Endpoint +public class CreateUserEndpoint( + UsersRepository usersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -27,20 +30,8 @@ public class CreateUserEndpoint(PyroFetesDbContext database) : Endpoint(user), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Users/DeleteUserEndpoint.cs b/PyroFetes/Endpoints/Users/DeleteUserEndpoint.cs index d5b0b62..d6df7d3 100644 --- a/PyroFetes/Endpoints/Users/DeleteUserEndpoint.cs +++ b/PyroFetes/Endpoints/Users/DeleteUserEndpoint.cs @@ -1,6 +1,8 @@ using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Users; namespace PyroFetes.Endpoints.Users; @@ -9,7 +11,7 @@ public class DeleteUserRequest public int Id { get; set; } } -public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint +public class DeleteUserEndpoint(UsersRepository usersRepository) : Endpoint { public override void Configure() { @@ -19,7 +21,7 @@ public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct); if (user == null) { @@ -27,8 +29,7 @@ public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint> +public class GetAllUsersEndpoint(UsersRepository usersRepository) : EndpointWithoutRequest> { public override void Configure() { @@ -14,18 +15,6 @@ public class GetAllUsersEndpoint(PyroFetesDbContext database) : EndpointWithoutR public override async Task HandleAsync(CancellationToken ct) { - List 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); + await Send.OkAsync(await usersRepository.ProjectToListAsync(ct), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Users/GetUserEndpoint.cs b/PyroFetes/Endpoints/Users/GetUserEndpoint.cs index 3bcc589..222079d 100644 --- a/PyroFetes/Endpoints/Users/GetUserEndpoint.cs +++ b/PyroFetes/Endpoints/Users/GetUserEndpoint.cs @@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.User.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Users; namespace PyroFetes.Endpoints.Users; @@ -10,7 +12,9 @@ public class GetUserRequest public int Id { get; set; } } -public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint +public class GetUserEndpoint( + UsersRepository usersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -20,8 +24,7 @@ public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); + User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct); if (user == null) { @@ -29,16 +32,6 @@ public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint(user), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Users/PatchUserPasswordEndpoint.cs b/PyroFetes/Endpoints/Users/PatchUserPasswordEndpoint.cs index 17e5af8..6f7491e 100644 --- a/PyroFetes/Endpoints/Users/PatchUserPasswordEndpoint.cs +++ b/PyroFetes/Endpoints/Users/PatchUserPasswordEndpoint.cs @@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.User.Request; using PyroFetes.DTO.User.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Users; namespace PyroFetes.Endpoints.Users; -public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint +public class PatchUserPasswordEndpoint( + UsersRepository usersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -16,7 +20,8 @@ public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint

x.Id == req.Id, ct); + User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct); + if (user == null) { await Send.NotFoundAsync(ct); @@ -24,17 +29,8 @@ public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint

(user), ct); } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Users/UpdateUserEndpoint.cs b/PyroFetes/Endpoints/Users/UpdateUserEndpoint.cs index 20bb3d4..cf5f8d6 100644 --- a/PyroFetes/Endpoints/Users/UpdateUserEndpoint.cs +++ b/PyroFetes/Endpoints/Users/UpdateUserEndpoint.cs @@ -4,10 +4,14 @@ using PasswordGenerator; using PyroFetes.DTO.User.Request; using PyroFetes.DTO.User.Response; using PyroFetes.Models; +using PyroFetes.Repositories; +using PyroFetes.Specifications.Users; namespace PyroFetes.Endpoints.Users; -public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint +public class UpdateUserEndpoint( + UsersRepository usersRepository, + AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -17,8 +21,8 @@ public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint x.Id == req.Id, ct); - User? ckeckName = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct); + User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct); + User? ckeckName = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct); if (user == null) { @@ -39,18 +43,9 @@ public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint(user), ct); } } \ No newline at end of file diff --git a/PyroFetes/Program.cs b/PyroFetes/Program.cs index 102bf2d..21f864b 100644 --- a/PyroFetes/Program.cs +++ b/PyroFetes/Program.cs @@ -30,6 +30,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); MapperConfiguration mappingConfig = new(mc => { diff --git a/PyroFetes/Repositories/UsersRepository.cs b/PyroFetes/Repositories/UsersRepository.cs new file mode 100644 index 0000000..b943703 --- /dev/null +++ b/PyroFetes/Repositories/UsersRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class UsersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/Users/GetUserByIdSpec.cs b/PyroFetes/Specifications/Users/GetUserByIdSpec.cs new file mode 100644 index 0000000..db5c0be --- /dev/null +++ b/PyroFetes/Specifications/Users/GetUserByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Users; + +public sealed class GetUserByIdSpec : Specification +{ + public GetUserByIdSpec(int userId) + { + Query + .Where(x => x.Id == userId); + } +} \ No newline at end of file diff --git a/PyroFetes/Specifications/Users/GetUserByNameSpec.cs b/PyroFetes/Specifications/Users/GetUserByNameSpec.cs new file mode 100644 index 0000000..daa75ad --- /dev/null +++ b/PyroFetes/Specifications/Users/GetUserByNameSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.Users; + +public sealed class GetUserByNameSpec : Specification +{ + public GetUserByNameSpec(string userName) + { + Query + .Where(x=> x.Name == userName); + } +} \ No newline at end of file