Ejemplo n.º 1
0
        public IHttpActionResult Get([FromUri] int id)
        {
            try
            {
                //throw new ArgumentNullException("this is a test");
                Product product;
                var     productsRepository = new ProductsRepository();

                if (id > 0)
                {
                    var products = productsRepository.Retrieve();
                    product = products.FirstOrDefault(p => p.ProductId == id);
                    if (product == null)
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    product = productsRepository.Create();
                }

                return(Ok(product));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Ejemplo n.º 2
0
        public async Task Delete_Existing_RemovesFromDatabase()
        {
            var opt = new DbContextOptionsBuilder <ProductContext>()
                      .UseInMemoryDatabase(nameof(Delete_Existing_RemovesFromDatabase))
                      .Options;
            var     request = new ProductRequest("Coconut", 0.5f);
            Product createdProduct;

            await using (var ctx = new ProductContext(opt))
            {
                var service = new ProductsRepository(ctx);
                createdProduct = await service.Create(request);
            }

            await using (var ctx = new ProductContext(opt))
            {
                var service = new ProductsRepository(ctx);
                await service.Delete(createdProduct.Id);
            }

            await using (var ctx = new ProductContext(opt))
            {
                Assert.AreEqual(0, ctx.Products.Count());
            }
        }
Ejemplo n.º 3
0
        public async Task Get_Existing_ReturnsProduct()
        {
            var opt = new DbContextOptionsBuilder <ProductContext>()
                      .UseInMemoryDatabase(nameof(Get_Existing_ReturnsProduct))
                      .Options;
            var     request = new ProductRequest("Coconut", 0.5f);
            Product createdProduct;
            Product fetchedProduct;

            await using (var ctx = new ProductContext(opt))
            {
                var service = new ProductsRepository(ctx);
                createdProduct = await service.Create(request);
            }

            await using (var ctx = new ProductContext(opt))
            {
                var service = new ProductsRepository(ctx);
                fetchedProduct = await service.Get(createdProduct.Id);
            }

            await using (var ctx = new ProductContext(opt))
            {
                Assert.AreEqual(1, ctx.Products.Count());
                Assert.AreEqual(0.5f, ctx.Products.Single().Price);
                Assert.AreEqual(false, fetchedProduct.Created);
            }
        }
        public async Task <ActionResult <Products> > PostProducts(Products products)
        {
            await Task.Run(() => _productsRepository.Create(products));

            await Task.Run(() => _productsRepository.Save());

            return(CreatedAtAction("GetProducts", new { id = products.IdProducts }, products));
        }
        public HttpResponseMessage AddProduct(ProductsDto product)
        {
            var repo   = new ProductsRepository();
            var result = repo.Create(product);

            if (result)
            {
                return(Request.CreateResponse(HttpStatusCode.Created));
            }
            return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Could not create product, try again later..."));
        }
 public bool CreateProduct(Products products)
 {
     try
     {
         productsRepository.Create(products);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Ejemplo n.º 7
0
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                Product created = _repository.Create(product);

                return(RedirectToAction("Details", new { id = created.Id }));
            }

            IncludeSubCategoriesListItems();
            return(View(product));
        }
Ejemplo n.º 8
0
        //新增產品
        public void Create(Products model)
        {
            string Id     = productsRepository.GetNewId();
            var    split  = Id.Split('A');
            string numner = (Convert.ToInt32(split[1]) + 1).ToString();

            while (numner.Length < 3)
            {
                numner = "0" + numner;
            }
            model.ProductID = "A" + numner;
            productsRepository.Create(model);
        }
        public void CreateTest()
        {
            var     productRepository = new ProductsRepository(new FakeContextFactory());
            Fixture fixture           = new Fixture();
            var     product           = fixture.Create <Product>();

            var created = productRepository.Create(product);

            var found = productRepository.GetById(created.Id);

            Assert.NotNull(found);
            Assert.Equal(created.Id, found.Id);
        }
Ejemplo n.º 10
0
 public ActionResult Create(Product model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _repo.Create(model);
             return(RedirectToAction("Index"));
         }
     }
     catch (Exception ex)
     {
     }
     return(View(model));
 }
Ejemplo n.º 11
0
        public void ProductsRepositoryTests_Create()
        {
            ProductsRepository repository = new ProductsRepository();
            var model = new Products
            {
                Product_ID   = 5,
                Product_Name = "機能外套",
                UnitPrice    = 500,
                CategoryName = "外套",
                Gender       = "女"
            };

            repository.Create(model);
            var result = repository.GetByProduct_ID(5);

            Assert.IsTrue(result != null);
        }
Ejemplo n.º 12
0
        public async Task Create_AddsToDatabase()
        {
            var opt = new DbContextOptionsBuilder <ProductContext>()
                      .UseInMemoryDatabase(nameof(Create_AddsToDatabase))
                      .Options;
            var request = new ProductRequest("Coconut", 0.5f);

            await using (var ctx = new ProductContext(opt))
            {
                var service = new ProductsRepository(ctx);
                await service.Create(request);
            }

            await using (var ctx = new ProductContext(opt))
            {
                Assert.AreEqual(1, ctx.Products.Count());
                Assert.AreEqual(0.5f, ctx.Products.Single().Price);
            }
        }
        public void CreateTest()
        {
            Products model = new Products()
            {
                ProductID    = 5,
                ProductName  = "高腰開岔長裙",
                UnitPrice    = 200,
                UnitsInStock = 50,
                CategoryID   = 3,
                Size         = "S",
                Color        = "Red",
                Uptime       = new DateTime(2018, 05, 10)
            };
            ProductsRepository products = new ProductsRepository();

            products.Create(model);
            var list = products.FindByID(model.ProductID.ToString());

            Assert.IsTrue(list != null);
        }
 internal Product Create(Product newProd)
 {
     return(_repo.Create(newProd));
 }
Ejemplo n.º 15
0
 public IActionResult Post([FromBody] Product product)
 {
     return(Ok(_productsRepository.Create(product)));
     //return new CreatedAtRouteResult("Get", new { id = product.Id }, product);
 }
 public void Create(Products model)
 {
     repository.Create(model);
 }
Ejemplo n.º 17
0
 public Product Create(Product newProduct)
 {
     newProduct.Id = _repo.Create(newProduct);
     return(newProduct);
 }
Ejemplo n.º 18
0
 public Response Post([FromBody] Products value)
 {
     return(repo.Create(value));
 }
Ejemplo n.º 19
0
        public ActionResult Create(Product model)
        {
            _productRepository.Create(model);

            return(RedirectToAction("Index"));
        }