|
|
@@ -13,6 +13,7 @@ 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.Exceptions;
|
|
|
using Quadarax.Foundation.Core.Value;
|
|
|
|
|
|
namespace Quadarax.Application.QLiberace.Customer.Services
|
|
|
@@ -140,38 +141,86 @@ namespace Quadarax.Application.QLiberace.Customer.Services
|
|
|
ValidateWDto(contact);
|
|
|
|
|
|
var contactDao = Repository2.New();
|
|
|
+ contactDao.Customer = customerDao!;
|
|
|
contact.CopyToDto(contactDao);
|
|
|
return new ResultValueDto<ContactRDto?>(contactDao.Map<Contact, ContactRDto>());
|
|
|
});
|
|
|
}
|
|
|
- public ResultDto UpdateContact(string customerCode, ContactWDto contact)
|
|
|
+ public ResultDto UpdateContact(long contactId, ContactWDto contact)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ if (contact==null) throw new ArgumentNullException(nameof(contact));
|
|
|
+
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ var contactDao = FetchContact(contactId);
|
|
|
+ ValidateWDto(contact);
|
|
|
+ contact.CopyToDto(contactDao);
|
|
|
+ return new ResultValueDto<ContactRDto?>(contactDao.Map<Contact, ContactRDto>());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public ResultDto DeleteContact(long contactId)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ Repository2.Remove(contactId);
|
|
|
+ return new ResultPlain();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public ResultValueDto<ContactRDto?> GetContact(long contactId)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ var contactDao = FetchContact(contactId);
|
|
|
+ return new ResultValueDto<ContactRDto?>(contactDao.Map<Contact, ContactRDto>());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public ResultsValueDto<ContactRDto?> GetContacts(string customerCode)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ var customerDao = FetchCustomer(customerCode);
|
|
|
+ return new ResultsValueDto<ContactRDto?>(customerDao!.Contacts.MapList<Contact, ContactRDto>());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public ResultsValueDto<ContactRDto?> GetPreferredContacts(string customerCode, params ContactTypeEnum[] contactTypes)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ if (contactTypes == null || contactTypes.Length == 0) throw new ArgumentNullException(nameof(contactTypes));
|
|
|
+
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ var customerDao = FetchCustomer(customerCode);
|
|
|
+ return new ResultsValueDto<ContactRDto?>(customerDao!.Contacts.Where(x=>x.IsPreferred && contactTypes.Contains(x.Type)).MapList<Contact, ContactRDto>());
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public ResultDto ValidateContacts(string customerCode)
|
|
|
{
|
|
|
- throw new NotImplementedException();
|
|
|
+ return TryCatchBlock(() =>
|
|
|
+ {
|
|
|
+ var customerDao = FetchCustomer(customerCode);
|
|
|
+ var contactTypes = new List<ContactTypeEnum>(new[]
|
|
|
+ { ContactTypeEnum.Address, ContactTypeEnum.Email, ContactTypeEnum.Phone });
|
|
|
+ var contacts = customerDao!.Contacts.Where(x => x.IsPreferred && contactTypes.Contains(x.Type)).ToArray();
|
|
|
+
|
|
|
+ if (contacts!=null && !contacts.Any())
|
|
|
+ throw ExceptionFactory.CreateException(
|
|
|
+ ExceptionLibrary.ExceptionsCodes.CustomerMissingContactCode, "code", customerCode);
|
|
|
+
|
|
|
+
|
|
|
+ var elist = new List<CodeException>();
|
|
|
+ if (contacts?.Count(x=>x.IsPreferred && x.Type == ContactTypeEnum.Address) != 1) elist.Add(ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerMissingAddressCode, "code",customerCode));
|
|
|
+ if (contacts?.Count(x=>x.IsPreferred && x.Type == ContactTypeEnum.Phone) != 1) elist.Add(ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerMissingPhoneCode, "code",customerCode));
|
|
|
+ if (contacts?.Count(x=>x.IsPreferred && x.Type == ContactTypeEnum.Email) != 1) elist.Add(ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerMissingEmailCode, "code",customerCode));
|
|
|
+
|
|
|
+ if (elist.Count > 0)
|
|
|
+ throw new AggregateException(elist);
|
|
|
+ return new ResultPlain();
|
|
|
+
|
|
|
+ });
|
|
|
}
|
|
|
#endregion
|
|
|
#endregion
|
|
|
@@ -228,6 +277,15 @@ namespace Quadarax.Application.QLiberace.Customer.Services
|
|
|
ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerNotFoundCode, "id", customerId.ToString());
|
|
|
return exists;
|
|
|
}
|
|
|
+
|
|
|
+ private Contact? FetchContact(long contactId)
|
|
|
+ {
|
|
|
+ var exists = Repository2.Get(contactId);
|
|
|
+ if (exists != null) throw
|
|
|
+ ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ContactNotFoundCode, "id", contactId.ToString());
|
|
|
+ return exists;
|
|
|
+ }
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
}
|