Skip to content

Commit

Permalink
Add more request validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanderlan Gomes da Silva committed Oct 31, 2023
1 parent c631e7a commit ab24998
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/Orion.Api/Controllers/V1/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public UsersController(IMediator mediator) : base(mediator)
}

[HttpGet]
[SwaggerResponse((int)HttpStatusCode.OK,"A success reponse with a list of Users paginated" ,typeof(PagedList<User>))]
[SwaggerResponse((int)HttpStatusCode.OK,"A success response with a list of Users paginated", typeof(PagedList<User>))]
[SwaggerResponse((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> Get([FromQuery] UserGetPaginatedRequest filter)
{
Expand All @@ -38,7 +38,7 @@ public async Task<IActionResult> Get([FromQuery] UserGetPaginatedRequest filter)
}

[HttpGet("{id}")]
[SwaggerResponse((int)HttpStatusCode.OK,"A success reponse with a single User" ,typeof(UserGetByIdResponse))]
[SwaggerResponse((int)HttpStatusCode.OK,"A success response with a single User", typeof(UserGetByIdResponse))]
[SwaggerResponse((int)HttpStatusCode.NotFound)]
[SwaggerResponse((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> Get(string id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MediatR;
using Orion.Application.Core.Commands.UserCreate;
using Orion.Domain.Core.Services.Interfaces;

namespace Orion.Application.Core.Commands.LoginWithCredentials;
Expand All @@ -15,8 +14,8 @@ public LoginWithCredentialsRequestHandler(IUserService userService)

public async Task<LoginWithCredentialsResponse> Handle(LoginWithCredentialsRequest request, CancellationToken cancellationToken)
{
var customerCreated = await _userService.LoginAsync(request.Email, request.Password);
var user = await _userService.LoginAsync(request.Email, request.Password);

return (LoginWithCredentialsResponse)customerCreated;
return (LoginWithCredentialsResponse)user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public LoginWithRefreshTokenRequestHandler(IUserService userService)

public async Task<LoginWithRefreshTokenResponse> Handle(LoginWithRefreshTokenRequest request, CancellationToken cancellationToken)
{
var customerCreated = await _userService.SignInWithRehreshTokenAsync(request.RefreshToken, request.Token);
var user = await _userService.SignInWithRehreshTokenAsync(request.RefreshToken, request.Token);

return (LoginWithRefreshTokenResponse)customerCreated;
return (LoginWithRefreshTokenResponse)user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentValidation;
using Microsoft.Extensions.Localization;
using Orion.Croscutting.Resources;
using static Orion.Croscutting.Resources.Messages.MessagesKeys;

namespace Orion.Application.Core.Commands.UserDelete;

public class UserDeleteRequestValidator : AbstractValidator<UserDeleteRequest>
{
public UserDeleteRequestValidator(IStringLocalizer<OrionResources> stringLocalizer)
{
RuleFor(c => c)
.NotNull()
.WithMessage(stringLocalizer[UserMessages.NullEntity]);

RuleFor(c => c.PublicId)
.NotEmpty().WithMessage(stringLocalizer[UserMessages.EmptyId])
.NotNull().WithMessage(stringLocalizer[UserMessages.EmptyId]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FluentValidation;
using Microsoft.Extensions.Localization;
using Orion.Croscutting.Resources;
using static Orion.Croscutting.Resources.Messages.MessagesKeys;

namespace Orion.Application.Core.Commands.UserUpdate;

public class UserUpdateRequestValidator : AbstractValidator<UserUpdateRequest>
{
public UserUpdateRequestValidator(IStringLocalizer<OrionResources> stringLocalizer)
{
RuleFor(c => c)
.NotNull()
.WithMessage(stringLocalizer[UserMessages.NullEntity]);

RuleFor(c => c.Name)
.NotEmpty().WithMessage(stringLocalizer[UserMessages.EmptyName])
.NotNull().WithMessage(stringLocalizer[UserMessages.EmptyName]);

RuleFor(c => c.Email)
.NotEmpty().WithMessage(stringLocalizer[UserMessages.EmptyEmail])
.NotNull().WithMessage(stringLocalizer[UserMessages.EmptyEmail]);

RuleFor(c => c.PublicId)
.NotEmpty().WithMessage(stringLocalizer[UserMessages.EmptyId])
.NotNull().WithMessage(stringLocalizer[UserMessages.EmptyId]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace Orion.Application.Core.Queries.UserGetById;

public class UserGetByIdHandler : IRequestHandler<UserGetByIdRequest, UserGetByIdResponse>
{
private readonly IUserService _customerService;
private readonly IUserService _userService;

public UserGetByIdHandler(IUserService customerService)
public UserGetByIdHandler(IUserService userService)
{
_customerService = customerService;
_userService = userService;
}

public async Task<UserGetByIdResponse> Handle(UserGetByIdRequest command, CancellationToken cancellationToken)
public async Task<UserGetByIdResponse> Handle(UserGetByIdRequest request, CancellationToken cancellationToken)
{
return (UserGetByIdResponse) await _customerService.FindByIdAsync(command.PublicId);
return (UserGetByIdResponse) await _userService.FindByIdAsync(request.PublicId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentValidation;
using Microsoft.Extensions.Localization;
using Orion.Croscutting.Resources;
using static Orion.Croscutting.Resources.Messages.MessagesKeys;

namespace Orion.Application.Core.Queries.UserGetById;

public class UserGetByIdRequestValidator : AbstractValidator<UserGetByIdRequest>
{
public UserGetByIdRequestValidator(IStringLocalizer<OrionResources> stringLocalizer)
{
RuleFor(c => c)
.NotNull()
.WithMessage(stringLocalizer[UserMessages.NullEntity]);

RuleFor(c => c.PublicId)
.NotEmpty().WithMessage(stringLocalizer[UserMessages.EmptyId])
.NotNull().WithMessage(stringLocalizer[UserMessages.EmptyId]);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using MediatR;
using Orion.Application.Core.Queries.UserGetById;
using Orion.Domain.Core.Entities;
using Orion.Domain.Core.Services.Interfaces;
using Orion.Domain.Core.ValueObjects.Pagination;

namespace Orion.Application.Core.Queries.UserGetPaginated;

public class UserGetPaginatedRequestHandler : IRequestHandler<UserGetPaginatedRequest, PagedList<User>>
public class UserGetPaginatedRequestHandler : IRequestHandler<UserGetPaginatedRequest, PagedList<UserGetPaginatedResponse>>
{
private readonly IUserService _customerService;
private readonly IUserService _userService;

public UserGetPaginatedRequestHandler(IUserService customerService)
public UserGetPaginatedRequestHandler(IUserService userService)
{
_customerService = customerService;
_userService = userService;
}

public async Task<PagedList<User>> Handle(UserGetPaginatedRequest request, CancellationToken cancellationToken)
public async Task<PagedList<UserGetPaginatedResponse>> Handle(UserGetPaginatedRequest request, CancellationToken cancellationToken)
{
return await _customerService.ListPaginateAsync(request);
var users = await _userService.ListPaginateAsync(request);

var usersPaginated = users.Items.Select(user => (UserGetPaginatedResponse)user).ToList();

return new PagedList<UserGetPaginatedResponse>(usersPaginated);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using MediatR;
using Orion.Domain.Core.Entities;
using Orion.Domain.Core.Filters;
using Orion.Domain.Core.ValueObjects.Pagination;

namespace Orion.Application.Core.Queries.UserGetPaginated;

public class UserGetPaginatedRequest : UserFilter, IRequest<PagedList<User>>
public class UserGetPaginatedRequest : UserFilter, IRequest<PagedList<UserGetPaginatedResponse>>
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Orion.Domain.Core.Entities;
using Orion.Domain.Core.Entities.Enuns;

namespace Orion.Application.Core.Queries.UserGetPaginated;

public class UserGetPaginatedResponse
{
public string PublicId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public UserProfile Profile { get; set; }
public DateTime LastUpdated { get; set; }
public DateTime CreatedAt { get; set; }

public static implicit operator UserGetPaginatedResponse(User user)
{
if (user is null)
return default;

return new UserGetPaginatedResponse
{
PublicId = user.PublicId,
Name = user.Name,
Email = user.Email,
Profile = user.Profile,
LastUpdated = user.LastUpdated,
CreatedAt = user.CreatedAt
};
}
}
1 change: 1 addition & 0 deletions src/Orion.Croscutting.Resources/Messages/MessagesKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class UserMessages
public const string EmptyName = "User.EmptyName";
public const string EmptyPasword = "User.EmptyPassword";
public const string EmptyEmail = "User.EmptyEmail";
public const string EmptyId = "User.EmptyId"; //todo: add to resource
public const string EmailExists = "User.EmailExists";
public const string InvalidCredentials = "User.InvalidCredentials";
public const string InvalidRefreshToken = "User.InvalidRefreshToken";
Expand Down
8 changes: 1 addition & 7 deletions src/Orion.Croscutting.Resources/OrionResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,5 @@ public OrionResources(IStringLocalizer<OrionResources> localizer)
_localizer = localizer;
}

public string this[string index]
{
get
{
return _localizer[index];
}
}
public string this[string index] => _localizer[index];
}
9 changes: 3 additions & 6 deletions src/Orion.Domain.Core/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
using System.Threading.Tasks;
using Orion.Domain.Core.Extensions;
using Orion.Croscutting.Resources;
using static Orion.Croscutting.Resources.Messages.MessagesKeys;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Orion.Domain.Core.Entities;
using Orion.Domain.Core.Exceptions;
using Orion.Domain.Core.Filters;
using Orion.Domain.Core.Repositories.UnitOfWork;
using Orion.Domain.Core.Services.Interfaces;
using Orion.Domain.Core.ValueObjects.Pagination;
using static Orion.Croscutting.Resources.Messages.MessagesKeys;

namespace Orion.Domain.Core.Services;

Expand Down Expand Up @@ -40,9 +41,6 @@ public async Task<User> AddAsync(User user)

private async Task ValidateUser(User user)
{
if (string.IsNullOrEmpty(user.Password))
throw new BusinessException(_messages[UserMessages.EmptyPasword]);

var userFound = await _unitOfWork.UserRepository.FindByEmailAsync(user.Email);

if (userFound != null && userFound.PublicId != user.PublicId)
Expand Down Expand Up @@ -75,7 +73,6 @@ public async Task UpdateAsync(User user)

entitySaved.Email = user.Email;
entitySaved.Name = user.Name;
entitySaved.Password = user.Password.ToSha512();

_unitOfWork.UserRepository.Update(entitySaved);

Expand Down Expand Up @@ -105,7 +102,7 @@ public async Task<User> SignInWithRehreshTokenAsync(string refreshToken, string
);
}

var email = GetClaimFromJwtToken(expiredToken, JwtRegisteredClaimNames.Email);
var email = GetClaimFromJwtToken(expiredToken, ClaimTypes.Email);

var userRefreshToken = (await _unitOfWork.RefreshTokenRepository.SearchByAsync(x => x.Refreshtoken.Equals(refreshToken) && x.Email == email)).FirstOrDefault();

Expand Down

0 comments on commit ab24998

Please sign in to comment.