public IHttpActionResult Post(OrderProductCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateOrderProductService();

            string created = service.CreateOrderProduct(model);

            if (created.Contains("There is not enough inventory"))
            {
                return(BadRequest(created));
            }

            if (created.Contains("finalized"))
            {
                return(BadRequest(created));
            }

            if (created.Contains("The OrderProduct"))
            {
                return(Ok(created));
            }

            return(InternalServerError());
        }
示例#2
0
        public IHttpActionResult Post(OrderProductCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateOrderDetailService();

            if (!service.CreateOrderProduct(model))
            {
                return(InternalServerError());
            }

            return(Ok());
        }
示例#3
0
        public bool CreateOrderProduct(OrderProductCreate model)
        {
            var entity =
                new OrderProduct()
            {
                OrderId      = model.OrderId,
                ProductId    = model.ProductId,
                ProductCount = model.ProductCount
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.OrderProducts.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
        public string CreateOrderProduct(OrderProductCreate model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var order =
                    ctx
                    .Orders
                    .Single(o => o.OrderId == model.OrderId);

                if (order.OrderFinalized)
                {
                    return("This order has been finalized. You cannot make updates to it.");
                }

                var entity =
                    new OrderProduct()
                {
                    OrderId      = model.OrderId,
                    ProductId    = model.ProductId,
                    ProductCount = model.ProductCount
                };


                var product =
                    ctx
                    .Products
                    .Single(p => p.ProductId == model.ProductId);

                if (model.ProductCount >= product.UnitCount)
                {
                    return($"There is not enough inventory of {product.ProductName} available to create this OrderProduct. The current inventory is {product.UnitCount}.");
                }

                product.UnitCount -= model.ProductCount;

                ctx.OrderProducts.Add(entity);
                ctx.SaveChanges();

                return($"The OrderProduct {entity.PrimaryId} has been created."); //when bool, savechanges == 2
            }
        }