예제 #1
0
        public IActionResult ModifiedProductsById(int id, [FromBody] Product product)
        {
            var products = new Product();
            ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);

            if (product == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != 0)
            {
                products = (from p in productWebApiContext.Product where p.Id == id select p).FirstOrDefault();
                if (products != null)
                {
                    products.Name  = product.Name;
                    products.Price = product.Price;
                }

                productWebApiContext.SaveChanges();
            }
            return(Ok(products));
        }
예제 #2
0
        public IActionResult InsertProductsAndMaterials([FromBody] ProductMaterialInput input)
        {
            ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);

            if (input == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var maxId      = (from p in productWebApiContext.Product select p.Id).Max();
            var newProduct = new Product {
                Id    = ++maxId,
                Name  = input.Name,
                Price = input.Price,
            };
            var maxMaterialId = (from m in productWebApiContext.Material select m.Id).Max();
            var newMaterial   = new Material {
                Id        = ++maxMaterialId,
                Name      = input.Materials.First().Name,
                ProductId = maxId
            };

            productWebApiContext.Add(newProduct);
            productWebApiContext.Add(newMaterial);
            productWebApiContext.SaveChanges();
            return(Ok(newProduct));
        }
예제 #3
0
        public IActionResult DeleteMaterialById(int productid, int materialid)
        {
            ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);
            var product = (from p in productWebApiContext.Product where p.Id == productid select p).FirstOrDefault();

            if (product == null)
            {
                return(NotFound());
            }
            var material = (from m in productWebApiContext.Material where m.Id == materialid select m).FirstOrDefault();

            if (material == null)
            {
                return(NotFound());
            }
            productWebApiContext.Remove(material);
            productWebApiContext.SaveChanges();
            return(Ok());
        }
예제 #4
0
        public IActionResult InsertProductsCollection([FromBody] IEnumerable <Product> products)
        {
            ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);

            if (products == null)
            {
                return(BadRequest());
            }
            foreach (var product in products)
            {
                var maxId      = (from p in productWebApiContext.Product select p.Id).Max();
                var newProduct = new Product {
                    Id    = ++maxId,
                    Name  = product.Name,
                    Price = product.Price,
                };
                productWebApiContext.Add(newProduct);
                productWebApiContext.SaveChanges();
            }

            return(Ok());
        }
예제 #5
0
        public IActionResult InsertProductsById([FromBody] Product product)
        {
            ProductWebApiContext productWebApiContext = new ProductWebApiContext(conn.DefaultConnection);

            if (product == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var maxId      = (from p in productWebApiContext.Product select p.Id).Max();
            var newProduct = new Product {
                Id    = ++maxId,
                Name  = product.Name,
                Price = product.Price,
            };

            productWebApiContext.Add(newProduct);
            productWebApiContext.SaveChanges();
            return(Ok(newProduct));
        }