Refactored User
This commit is contained in:
@@ -4,10 +4,12 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using PyroFetes.DTO.User.Request;
|
using PyroFetes.DTO.User.Request;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
public class ConnectUserEndpoint(PyroFetesDbContext database) : Endpoint<ConnectUserDto, GetTokenDto>
|
public class ConnectUserEndpoint(UsersRepository usersRepository) : Endpoint<ConnectUserDto, GetTokenDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -17,7 +19,7 @@ public class ConnectUserEndpoint(PyroFetesDbContext database) : Endpoint<Connect
|
|||||||
|
|
||||||
public override async Task HandleAsync(ConnectUserDto req, CancellationToken ct)
|
public override async Task HandleAsync(ConnectUserDto req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
User? user = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,10 +3,13 @@ using PasswordGenerator;
|
|||||||
using PyroFetes.DTO.User.Request;
|
using PyroFetes.DTO.User.Request;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
public class CreateUserEndpoint(PyroFetesDbContext database) : Endpoint<CreateUserDto, GetUserDto>
|
public class CreateUserEndpoint(
|
||||||
|
UsersRepository usersRepository,
|
||||||
|
AutoMapper.IMapper mapper) : Endpoint<CreateUserDto, GetUserDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -27,20 +30,8 @@ public class CreateUserEndpoint(PyroFetesDbContext database) : Endpoint<CreateUs
|
|||||||
Fonction = req.Fonction
|
Fonction = req.Fonction
|
||||||
};
|
};
|
||||||
|
|
||||||
database.Users.Add(user);
|
await usersRepository.AddAsync(user, ct);
|
||||||
|
|
||||||
await database.SaveChangesAsync(ct);
|
await Send.OkAsync(mapper.Map<GetUserDto>(user), 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
@@ -9,7 +11,7 @@ public class DeleteUserRequest
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint<DeleteUserRequest>
|
public class DeleteUserEndpoint(UsersRepository usersRepository) : Endpoint<DeleteUserRequest>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -19,7 +21,7 @@ public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint<DeleteUs
|
|||||||
|
|
||||||
public override async Task HandleAsync(DeleteUserRequest req, CancellationToken ct)
|
public override async Task HandleAsync(DeleteUserRequest req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
@@ -27,8 +29,7 @@ public class DeleteUserEndpoint(PyroFetesDbContext database) : Endpoint<DeleteUs
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
database.Users.Remove(user);
|
await usersRepository.DeleteAsync(user, ct);
|
||||||
await database.SaveChangesAsync(ct);
|
|
||||||
|
|
||||||
await Send.NoContentAsync(ct);
|
await Send.NoContentAsync(ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
public class GetAllUsersEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetUserDto>>
|
public class GetAllUsersEndpoint(UsersRepository usersRepository) : EndpointWithoutRequest<List<GetUserDto>>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -14,18 +15,6 @@ public class GetAllUsersEndpoint(PyroFetesDbContext database) : EndpointWithoutR
|
|||||||
|
|
||||||
public override async Task HandleAsync(CancellationToken ct)
|
public override async Task HandleAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
List<GetUserDto> users = await database.Users
|
await Send.OkAsync(await usersRepository.ProjectToListAsync<GetUserDto>(ct), ct);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
@@ -10,7 +12,9 @@ public class GetUserRequest
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint<GetUserRequest, GetUserDto>
|
public class GetUserEndpoint(
|
||||||
|
UsersRepository usersRepository,
|
||||||
|
AutoMapper.IMapper mapper) : Endpoint<GetUserRequest, GetUserDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -20,8 +24,7 @@ public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint<GetUserRequ
|
|||||||
|
|
||||||
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
|
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
User? user = await database.Users
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||||
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
@@ -29,16 +32,6 @@ public class GetUserEndpoint(PyroFetesDbContext database) : Endpoint<GetUserRequ
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetUserDto responseDto = new()
|
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||||
{
|
|
||||||
Id = user.Id,
|
|
||||||
Name = user.Name,
|
|
||||||
Password = user.Password,
|
|
||||||
Salt = user.Salt,
|
|
||||||
Email = user.Email,
|
|
||||||
Fonction = user.Fonction
|
|
||||||
};
|
|
||||||
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,14 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using PyroFetes.DTO.User.Request;
|
using PyroFetes.DTO.User.Request;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint<PatchUserPasswordDto, GetUserDto>
|
public class PatchUserPasswordEndpoint(
|
||||||
|
UsersRepository usersRepository,
|
||||||
|
AutoMapper.IMapper mapper) : Endpoint<PatchUserPasswordDto, GetUserDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -16,7 +20,8 @@ public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint<P
|
|||||||
|
|
||||||
public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct)
|
public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
await Send.NotFoundAsync(ct);
|
await Send.NotFoundAsync(ct);
|
||||||
@@ -24,17 +29,8 @@ public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint<P
|
|||||||
}
|
}
|
||||||
|
|
||||||
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + user.Salt);
|
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + user.Salt);
|
||||||
await database.SaveChangesAsync(ct);
|
await usersRepository.UpdateAsync(user, ct);
|
||||||
|
|
||||||
GetUserDto responseDto = new()
|
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||||
{
|
|
||||||
Id = user.Id,
|
|
||||||
Name = user.Name,
|
|
||||||
Password = user.Password,
|
|
||||||
Salt = user.Salt,
|
|
||||||
Email = user.Email,
|
|
||||||
Fonction = user.Fonction
|
|
||||||
};
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,10 +4,14 @@ using PasswordGenerator;
|
|||||||
using PyroFetes.DTO.User.Request;
|
using PyroFetes.DTO.User.Request;
|
||||||
using PyroFetes.DTO.User.Response;
|
using PyroFetes.DTO.User.Response;
|
||||||
using PyroFetes.Models;
|
using PyroFetes.Models;
|
||||||
|
using PyroFetes.Repositories;
|
||||||
|
using PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
namespace PyroFetes.Endpoints.Users;
|
namespace PyroFetes.Endpoints.Users;
|
||||||
|
|
||||||
public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint<UpdateUserDto, GetUserDto>
|
public class UpdateUserEndpoint(
|
||||||
|
UsersRepository usersRepository,
|
||||||
|
AutoMapper.IMapper mapper) : Endpoint<UpdateUserDto, GetUserDto>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
@@ -17,8 +21,8 @@ public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint<UpdateUs
|
|||||||
|
|
||||||
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
|
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
|
||||||
{
|
{
|
||||||
User? user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
User? user = await usersRepository.FirstOrDefaultAsync(new GetUserByIdSpec(req.Id), ct);
|
||||||
User? ckeckName = await database.Users.SingleOrDefaultAsync(x => x.Name == req.Name, ct);
|
User? ckeckName = await usersRepository.FirstOrDefaultAsync(new GetUserByNameSpec(req.Name!), ct);
|
||||||
|
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
@@ -39,18 +43,9 @@ public class UpdateUserEndpoint(PyroFetesDbContext database) : Endpoint<UpdateUs
|
|||||||
user.Salt = salt;
|
user.Salt = salt;
|
||||||
user.Email = req.Email;
|
user.Email = req.Email;
|
||||||
user.Fonction = req.Fonction;
|
user.Fonction = req.Fonction;
|
||||||
await database.SaveChangesAsync(ct);
|
|
||||||
|
|
||||||
GetUserDto responseDto = new()
|
await usersRepository.UpdateAsync(user, ct);
|
||||||
{
|
|
||||||
Id = user.Id,
|
|
||||||
Name = user.Name,
|
|
||||||
Password = user.Password,
|
|
||||||
Salt = user.Salt,
|
|
||||||
Email = user.Email,
|
|
||||||
Fonction = user.Fonction
|
|
||||||
};
|
|
||||||
|
|
||||||
await Send.OkAsync(responseDto, ct);
|
await Send.OkAsync(mapper.Map<GetUserDto>(user), ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,7 @@ builder.Services.AddScoped<QuotationProductsRepository>();
|
|||||||
builder.Services.AddScoped<QuotationsRepository>();
|
builder.Services.AddScoped<QuotationsRepository>();
|
||||||
builder.Services.AddScoped<SuppliersRepository>();
|
builder.Services.AddScoped<SuppliersRepository>();
|
||||||
builder.Services.AddScoped<SettingsRepository>();
|
builder.Services.AddScoped<SettingsRepository>();
|
||||||
|
builder.Services.AddScoped<UsersRepository>();
|
||||||
|
|
||||||
MapperConfiguration mappingConfig = new(mc =>
|
MapperConfiguration mappingConfig = new(mc =>
|
||||||
{
|
{
|
||||||
|
|||||||
5
PyroFetes/Repositories/UsersRepository.cs
Normal file
5
PyroFetes/Repositories/UsersRepository.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
using PyroFetes.Models;
|
||||||
|
|
||||||
|
namespace PyroFetes.Repositories;
|
||||||
|
|
||||||
|
public class UsersRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository<User>(pyrofetesContext, mapper);
|
||||||
13
PyroFetes/Specifications/Users/GetUserByIdSpec.cs
Normal file
13
PyroFetes/Specifications/Users/GetUserByIdSpec.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Ardalis.Specification;
|
||||||
|
using PyroFetes.Models;
|
||||||
|
|
||||||
|
namespace PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
|
public sealed class GetUserByIdSpec : Specification<User>
|
||||||
|
{
|
||||||
|
public GetUserByIdSpec(int userId)
|
||||||
|
{
|
||||||
|
Query
|
||||||
|
.Where(x => x.Id == userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
PyroFetes/Specifications/Users/GetUserByNameSpec.cs
Normal file
13
PyroFetes/Specifications/Users/GetUserByNameSpec.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Ardalis.Specification;
|
||||||
|
using PyroFetes.Models;
|
||||||
|
|
||||||
|
namespace PyroFetes.Specifications.Users;
|
||||||
|
|
||||||
|
public sealed class GetUserByNameSpec : Specification<User>
|
||||||
|
{
|
||||||
|
public GetUserByNameSpec(string userName)
|
||||||
|
{
|
||||||
|
Query
|
||||||
|
.Where(x=> x.Name == userName);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user