| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using Microsoft.Extensions.Logging;
- using Quadarax.Application.QLiberace.Base.Dtos;
- using Quadarax.Application.QLiberace.Base.Entities;
- using Quadarax.Application.QLiberace.Base.Mapper;
- using Quadarax.Application.QLiberace.Base.Repositories;
- using Quadarax.Application.QLiberace.Common;
- using Quadarax.Application.QLiberace.Common.Domain;
- using Quadarax.Application.QLiberace.Customer.Dtos;
- using Quadarax.Application.QLiberace.Customer.Dtos.Validators;
- using Quadarax.Application.QLiberace.Customer.Entities;
- using Quadarax.Application.QLiberace.Customer.Enums;
- using Quadarax.Application.QLiberace.Customer.Repositories;
- using Quadarax.Foundation.Core.Business;
- using Quadarax.Foundation.Core.Data.Interface.Domain;
- using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
- using Quadarax.Foundation.Core.Value;
- namespace Quadarax.Application.QLiberace.Customer.Services
- {
- public class CustomerService : AbstractMultiRepositoryService<RepoCustomer,RepoContact, RepoUser, RepoCountry>
- {
- #region *** Constructor ***
- public CustomerService(RepoCustomer repository1, RepoContact repository2, RepoUser repository3, RepoCountry repository4, IContext currentContext, ILoggerFactory logger) : base(repository1, repository2, repository3, repository4, currentContext, logger)
- {
- }
- #endregion
- #region *** Public operations ***
- #region **** Customer ****
- public ResultValueDto<CustomerRDto?> CreateCustomer(CustomerWDto customer)
- {
- if (customer==null) throw new ArgumentNullException(nameof(customer));
- return TryCatchBlock(() =>
- {
- ValidateWDto(customer);
- var customerDao = Repository1.New();
- customer.CopyToDto(customerDao);
- return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
- });
- }
- public ResultDto UpdateCustomer(string customerCode, CustomerWDto customer)
- {
- if (customer==null) throw new ArgumentNullException(nameof(customer));
- return TryCatchBlock(() =>
- {
- ValidateWDto(customer);
- var customerDao = FetchCustomer(customerCode);
- customer.CopyToDto(customerDao);
- return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
- });
- }
- public ResultDto DeleteCustomer(long customerId)
- {
- return TryCatchBlock(() =>
- {
- var result = FetchCustomer(customerId);
- Repository1.Remove(customerId);
- return new ResultPlain();
- });
- }
- public ResultValueDto<CustomerRDto?> GetCustomer(string customerCode)
- {
- return TryCatchBlock(() =>
- {
- var customerDao = FetchCustomer(customerCode);
- return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
- });
- }
- public ResultValueDto<CustomerRDto?> GetCustomer(long customerId)
- {
- return TryCatchBlock(() =>
- {
- var customerDao = FetchCustomer(customerId);
- return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
- });
- }
- public ResultBoolDto ExistsCustomer(string customerCode)
- {
- return TryCatchBlock(() => new ResultBoolDto(FetchCustomer(customerCode)!=null));
- }
- #endregion
- #region **** User Assignments ****
- public ResultDto AssignUser(string customerCode, string userLogin)
- {
- return TryCatchBlock(() =>
- {
- var customerDao = FetchCustomer(customerCode);
- FetchUser(userLogin);
-
- if (customerDao!.Users.Any(x=>string.Equals(x.UserLoginName, userLogin)))
- throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerUserAlreadyAssignedCode, "code",customerCode, userLogin);
- customerDao.Users.Add(new CustomerUser()
- {
- CustomerId = customerDao.Id,
- UserLoginName = userLogin
- });
- return new ResultPlain();
- });
- }
- public ResultDto UnAssignUser(string customerCode, string userLogin)
- {
- return TryCatchBlock(() =>
- {
- var customerDao = FetchCustomer(customerCode);
- FetchUser(userLogin);
- var customerUser = customerDao!.Users.FirstOrDefault(x => string.Equals(x.UserLoginName, userLogin));
- if (customerUser == null)
- throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerUserNotFoundAssignedCode, "code",customerCode, userLogin);
- customerDao.Users.Remove(customerUser);
- return new ResultPlain();
- });
- }
- #endregion
- #region **** Customer Constacts ****
- public ResultValueDto<ContactRDto?> CreateContact(string customerCode, ContactWDto contact)
- {
- if (contact==null) throw new ArgumentNullException(nameof(contact));
- return TryCatchBlock(() =>
- {
- var customerDao = FetchCustomer(customerCode);
- ValidateWDto(contact);
- var contactDao = Repository2.New();
- contact.CopyToDto(contactDao);
- return new ResultValueDto<ContactRDto?>(contactDao.Map<Contact, ContactRDto>());
- });
- }
- public ResultDto UpdateContact(string customerCode, ContactWDto contact)
- {
- throw new NotImplementedException();
- }
- public ResultDto DeleteContact(long contactId)
- {
- throw new NotImplementedException();
- }
- public ResultValueDto<ContactRDto?> GetContact(long contactId)
- {
- throw new NotImplementedException();
- }
- public ResultsValueDto<ContactRDto?> GetContacts(string customerCode)
- {
- throw new NotImplementedException();
- }
- public ResultsValueDto<ContactRDto?> GetPreferredContacts(string customerCode, params ContactTypeEnum[] contactTypes)
- {
- throw new NotImplementedException();
- }
- public ResultDto ValidateContacts(string customerCode)
- {
- throw new NotImplementedException();
- }
- #endregion
- #endregion
- #region *** Private Operations ***
- private void ValidateWDto(CustomerWDto customer)
- {
- if (customer == null) throw new ArgumentNullException(nameof(customer));
- var validator = new CustomerValidator();
- validator.Validate(customer, new ValidatorContext(new KeyValuePair<string, object>[]{ new("RepoCountry", Repository4)}));
- if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException());
- }
- private void ValidateWDto(ContactWDto contact)
- {
- if (contact == null) throw new ArgumentNullException(nameof(contact));
- var validator = new ContactValidator();
- validator.Validate(contact, new ValidatorContext(new KeyValuePair<string, object>[]{ new("RepoCountry", Repository4)}));
- 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!);
- }
- private Entities.Customer? FetchCustomer(string customerCode)
- {
- if (string.IsNullOrEmpty(customerCode)) throw new ArgumentNullException(nameof(customerCode));
- var exists = Repository1.GetByCode(customerCode);
- if (exists != null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerNotFoundCode, "code", customerCode);
- return exists;
- }
- private User? FetchUser(string userLogin)
- {
- if (string.IsNullOrEmpty(userLogin)) throw new ArgumentNullException(nameof(userLogin));
- var exists = Repository3.GetByLoginName(userLogin);
- if (exists != null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserNotFoundCode, "loginName", userLogin);
- return exists;
- }
- private Entities.Customer? FetchCustomer(long customerId)
- {
- var exists = Repository1.Get(customerId);
- if (exists != null) throw
- ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerNotFoundCode, "id", customerId.ToString());
- return exists;
- }
- #endregion
- }
- }
|