예제 #1
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));
        }
예제 #2
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());
        }
예제 #3
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));
        }