Esempio n. 1
0
        public async Task <string> CreateOrderAsync(OrderBasketDto basket, CancellationToken cancellationToken)
        {
            var isEmail = Regex.IsMatch(basket.Email, OrderEmailConstants.emailFormatMatchRegex, RegexOptions.IgnoreCase);

            if (!isEmail)
            {
                return(OrderEmailConstants.errorEmailAddressIsNotValid);
            }

            try
            {
                var orderRepository = _unitOfWork.GetRepository <DAL.Model.Order>();
                var newOrder        = new DAL.Model.Order
                {
                    CreateDate    = DateTime.Now,
                    CustomerEmail = basket.Email,
                    Note          = basket.Note,
                    State         = Common.Enums.OrderEnums.OrderState.Created
                };
                await orderRepository.InsertAsync(newOrder).ConfigureAwait(false);

                await _unitOfWork.SaveChangesAsync().ConfigureAwait(false);

                var itemCardsInBasket = await _orderItemService.CreateOrderItemsFromBasketAsync(basket.Items, newOrder.Id, cancellationToken).ConfigureAwait(false);

                if (!itemCardsInBasket.Any())
                {
                    return(OrderEmailConstants.errorNoItemsInBasket);
                }

                var emailBody = await _orderEmailService.RenderViewToStringAsync(itemCardsInBasket, basket.Email, newOrder.Id.ToString(), cancellationToken).ConfigureAwait(false);

                await _orderEmailService.SendAsync(basket.Email, emailBody, basket.IsRequestedCopyOfOrder, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _errorLogService.LogExceptionAsync(ex).ConfigureAwait(false);

                return($"Systémová chyba: {ex.Message}");
            }
            return(string.Empty);
        }