public void AddProduct(Product product)
 {
     if (product.ProductId == 0)
     {
         _context.Add(product);
     }
     else
     {
         _context.Update(product);
     }
     _context.SaveChanges();
 }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] Product product)
        {
            if (id != product.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Email,Password")] Customer customer)
        {
            if (id != customer.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Esempio n. 4
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,UserId,ProductId,Quantity,OrderDate,CancelDate")] Purchase purchase)
        {
            if (id != purchase.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(purchase);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PurchaseExists(purchase.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductId"] = new SelectList(_context.Product, "Id", "Name", purchase.ProductId);
            ViewData["UserId"]    = new SelectList(_context.Customer, "Id", "Email", purchase.UserId);
            return(View(purchase));
        }
        public async Task <ProductOption> UpdateAsync(ProductOption productOption)
        {
            if (productOption == null)
            {
                throw new BadRequestException();
            }
            _context.Update(productOption);
            await _context.SaveChangesAsync();

            return(productOption);
        }
Esempio n. 6
0
        public IActionResult Put(int id, [FromBody] Product product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            if (!db.Products.Any(x => x.Id == product.Id))
            {
                return(NotFound());
            }

            db.Update(product);
            db.SaveChanges();
            return(Ok(product));
        }
Esempio n. 7
0
        public async Task <TEntity> UpdateAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
            }

            try
            {
                RepositoryContext.Update(entity);
                await RepositoryContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be updated: {ex.Message}");
            }
        }
Esempio n. 8
0
        public async Task <IActionResult> Put(String id, [FromBody] ProductModel product)
        {
            Guid productId = id.AsObjectId().AsGuid();

            var errors = _productValidator.Validate(product);

            if (errors.Any())
            {
                return(BadRequest(errors));
            }

            var existentProduct = _productContext.Products.Find(productId);

            if (existentProduct == null)
            {
                return(BadRequest(new List <BusinessError>
                {
                    new BusinessError("The product could not be found to be updated", "Product")
                }));
            }

            existentProduct.Title       = product.Title;
            existentProduct.Price       = product.Price;
            existentProduct.Description = product.Description;
            existentProduct.Version++;

            _productContext.Update(existentProduct);
            await _productContext.SaveChangesAsync();

            _productUpdatedPublisher.Publish(new ProductUpdatedEvent
            {
                Id          = existentProduct.Id.AsObjectId(),
                Title       = existentProduct.Title,
                Price       = existentProduct.Price,
                Description = existentProduct.Description,
                Version     = existentProduct.Version
            });

            return(Ok(new ProductModel(existentProduct)));
        }