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.Logging; using Quadarax.Foundation.Core.Value; namespace Quadarax.Application.QLiberace.Base.Services { public class UserService: AbstractRepositoryService { #region *** Constructor *** public UserService(RepoUser repository, IContext currentContext, IQLogger logger) : base(repository, currentContext, logger) { } #endregion #region *** Public Operations *** public ResultValueDto 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(userDao.Map()); }); } 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 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(result.Map()); }); } 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 } }