using System.ComponentModel.DataAnnotations; using Quadarax.Application.QLiberace.Base.Repositories; using Quadarax.Foundation.Core.Business; using Microsoft.Extensions.Logging; using Quadarax.Application.QLiberace.Base.Dtos; using Quadarax.Application.QLiberace.Base.Dtos.Validators; using Quadarax.Application.QLiberace.Base.Entities; using Quadarax.Application.QLiberace.Base.Mapper; using Quadarax.Foundation.Core.Data.Interface.Domain; using Quadarax.Foundation.Core.Data.Interface.Entity.Dto; using Quadarax.Application.QLiberace.Common; using Quadarax.Application.QLiberace.Common.Domain; using Quadarax.Foundation.Core.Logging; using Quadarax.Foundation.Core.Value; namespace Quadarax.Application.QLiberace.Base.Services { public class TenantService : AbstractRepositoryService { #region *** Constructor *** public TenantService(RepoTenant repository, IContext currentContext, IQLogger logger) : base(repository, currentContext, logger) { } #endregion #region *** Public Operations *** public ResultBoolDto ExistsTenant(string tenantCode) { if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode)); return TryCatchBlock(() => { var result = Repository.GetByCode(tenantCode); if (result !=null) UpdateAccess(result); return new ResultBoolDto(result!=null); }); } public ResultDto Lock(string tenantCode, bool isLocked) { if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode)); return TryCatchBlock(() => { var result = Repository.GetByCode(tenantCode); if (result == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode); result.IsLocked = isLocked; Repository.Set(result); UpdateAccess(result); return new ResultPlain(); }); } public ResultValueDto GetTenant(string tenantCode) { if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode)); return TryCatchBlock(() => { var result = Repository.GetByCode(tenantCode); if (result == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode); UpdateAccess(result); return new ResultValueDto(result.Map()); }); } public ResultValueDto GetTenant(long tenantId) { return TryCatchBlock(() => { var result = Repository.Get(tenantId); if (result == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString()); UpdateAccess(result); return new ResultValueDto(result.Map()); }); } public ResultValueDto CreateTenant(TenantWDto tenant) { if (tenant==null) throw new ArgumentNullException(nameof(tenant)); return TryCatchBlock(() => { ValidateWDto(tenant); var exists = Repository.GetByCode(tenant.Code); if (exists != null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantAlreadyExistsCode, tenant.Code); var tenantDao = Repository.New(); tenant.CopyToDto(tenantDao); return new ResultValueDto(tenantDao.Map()); }); } public ResultPlain Validate(string tenantCode) { if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode)); return TryCatchBlock(() => { var result = Repository.GetByCode(tenantCode); if (result == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode); if(result.IsLocked) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantLockedCode, tenantCode); UpdateAccess(result); return new ResultPlain(); }); } public ResultDto UpdateTenant(TenantWDto tenant) { if (tenant==null) throw new ArgumentNullException(nameof(tenant)); return TryCatchBlock(() => { ValidateWDto(tenant); var exists = Repository.GetByCode(tenant.Code); if (exists == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenant.Code); tenant.CopyToDto(exists); Repository.Set(exists); UpdateAccess(exists); return new ResultPlain(); }); } public ResultDto DeleteTenant(long tenantId) { return TryCatchBlock(() => { var result = Repository.Get(tenantId); if (result == null) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString()); Repository.Remove(tenantId); return new ResultPlain(); }); } #endregion #region *** Private Operations *** private void UpdateAccess(Tenant tenant) { tenant.LastAccess = DateTime.Now; Repository.Set(tenant); } private void ValidateWDto(TenantWDto tenant) { if (tenant == null) throw new ArgumentNullException(nameof(tenant)); var validator = new TenantValidator(); validator.Validate(tenant, new ValidatorContext()); if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException()); } private void ValidateSameTenant(TenantWDto tenant) { if (tenant == null) throw new ArgumentNullException(nameof(tenant)); var currentTenantCode = CurrentContext.GetContext().TenantCode; if (!string.Equals(currentTenantCode, tenant.Code, StringComparison.InvariantCulture)) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantDifferentContextCode, tenant.Code, currentTenantCode!); } #endregion } }