/// <summary>
        /// This validates the input and if OK creates
        /// an order and calls the _dbAccess to add to orders
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>returns an Order. Will be null if there are errors</returns>
        public Order Action(PlaceOrderInDto dto)                    //#D
        {                                                           //#E
            if (!dto.LineItems.Any())                               //#E
            {                                                       //#E
                AddError("No items in your basket.");               //#E
                return(null);                                       //#E
            }                                                       //#E

            var itemsDict =                                         //#F
                            _dbAccess.FindItemsByIdsWithPriceOffers //#F
                                (dto.LineItems.Select(x => x.SelectedItem.Key));

            var order = new Order                             //#G
            {                                                 //#G
                CustomerKey  = dto.Customer.Key,              //#G
                EmployeeKey  = dto.Employee.Key,
                OrderedItems =                                //#G
                               FormLineItemsWithErrorChecking //#G
                                   (dto.LineItems, itemsDict) //#G
            };                                                //#G

            if (!HasErrors)                                   //#H
            {
                _dbAccess.Add(order);                         //#H
            }
            return(HasErrors ? null : order);                 //#I
        }
Example #2
0
        /// <summary>
        /// This validates the input and if OK creates
        /// an order and calls the _dbAccess to add to orders
        /// </summary>
        /// <param name="dto"></param>
        /// <returns>returns an Order. Will be null if there are errors</returns>
        public Order Action(PlaceOrderInDto dto)                       //#D
        {
            if (!dto.AcceptTAndCs)                                     //#E
            {                                                          //#E
                AddError(                                              //#E
                    "You must accept the T&Cs to place an order.");    //#E
                return(null);                                          //#E
            }                                                          //#E
            if (!dto.LineItems.Any())                                  //#E
            {                                                          //#E
                AddError("No items in your basket.");                  //#E
                return(null);                                          //#E
            }                                                          //#E

            var booksDict =                                            //#F
                            _dbAccess.FindBooksByIdsWithPriceOffers    //#F
                                (dto.LineItems.Select(x => x.BookId)); //#F
            var order = new Order                                      //#G
            {                                                          //#G
                CustomerName = dto.UserId,                             //#G
                LineItems    =                                         //#G
                               FormLineItemsWithErrorChecking          //#G
                                   (dto.LineItems, booksDict)          //#G
            };                                                         //#G

            if (!HasErrors)                                            //#H
            {
                _dbAccess.Add(order);                                  //#H
            }
            return(HasErrors ? null : order);                          //#I
        }