Ejemplo n.º 1
0
        public async Task <ProductVm> Update(Guid key, ProductVm updatedProductVm)
        {
            if (key != updatedProductVm.Guid)
            {
                return(null);
            }

            var product = _database.Products.SingleOrDefault(p => p.Guid == key);

            if (product == null)
            {
                return(null);
            }

            Mapper.Map(updatedProductVm, product);

            if (updatedProductVm.SupplierId != product.Supplier?.Guid)
            {
                var supplier = updatedProductVm.SupplierId != null?_database.Suppliers.SingleOrDefault(s => s.Guid == updatedProductVm.SupplierId) : null;

                product.Supplier = supplier;
            }
            _database.Products.Attach(product);
            _database.Entry(product).State = EntityState.Modified;
            _database.SaveChanges();

            return(await Task.FromResult(updatedProductVm));
        }
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public ActionResult AddProduct(EditProductViewModel model, HttpPostedFileBase file)
        {
            if (model.Product.ProductId > 0)
            {
                //product modification
                _db.Entry(model.Product).State = EntityState.Modified;
                _db.SaveChanges();
                return(RedirectToAction("AddProduct", new { confirm = true }));
            }
            if (file != null && file.ContentLength > 0)
            {
                if (ModelState.IsValid)
                {
                    //var fileExt = Path.GetExtension(file.FileName);
                    //var filename = Guid.NewGuid() + fileExt;
                    var filename = Path.GetFileName(file.FileName);

                    if (filename != null)
                    {
                        var path = Path.Combine(Server.MapPath("~/Content/Products"), filename);
                        file.SaveAs(path);
                    }

                    model.Product.ImageFile = filename;
                    model.Product.AddTime   = DateTime.Now;

                    _db.Entry(model.Product).State = EntityState.Added;
                    _db.SaveChanges();

                    return(RedirectToAction("AddProduct", new { confirm = true }));
                }
                var categories = _db.Categories.ToList();

                model.Categories = categories;

                var product = _db.Products.Where(a => a.Hidden).ToList();

                model.Products = product;

                return(View(model));
            }

            else
            {
                ModelState.AddModelError("", "Nie wskazano pliku !");

                var categories = _db.Categories.ToList();

                model.Categories = categories;

                var product = _db.Products.Where(a => a.Hidden).ToList();

                model.Products = product;

                return(View(model));
            }
        }
Ejemplo n.º 4
0
        public ActionResult DeleteProduct(string id)
        {
            Product product = new Product {
                Id = id
            };

            pc.Entry(product).State = EntityState.Deleted;
            pc.SaveChanges();

            return(Json(product));
        }
Ejemplo n.º 5
0
 public bool Add(T entity)
 {
     try
     {
         _db.Entry(entity).State = EntityState.Added;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        /// <summary>
        /// Support for updating products
        /// </summary>
        public HttpResponseMessage Put([FromODataUri] int key, Product update)
        {
            if (!_db.Products.Any(p => p.ID == key))
            {
                throw ODataErrors.EntityNotFound(Request);
            }
            update.ID = key; // ignore the key in the entity use the key in the URL.

            _db.Products.Attach(update);
            _db.Entry(update).State = EntityState.Modified;
            _db.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.NoContent));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Support for updating products
        /// </summary>
        public HttpResponseMessage Put(int id, Product update)
        {
            if (!_db.Products.Any(p => p.ID == id))
            {
                throw ODataErrors.EntityNotFound(Request);
            }
            update.ID = id; // ignore the ID in the entity use the ID in the URL.

            _db.Products.Attach(update);
            _db.Entry(update).State = System.Data.EntityState.Modified;
            _db.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.NoContent));
        }
Ejemplo n.º 8
0
        public IHttpActionResult PutSubjectOfLabor(int id, SubjectOfLabor subjectOfLabor)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != subjectOfLabor.Id)
            {
                return(BadRequest());
            }

            db.Entry(subjectOfLabor).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SubjectOfLaborExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 9
0
        public void UpdateDifDBContext_WhenCalled_ExpectDbUpdateConcurrencyException()
        {
            var logFactory = new LoggerFactory();

            logFactory.AddProvider(new SqliteLoggerProvider());


            var context1 = new ProductsContext(new DbContextOptionsBuilder <ProductsContext>()
                                               .UseSqlite("Data Source=products.db").UseLoggerFactory(logFactory).Options);
            var context2 = new ProductsContext(new DbContextOptionsBuilder <ProductsContext>()
                                               .UseSqlite("Data Source=products.db").UseLoggerFactory(logFactory).Options);

            context1.Database.ExecuteSqlCommand(
                @"UPDATE Products SET RowVersion = randomblob(8) WHERE RowVersion = null");

            var productFromContext1 = context1.Products.AsNoTracking().FirstOrDefault(p => p.ProductId == 1);
            var productFromContext2 = context1.Products.AsNoTracking().FirstOrDefault(p => p.ProductId == 1);

            productFromContext1.Description = DateTime.Now.ToString();
            productFromContext2.Description = DateTime.UtcNow.ToString();

            try
            {
                context1.Entry(productFromContext1).State = EntityState.Modified;
                var count = context1.SaveChanges();
                productFromContext1 = context1.Products.FirstOrDefault(p => p.ProductId == 1);
                context2.Entry(productFromContext2).State = EntityState.Modified;
                count = context2.SaveChanges();
                Assert.True(false);
            }
            catch (DbUpdateConcurrencyException e)
            {
                Assert.True(true);
            }
        }
Ejemplo n.º 10
0
        public async Task <Product> Update(int id, Product product)
        {
            _context.Entry(product).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(product);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Support for replacing a ProductFamily
        /// </summary>
        public async Task <IActionResult> Put([FromODataUri] int key, ProductFamily family)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != family.Id)
            {
                return(BadRequest());
            }

            _db.Entry(family).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_db.ProductFamilies.Any(p => p.Id == key))
                {
                    return(NotFound());
                }
                throw;
            }

            return(Ok(family));
        }
Ejemplo n.º 12
0
        public override async Task <ProductModel> UpdateProduct(UpdateProductRequest request, ServerCallContext context)
        {
            // 1. Mapping ProductModel to Product
            var product = _mapper.Map <Product>(request.Product);

            bool isExist = await _productsContext.Product.AnyAsync(p => p.ProductId == product.ProductId);

            if (!isExist)
            {
                _logger.LogError($"Error: Product with ID = {product.ProductId} is not found.");
                return(new ProductModel());
            }

            _productsContext.Entry(product).State = EntityState.Modified;

            try
            {
                // 2. Update Product to Database
                await _productsContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exception)
            {
                _logger.LogError($"Error: {exception.Message}");
                return(new ProductModel());
            }

            // 3. Resturn response: ProductModel
            return(_mapper.Map <ProductModel>(product));
        }
Ejemplo n.º 13
0
        public override async Task <ProductModel> UpdateProduct(UpdateProductRequest request,
                                                                ServerCallContext context)
        {
            var product = _mapper.Map <Product>(request.Product);

            bool isExists = await _productContext.Products
                            .AnyAsync(p => p.ProductId == product.ProductId);

            if (!isExists)
            {
                throw new RpcException(new Status(StatusCode.NotFound, "There is no product"));
            }

            _productContext.Entry(product).State = EntityState.Modified;
            try
            {
                await _productContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                throw;
            }

            var productModel = _mapper.Map <ProductModel>(product);

            return(productModel);
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> PutProductsDetails([FromRoute] int id, [FromBody] ProductsDetails productsDetails)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != productsDetails.id)
            {
                return(BadRequest());
            }

            _context.Entry(productsDetails).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductsDetailsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 15
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 16
0
 public async Task <IHttpActionResult> Put([FromODataUri] int key, Product update)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (key != update.Id)
     {
         return(BadRequest());
     }
     db.Entry(update).State = EntityState.Modified;
     try
     {
         await db.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         if (!ProductExists(key))
         {
             return(NotFound());
         }
         else
         {
             throw;
         }
     }
     return(Updated(update));
 }
        public async Task <IActionResult> Update(long id, Product product)
        {
            try
            {
                if (id != product.Id)
                {
                    //BadRequest
                    //Http status Code: 400
                    //The Product object's Id value doesn't match the id value provided in the route.
                    return(BadRequest());
                }

                _context.Entry(product).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                //NoContent
                //Http status Code: 204
                //The product was updated in the database.
                return(NoContent());
            }
            catch (Exception)
            {
                //BadRequest
                //Http status Code: 400
                //The Product object provided in the request body is invalid.
                return(BadRequest());
            }
        }
Ejemplo n.º 18
0
 internal void UpdateProduct(Product product)
 {
     using (var db = new ProductsContext())
     {
         db.Entry(product).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
        public void UpdateProduct(Guid productId, Product changedProduct)
        {
            if (changedProduct == null)
            {
                throw new ArgumentNullException(nameof(changedProduct));
            }

            _dbContext.Entry(changedProduct).State = EntityState.Modified;
        }
Ejemplo n.º 20
0
        public async Task <OrderVm> Update(Guid key, OrderVm updatedOrderVm)
        {
            if (key != updatedOrderVm.Reference)
            {
                return(null);
            }

            var order = _database.Orders
                        .Include(o => o.ProductLines)
                        .SingleOrDefault(o => o.Guid == key);

            if (order == null)
            {
                return(null);
            }

            _database.ProductLines.RemoveRange(order.ProductLines);

            Mapper.Map(updatedOrderVm, order);

            var newProductList = new List <ProductLine>();

            foreach (var newProduct in updatedOrderVm.ProductLines)
            {
                var p = new ProductLine
                {
                    Price     = newProduct.Price,
                    Quantity  = newProduct.Quantity,
                    ProductId = _database.Products.Single(x => x.Guid == newProduct.ProductId).Id,
                };

                if (p.ProductId != 0)
                {
                    newProductList.Add(p);
                }
            }
            order.ProductLines = newProductList;

            _database.Orders.Attach(order);
            _database.Entry(order).State = EntityState.Modified;
            await _database.SaveChangesAsync();

            return(updatedOrderVm);
        }
Ejemplo n.º 21
0
 public ActionResult Edit([Bind(Include = "ID,Name")] Manufacturer manufacturer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(manufacturer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(manufacturer));
 }
Ejemplo n.º 22
0
 public ActionResult Edit([Bind(Include = "Id,CategoryName")] Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }
Ejemplo n.º 23
0
 public ActionResult Edit([Bind(Include = "ID,ComputerPartType")] PartType partType)
 {
     if (ModelState.IsValid)
     {
         db.Entry(partType).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(partType));
 }
Ejemplo n.º 24
0
 public ActionResult Edit([Bind(Include = "Id,ProductName,Price,Caregory,UnitPrice,UnitRemain")] Products products)
 {
     if (ModelState.IsValid)
     {
         db.Entry(products).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(products));
 }
 public ActionResult Edit([Bind(Include = "Id,Name,Price")] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(product));
 }
Ejemplo n.º 26
0
 public ActionResult Edit([Bind(Include = "ProductId,ProductName,Quantity,CategoryId")] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CategoryId = new SelectList(db.Category, "Id", "CategoryName", product.CategoryId);
     return(View(product));
 }
Ejemplo n.º 27
0
 public ActionResult Edit([Bind(Include = "ID,SKU,ProductName,Description,PartTypeID,Quantity,Price,ManufacturerID")] Products products)
 {
     if (ModelState.IsValid)
     {
         db.Entry(products).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ID", "Name", products.ManufacturerID);
     ViewBag.PartTypeID     = new SelectList(db.PartTypes, "ID", "ComputerPartType", products.PartTypeID);
     return(View(products));
 }
Ejemplo n.º 28
0
        public async Task <SupplierVm> Update(Guid id, SupplierVm updateSupplierVm)
        {
            if (id != updateSupplierVm.Guid)
            {
                return(null);
            }

            var supplier = _database.Suppliers.SingleOrDefault(s => s.Guid == id);

            if (supplier == null)
            {
                return(null);
            }

            Mapper.Map(updateSupplierVm, supplier);
            _database.Suppliers.Attach(supplier);
            _database.Entry(supplier).State = EntityState.Modified;
            await _database.SaveChangesAsync();

            return(updateSupplierVm);
        }
Ejemplo n.º 29
0
        public async Task <IActionResult> Update(long id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 30
0
 public void Update(Product product)
 {
     using (productContext = new ProductsContext())
     {
         var item = productContext.Products.Find(product.Id);
         if (item != null)
         {
             productContext.Entry(item).CurrentValues.SetValues(product);
             productContext.SaveChanges();
         }
     }
 }