| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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<RepoTenant>
- {
- #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<TenantRDto?> 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<TenantRDto?>(result.Map<Tenant, TenantRDto>());
- });
- }
- public ResultValueDto<TenantRDto?> 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<TenantRDto?>(result.Map<Tenant, TenantRDto>());
- });
- }
- public ResultValueDto<TenantRDto?> 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<TenantRDto?>(tenantDao.Map<Tenant, TenantRDto>());
- });
- }
- 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<QlbrcContext>().TenantCode;
- if (!string.Equals(currentTenantCode, tenant.Code, StringComparison.InvariantCulture))
- throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantDifferentContextCode, tenant.Code, currentTenantCode!);
- }
- #endregion
- }
- }
|