diff --git a/ldap-cesi/Configurations/Conf.cs b/ldap-cesi/Configurations/Conf.cs index e4f5b10..64e7298 100644 --- a/ldap-cesi/Configurations/Conf.cs +++ b/ldap-cesi/Configurations/Conf.cs @@ -27,6 +27,7 @@ public static class Conf builder.AddSerilog(); builder.AddJwt(); builder.Services.AddAutoMapper(typeof(Program)); + } public static void AddRepositories(this WebApplicationBuilder builder) diff --git a/ldap-cesi/Models/ResponseDataModel.cs b/ldap-cesi/Models/ResponseDataModel.cs index eb66c0f..1b415c7 100644 --- a/ldap-cesi/Models/ResponseDataModel.cs +++ b/ldap-cesi/Models/ResponseDataModel.cs @@ -3,5 +3,4 @@ namespace ldap_cesi.Models; public class ResponseDataModel : ResponseModel, IResponseDataModel where T : class { public T Data { get; set; } = null!; - public string Token { get; set; } } \ No newline at end of file diff --git a/ldap-cesi/Models/ResponseModel.cs b/ldap-cesi/Models/ResponseModel.cs index 57393de..2173782 100644 --- a/ldap-cesi/Models/ResponseModel.cs +++ b/ldap-cesi/Models/ResponseModel.cs @@ -4,7 +4,7 @@ public class ResponseModel : IResponseModel { public bool Success { get; set; } public string? Message { get; set; } - public string? TokenJWT { get; set; } + public string? Token { get; set; } public int StatusCode { get; set; } } \ No newline at end of file diff --git a/ldap-cesi/Services/Interfaces/IUtilisateurService.cs b/ldap-cesi/Services/Interfaces/IUtilisateurService.cs index fb93caf..88d9fa2 100644 --- a/ldap-cesi/Services/Interfaces/IUtilisateurService.cs +++ b/ldap-cesi/Services/Interfaces/IUtilisateurService.cs @@ -7,7 +7,7 @@ namespace ldap_cesi.Services.Interfaces; public interface IUtilisateurService { - Task>> GetAll(); - Task> GetById(int id); + Task>> GetAll(); + Task> GetById(int id); Task> Login(UtilisateurLoginDto utilisateurInput); } \ No newline at end of file diff --git a/ldap-cesi/Services/UtilisateurService.cs b/ldap-cesi/Services/UtilisateurService.cs index bc7a00f..b908c5a 100644 --- a/ldap-cesi/Services/UtilisateurService.cs +++ b/ldap-cesi/Services/UtilisateurService.cs @@ -5,6 +5,7 @@ using ldap_cesi.Entities; using ldap_cesi.Models; using ldap_cesi.Repository.Services; using ldap_cesi.Services.Interfaces; +using ldap_cesi.Validator.Utilisateur; namespace ldap_cesi.Services; @@ -20,30 +21,44 @@ public class UtilisateurService : IUtilisateurService _mapper = mapper; } - public async Task>> GetAll() + public async Task>> GetAll() { var utilisateurs = await _repositoryUtilisateur.GetAllAsync(); - return new ResponseDataModel> + var utilisateursOutputDto = _mapper.Map>(utilisateurs); + return new ResponseDataModel> { Success = true, StatusCode = 200, - Data = utilisateurs, + Data = utilisateursOutputDto, }; } - public async Task> GetById(int id) + public async Task> GetById(int id) { var utililisateur = await _repositoryUtilisateur.GetByIdAsync(id); - return new ResponseDataModel + var utilisateurOutput = _mapper.Map(utililisateur); + return new ResponseDataModel { Success = true, StatusCode = 200, - Data = utililisateur, + Data = utilisateurOutput, }; } public async Task> Login(UtilisateurLoginDto utilisateurInput) { + var validation = new UtilisateurCreateValidator(); + var result = validation.Validate(utilisateurInput); + if (!result.IsValid) + { + return new ResponseDataModel + { + StatusCode = 400, + Success = false, + Message = "Données utilisateur invalides: " + string.Join(", ", result.Errors) + }; + } + var utilisateur = await _repositoryUtilisateur.GetByEmailAsync(utilisateurInput.Email); if (utilisateur == null) diff --git a/ldap-cesi/Validator/Utilisateur/UtilisateurCreateValidator.cs b/ldap-cesi/Validator/Utilisateur/UtilisateurCreateValidator.cs new file mode 100644 index 0000000..154b071 --- /dev/null +++ b/ldap-cesi/Validator/Utilisateur/UtilisateurCreateValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using ldap_cesi.DTOs.Inputs; + +namespace ldap_cesi.Validator.Utilisateur; + +public class UtilisateurCreateValidator : AbstractValidator +{ + public UtilisateurCreateValidator() + { + RuleFor(x => x.Email) + .NotEmpty().WithMessage("L'email est requis.") + .EmailAddress().WithMessage("L'email n'est pas valide."); + + RuleFor(x => x.MotDePasse) + .NotEmpty().WithMessage("Le mot de passe est requis.") + .MinimumLength(6).WithMessage("Le mot de passe doit contenir au moins 6 caractères."); + } +} \ No newline at end of file diff --git a/ldap-cesi/ldap-cesi.csproj b/ldap-cesi/ldap-cesi.csproj index 0d8adcc..f06e052 100644 --- a/ldap-cesi/ldap-cesi.csproj +++ b/ldap-cesi/ldap-cesi.csproj @@ -11,8 +11,10 @@ + + - + all