Browse Source

finalize CustomerService

Dalibor Votruba 3 years ago
parent
commit
3781774356

+ 20 - 0
Common/qdr.app.qlbrc.common/ExceptionLibrary.cs

@@ -26,6 +26,13 @@ namespace Quadarax.Application.QLiberace.Common
             AddCode(ExceptionsCodes.CustomerNotFoundCode, ExceptionsCodes.CustomerNotFoundMsg);
             AddCode(ExceptionsCodes.CustomerUserAlreadyAssignedCode, ExceptionsCodes.CustomerUserAlreadyAssignedMsg);
             AddCode(ExceptionsCodes.CustomerUserNotFoundAssignedCode, ExceptionsCodes.CustomerUserNotFoundAssignedMsg);
+
+            AddCode(ExceptionsCodes.CustomerMissingContactCode, ExceptionsCodes.CustomerMissingContactMsg);
+            AddCode(ExceptionsCodes.CustomerMissingAddressCode, ExceptionsCodes.CustomerMissingAddressMsg);
+            AddCode(ExceptionsCodes.CustomerMissingEmailCode, ExceptionsCodes.CustomerMissingEmailMsg);
+            AddCode(ExceptionsCodes.CustomerMissingPhoneCode, ExceptionsCodes.CustomerMissingPhoneMsg);
+
+            AddCode(ExceptionsCodes.ContactNotFoundCode, ExceptionsCodes.ContactNotFoundMsg);
         }
 
 
@@ -65,6 +72,19 @@ namespace Quadarax.Application.QLiberace.Common
             public const string CustomerUserNotFoundAssignedMsg = "Customer with identifier {0}='{1}' has no user '{2}' assigned.";
             public const int CustomerUserNotFoundAssignedCode = 10032;
 
+            public const string CustomerMissingContactMsg = "Customer with identifier {0}='{1}' has no any contact.";
+            public const int CustomerMissingContactCode = 10033;
+            public const string CustomerMissingAddressMsg = "Customer with identifier {0}='{1}' has no one preffered Address.";
+            public const int CustomerMissingAddressCode = 10034;
+            public const string CustomerMissingPhoneMsg = "Customer with identifier {0}='{1}' has no one preffered Phone.";
+            public const int CustomerMissingPhoneCode = 10035;
+            public const string CustomerMissingEmailMsg = "Customer with identifier {0}='{1}' has no one preffered Email.";
+            public const int CustomerMissingEmailCode = 10036;
+
+            public const string ContactNotFoundMsg = "Contact with identifier {0}='{1}' not found";
+            public const int ContactNotFoundCode = 10040;
+
+
             
         }
 

+ 65 - 7
Modules/qdr.app.qlbrc.customer/Services/CustomerService.cs

@@ -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
 
     }