Пример #1
0
        public void PostProduct_ShouldReturnSameProduct()
        {
            var controller = new ProductsController(new TestWebAppContext());

            var item = new PutProductsDto
            {
                Id        = Guid.NewGuid().ToString(),
                Timestamp = DateTime.Now,
                Products  = new List <Product>
                {
                    new Product {
                        Id = 1, Name = "Test1", Quantity = 20, SaleAmount = 10
                    }
                }
            };

            Task.Run(async() =>
            {
                var result = await controller.PostProduct(item) as CreatedAtRouteNegotiatedContentResult <PutProductsDto>;

                Assert.IsNotNull(result);
                Assert.AreEqual(result.RouteName, "DefaultApi");
                Assert.AreEqual(result.RouteValues["id"], result.Content.Id);
                Assert.AreEqual(result.Content.Timestamp, item.Timestamp);
            });
        }
Пример #2
0
        public async Task <IHttpActionResult> PostProduct(PutProductsDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            foreach (var product in dto.Products)
            {
                db.Products.Add(product);
                await db.SaveChangesAsync();
            }

            return(CreatedAtRoute("DefaultApi", new { id = dto.Id }, dto));
        }