Example #1
0
        /// <summary>
        /// Create a new shipment
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="number">Shipment number</param>
        public async Task Add(string userId, string number)
        {
            var invoiceDto = await SearchByNumber(number);

            if (invoiceDto != null)
            {
                int postOperatorId = 0;
                foreach (KeyValuePair <int, string> idName in invoicesRepository.GetPostOperatorsIdNames())
                {
                    if (idName.Value == invoiceDto.PostOperatorName)
                    {
                        postOperatorId = idName.Key;
                        break;
                    }
                }

                var     mapper  = new MapperConfiguration(cfg => cfg.CreateMap <InvoiceDto, Invoice>()).CreateMapper();
                Invoice invoice = mapper.Map <Invoice>(invoiceDto);
                invoice.AccountUserId  = userId;
                invoice.PostOperatorId = postOperatorId;

                var results = invoicesValidator.Validate(invoice);
                if (results.IsValid)
                {
                    invoicesRepository.Create(invoice);
                }
                else
                {
                    throw new Exception("Помилка валідації відправлення: " + Environment.NewLine +
                                        ValidationResultsHelper.GetValidationErrors(results));
                }
            }
            else
            {
                throw new Exception("Відправлення не знайдено.");
            }
        }