43 lines
2.1 KiB
C#
43 lines
2.1 KiB
C#
using GozareshgirProgramManager.Application._Common.Interfaces;
|
|
using GozareshgirProgramManager.Application._Common.Models;
|
|
using GozareshgirProgramManager.Domain._Common;
|
|
using GozareshgirProgramManager.Domain.RoleUserAgg;
|
|
using GozareshgirProgramManager.Domain.UserAgg.Entities;
|
|
using GozareshgirProgramManager.Domain.UserAgg.Repositories;
|
|
|
|
namespace GozareshgirProgramManager.Application.Modules.Users.Commands.CreateUser;
|
|
|
|
public class CreateUserCommandHandler : IBaseCommandHandler<CreateUserCommand>
|
|
{
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public CreateUserCommandHandler(IUnitOfWork unitOfWork, IUserRepository userRepository)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_userRepository = userRepository;
|
|
}
|
|
|
|
public async Task<OperationResult> Handle(CreateUserCommand request, CancellationToken cancellationToken)
|
|
{
|
|
|
|
#region CustomValidation
|
|
if (_userRepository.Exists(x => x.FullName == request.FullName))
|
|
return OperationResult.Failure("نام و خانوادگی تکراری است");
|
|
if (_userRepository.Exists(x => x.UserName == request.UserName))
|
|
return OperationResult.Failure("نام کاربری تکراری است");
|
|
if (_userRepository.Exists(x=> !string.IsNullOrWhiteSpace(x.Mobile) && x.Mobile == request.Mobile))
|
|
return OperationResult.ValidationError("این شماره همراه قبلا به فرد دیگری اختصاص داده شده است");
|
|
if(request.AccountId == 0)
|
|
return OperationResult.Failure("آی دی اکانت، از سمت گزارشگیر صفر است");
|
|
#endregion
|
|
|
|
var userRoles = request.Roles.Where(x => x > 0).Select(x => new RoleUser(x)).ToList() ;
|
|
var create = new User(request.FullName, request.UserName, request.Password, request.Mobile,
|
|
request.Email, request?.AccountId, userRoles);
|
|
|
|
await _userRepository.CreateAsync(create);
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return OperationResult.Success();
|
|
}
|
|
} |