Ejemplo n.º 1
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 BizAction(PlaceOrderInDto dto)
        {
            if (!dto.AcceptTAndCs)
            {
                AddError("You must accept the T&Cs to place an order.");
                return(null);
            }
            if (!dto.LineItems.Any())
            {
                AddError("No items in your basket.");
                return(null);
            }

            var booksDict = _dbAccess.FindBooksByIdsWithPriceOffers
                                (dto.LineItems.Select(x => x.BookId));
            var order = new Order
            {
                CustomerName         = dto.UserId,
                ExpectedDeliveryDate = DateTime.Today.AddDays(5),
                LineItems            = FormLineItemsWithErrorChecking(dto.LineItems, booksDict)
            };

            if (!HasErrors)
            {
                _dbAccess.Add(order);
            }

            return(HasErrors ? null : order);
        }
Ejemplo n.º 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
        }
Ejemplo n.º 3
0
        public Order Action(PlaceOrderInDto dto)
        {
            if (!dto.AcceptTAndCs)
            {
                AddError("You must accept the T&Cs to place an order.");
                return(null);
            }

            if (dto.LineItems.Count == 0)
            {
                AddError("No items in your basket.");
                return(null);
            }

            var bookIds = dto.LineItems.Select(lineItem => lineItem.BookId);
            //to present the data as an in-memory set(in this case, a dictionary)
            var booksDict = dbAccess.FindBooksByIdsWithPriceOffers(bookIds);
            var order     = new Order
            {
                CustomerName = dto.UserId,
                LineItems    = FormLineItemsWithErrorChecking(dto.LineItems, booksDict)
            };

            if (!HasErrors)
            {
                dbAccess.Add(order);
            }

            return(HasErrors ? null : order);
        }
Ejemplo n.º 4
0
        public Order Action(Part1ToPart2Dto dto)
        {
            var bookIds   = dto.LineItems.Select(lineItem => lineItem.BookId);
            var booksDict = dbAccess.FindBooksByIdsWithPriceOffers(bookIds);

            dto.Order.LineItems = FormLineItemsWithErrorChecking(dto.LineItems, booksDict);

            return(HasErrors ? null : dto.Order);
        }