Пример #1
0
        //CreateOrder
        public Order CreateOrder(
            CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = customerservice_.SearchCustomers(
                new SearchCustomerOptions
            {
                CustomerId = options.CustomerId
            }).SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }
            var order = new Order();

            customer.Orders.Add(order);
            return(null);  // error : not all paths have a return value;

            /*
             * foreach (var item in options.ProductIds)
             * {
             *  //var product = ProductService
             *
             * } */
        }
Пример #2
0
        public Order CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = customerService.SearchCustomers(new SearchCustomerOptions()
            {
                CustomerId = options.CustomerId
            }).SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }

            var order = new Order()
            {
                DeliveryAddress = options.DeliveryAddress,
            };

            foreach (var prod in options.ProductIds)
            {
                if (prod == null)
                {
                    continue;
                }

                var product = productService.SearchProducts(new SearchProductOptions()
                {
                    ProductId = prod
                }).SingleOrDefault();

                if (prod != null)
                {
                    var orderProd = new OrderProduct()
                    {
                        Product = product,
                    };

                    order.OrderProducts.Add(orderProd);
                }
                else
                {
                    return(null);
                }
            }

            if (order.OrderProducts.Count == 0)
            {
                return(null);
            }

            customer.Orders.Add(order);
            context_.Update(customer);

            return(context_.SaveChanges() > 0 ? order : null);
        }
Пример #3
0
        public Result <Order> CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(Result <Order> .CreateFailed(
                           StatusCode.BadRequest, "Null options"));
            }

            var customer = customerService_.SearchCustomers(new SearchCustomerOptions()
            {
                CustomerId = options.CustomerId
            }).SingleOrDefault();

            if (customer == null)
            {
                return(Result <Order> .CreateFailed(
                           StatusCode.NotFound, $"Customer with id {options.CustomerId} was not found"));
            }

            if (options.ProductIds == null)
            {
                return(Result <Order> .CreateFailed(
                           StatusCode.NotFound, $"No products have been added to the order"));
            }

            var order = new Order();

            foreach (var productId in options.ProductIds)
            {
                var orderProduct = new OrderProduct()
                {
                    ProductId = productId,
                    OrderId   = order.OrderId,
                };
                order.OrderProducts.Add(orderProduct);
            }
            customer.Orders.Add(order);
            context_.Add(customer);
            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Order> .CreateFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Order> .CreateFailed(
                           StatusCode.InternalServerError,
                           "Order could not be created"));
            }

            return(Result <Order> .CreateSuccessful(order));
        }
Пример #4
0
        public Order CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = customerService_.SearchCustomers(new SearchCustomerOptions
            {
                CustomerId = options.CustomerId
            }).SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }


            var order = new Order();

            foreach (var prod in options.ProductsId)
            {
                if (prod == null)
                {
                    continue;
                }

                var product = productService_.SearchProduct(new SearchProductOptions()
                {
                    ProductId = prod
                }).SingleOrDefault();

                if (product != null)
                {
                    var orderprod = new OrderProduct()
                    {
                        Product = product
                    };

                    order.OrderProducts.Add(orderprod);
                }
            }

            if (order.OrderProducts.Count == 0)
            {
                return(null);
            }

            customer.Orders.Add(order);
            context_.Update(customer);
            context_.Add(order);

            if (context_.SaveChanges() > 0)
            {
                return(order);
            }
            return(null);
        }
Пример #5
0
        public Order CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = customerService.SearchCustomer(new CustomerSearchOptions()
            {
                CustomerId = options.CustomerId
            }).Include(c => c.OrderList)
                           .ThenInclude(o => o.OrderProducts)
                           .SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }

            var order = new Order();

            foreach (var id in options.ProductIds)
            {
                var product = productService.SearchProduct(new ProductSearchOptions()
                {
                    ProductId = id
                }).SingleOrDefault();

                if (product == null)
                {
                    return(null);
                }

                order.OrderProducts.Add(new OrderProduct()
                {
                    ProductId = product.ProductId
                });
                order.TotalAmount = order.TotalAmount + product.Price;
            }
            if (options.DeliveryAddress != null)
            {
                order.DeliveryAddress = options.DeliveryAddress;
            }
            customer.OrderList.Add(order);
            //dbContext.Update(customer);//??

            if (dbContext.SaveChanges() > 0)
            {
                return(order);
            }

            return(null);
        }
Пример #6
0
        public Order CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var customer = customers_.GetCustomerById(options.CustomerId).SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }

            var order = new Order()
            {
                DeliveryAddress = options.DeliveryAddress,
            };

            //var products = new List<Product>();

            foreach (var item in options.ProductIds)
            {
                var pResult = products_.GetProductById(item).SingleOrDefault();

                if (pResult != null)
                {
                    var orderProduct = new OrderProduct()
                    {
                        Product = pResult,
                    };

                    order.TotalAmount += pResult.Price;

                    order.OrderProducts.Add(orderProduct);
                }
                else
                {
                    return(null);
                }
            }
            if (order.OrderProducts.Count == 0)
            {
                return(null);
            }

            customer.Orders.Add(order);

            context_.Update(customer);


            return(context_.SaveChanges() > 0 ? order : null);
        }
Пример #7
0
        // function to create a new order
        public bool CreateOrder(CreateOrderOptions opt)
        {
            if (opt == null)
            {
                return(false);
            }
            Order order;
            var   customer = customerservice_.SearchCustomers(new CustomerOptions()
            {
                CustomerId = opt.CustomerId,
            }).SingleOrDefault();

            foreach (var p in opt.ProductIds)
            {
                var id = productservice_.SearchProducts(new ProductOptions()
                {
                    ProductId = p,
                });
            }

            order = new Order()
            {
                DeliveryAddress = "Athina",
            };

            customer.Orders.Add(order);
            var orderProduct = new OrderProduct();

            foreach (var p in opt.ProductIds)
            {
                orderProduct.ProductId = p;
                orderProduct.OrderId   = order.OrderId;

                order.OrderProducts.Add(orderProduct);
            }
            db_.Add(order);
            db_.SaveChanges();

            if (db_.SaveChanges() > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #8
0
        public ApiResult <Order> CreateOrder(
            CreateOrderOptions options)
        {
            if (options == null)
            {
                return(new ApiResult <Order>(
                           StatusCode.BadRequest, "null options"));
            }

            var cresult = customer_
                          .GetCustomerById(options.CustomerId);

            if (!cresult.Success)
            {
                return(ApiResult <Order> .Create(cresult));
            }

            var order = new Order();

            foreach (var id in options.ProductIds)
            {
                var prodResult = product_
                                 .GetProductById(id);

                if (!prodResult.Success)
                {
                    return(ApiResult <Order> .Create(
                               prodResult));
                }

                order.TotalCost  = 0;
                order.TotalCost += prodResult.Data.Price;

                order.OrderProducts.Add(
                    new OrderProduct()
                {
                    Product = prodResult.Data
                });
            }

            context_.Add(order);
            cresult.Data.Orders.Add(order);
            context_.SaveChanges();

            return(ApiResult <Order> .CreateSuccessful(order));
        }
Пример #9
0
        public Order CreateOrder(CreateOrderOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            // search for existing Customer if not, no order can be made
            var customer = customerService.SearchCustomers(
                new SearchCustomerOptions()
            {
                CustomerId = options.CustomerId
            }).SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }

            // if customer found making the order
            var order = new Order()
            {
                DeliveryAddress = options.DeliveryAddress,
            };

            foreach (var prod in options.ProductIds)
            {
                if (prod == null)
                {
                    continue;
                }

                var product = productService.SearchProduct(
                    new SearchProductOptions()
                {
                    ProductId = prod
                }).SingleOrDefault();

                if (product != null)
                {
                    var orderProd = new OrderProduct()
                    {
                        Product = product
                    };
                    order.OrderProducts.Add(orderProd);
                }
                else
                {
                    return(null);
                }
            }

            if (order.OrderProducts.Count == 0)
            {
                return(null);
            }

            customer.Orders.Add(order);
            context_.Update(customer);

            if (context_.SaveChanges() > 0)
            {
                return(order);
            }
            return(null);
        }