| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using Quadarax.Application.QLiberace.Base.Repositories;
- using Quadarax.Foundation.Core.Business;
- using Microsoft.Extensions.Logging;
- using Quadarax.Application.QLiberace.Base.Dtos;
- using Quadarax.Foundation.Core.Data.Interface.Domain;
- using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
- using Quadarax.Application.QLiberace.Common;
- using Quadarax.Application.QLiberace.Base.Dtos.Validators;
- using Quadarax.Application.QLiberace.Base.Entities;
- using Quadarax.Application.QLiberace.Base.Mapper;
- using Quadarax.Foundation.Core.Value;
- namespace Quadarax.Application.QLiberace.Base.Services
- {
- public class UserService: AbstractRepositoryService<RepoUser>
- {
- #region *** Constructor ***
- public UserService(RepoUser repository, IContext currentContext, ILoggerFactory logger) : base(repository, currentContext, logger)
- {
- }
- #endregion
- #region *** Public Operations ***
- public ResultValueDto<UserRDto?> CreateUser(UserWDto user)
- {
- if (user == null) throw new ArgumentNullException(nameof(user));
- return TryCatchBlock(() =>
- {
- ValidateWDto(user);
- var exists = Repository.GetByLoginName(user.LoginName,true);
- if (exists != null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserAlreadyExistsCode, user.LoginName);
- var userDao = Repository.New();
- user.CopyToDto(userDao);
- return new ResultValueDto<UserRDto?>(userDao.Map<User, UserRDto>());
- });
- }
- public ResultDto DeleteUser(long userId)
- {
- return TryCatchBlock(() =>
- {
- var exists = Repository.Get(userId);
- if (exists != null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserNotFoundCode, "id", userId.ToString());
- Repository.Remove(userId);
- return new ResultPlain();
- });
- }
- public ResultValueDto<UserRDto?> GetUser(string loginName)
- {
- if (string.IsNullOrEmpty(loginName)) throw new ArgumentNullException(nameof(loginName));
- return TryCatchBlock(() =>
- {
- var result = Repository.GetByLoginName(loginName, true);
- if (result == null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserNotFoundCode, "loginName", loginName);
- return new ResultValueDto<UserRDto?>(result.Map<User, UserRDto>());
- });
- }
- public ResultDto Lock(string loginName, bool isLocked)
- {
- if (string.IsNullOrEmpty(loginName)) throw new ArgumentNullException(nameof(loginName));
- return TryCatchBlock(() =>
- {
- var result = Repository.GetByLoginName(loginName, true);
- if (result == null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserNotFoundCode, "loginName", loginName);
- result.IsLocked = isLocked;
- Repository.Set(result);
- return new ResultPlain();
- });
- }
- #endregion
- #region *** Private Operations ***
- private void ValidateWDto(UserWDto user)
- {
- if (user == null) throw new ArgumentNullException(nameof(user));
- var validator = new UserValidator();
- validator.Validate(user, new ValidatorContext());
- if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException());
- }
- #endregion
- }
- }
|