public async Task <IActionResult> Create(Product product) { if (product is null) { return(BadRequest()); } //model validation var newProduct = new Model.Product { Name = product.Name, Description = product.Description, ImageUrl = product.ImageUrl, Price = product.Price, Supplier = product.Supplier }; _dbContext.Add(newProduct); await _dbContext.SaveChangesAsync(); product.Id = newProduct.Id; return(CreatedAtAction(nameof(Get), new { id = newProduct.Id }, product)); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) //content-type { if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Product> Create(Product product) { _context.Add(product); await _context.SaveChangesAsync(); return(product); }
public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create([Bind("Id,Name,Price,Quantity,ShippingPrice")] Products products) { if (ModelState.IsValid) { _context.Add(products); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(products)); }
public async Task <IActionResult> Create([Bind("SowRatesId,SowWeight,TraysPerPack,CostPerTray, ProductsId,DateIn,DateOut")] SowRatesL sowRatesL) { if (ModelState.IsValid) { _context.Add(sowRatesL); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(sowRatesL)); }
public async Task <int> AddAsync(ProductInsertDto product) { var newProduct = _mapper.Map <Product>(product); var productAdded = await _productContext .Products .AddAsync(newProduct); await _productContext.SaveChangesAsync(); return(productAdded.Entity.ProductId); }
public async Task <IActionResult> Create([Bind("YieldId,Yield,CostPerTray,ProductsId,DateIn,DateOut")] Yields yields) { if (ModelState.IsValid) { _context.Add(yields); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ProductsId"] = new SelectList(_context.Products, "Id", "Id", yields.ProductsId); return(View(yields)); }
public async Task <ActionResult <Product> > Create([FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _productsDbContext.Products.AddAsync(product); await _productsDbContext.SaveChangesAsync(); return(Ok(product)); }
/// <summary> /// Store new product in database /// </summary> /// <param name="product"></param> /// <returns>Product object if successful, null if fail</returns> public async Task <Product> AddProductAsync(Product product) { try { _context.Product.Add(product); int rowsEffected = await _context.SaveChangesAsync(); return((rowsEffected > 0) ? product : null); } catch (Exception) { return(null); } }
public async void SaveList(List <T> list) { try { foreach (var record in list) { _context.Add(record); } await _context.SaveChangesAsync(); } catch (Exception) { throw; } }
public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } 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()); }
public async Task <IActionResult> CreateItem(ItemData data) { if (ModelState.IsValid) { await _context.Items.AddAsync(data); await _context.SaveChangesAsync(); return(CreatedAtAction("GetItem", new { data.Id }, data)); } return(new JsonResult("Something went wrong") { StatusCode = 500 }); }
public async Task MapDataAndSave(ProductAddViewModel viewModel) { if (viewModel != null) { var product = new Product(); var supplier = await db.Suppliers.Include(x => x.Products).Where(x => x.Id == viewModel.SupplierId).FirstOrDefaultAsync(); var manufacturer = await db.Manufacturers.SingleOrDefaultAsync(x => x.Id == viewModel.ManufacturerId); var category = await db.Categories.Include(x => x.Products).SingleOrDefaultAsync(x => x.Id == viewModel.CategoryId); product.Name = viewModel.Name; product.Price = viewModel.Price; product.Supplier = supplier; product.Manufacturer = manufacturer; product.Category = category; product.Description = viewModel.Description; Create(product); await db.SaveChangesAsync(); JsonCRUD.AddToJson(product, JsonPath.PathToProducts); return; } throw new System.ArgumentException(); }
public async Task <IActionResult> Post([FromBody] Product product) { try { await _context.AddAsync(product); var saved = await _context.SaveChangesAsync(); if (saved > 0) { return(CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product)); } return(BadRequest()); } catch (Exception exception) { return(BadRequest(exception)); } }
public async Task <IActionResult> Create([Bind("CategoryProductId,CategoryName")] CategoryProduct categoryProduct) { var max = _context.CategoryProducts.FirstOrDefault(); if (max == null) { categoryProduct.CategoryProductId = 1; } else { categoryProduct.CategoryProductId = _context.CategoryProducts.Max(item => item.CategoryProductId) + 1; } if (ModelState.IsValid) { _context.Add(categoryProduct); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(categoryProduct)); }
public async Task <IActionResult> Create([Bind("ProductId,Name,Description,CategoryProductId,Manufacturer,Supplier,Price")] Product product) { var max = _context.Products.FirstOrDefault(); if (max == null) { product.ProductId = 1; } else { product.ProductId = _context.Products.Max(item => item.ProductId) + 1; } if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <Product> CreateProductAsync(Product product) { try { if (IsProductModelValid(product)) { bool isProductExistInDb = await IsProductExistInDb(product.Id); if (!isProductExistInDb) { _context.Product.Add(product); var result = await _context.SaveChangesAsync(); if (result > 0) { return(product); } else { return(null); } } else { return(null); } } else { return(null); } } catch (Exception) { return(null); } }
public async Task <IActionResult> SaveCategory(string name, int id = 0) { Categories category = null; if (id == 0) { category = new Categories() { Name = name }; _pContext.Categories.Add(category); } else { category = _pContext.Categories.FirstOrDefault(f => f.Id == id); if (category != null) { category.Name = name; } } await _pContext.SaveChangesAsync(); return(Json(category)); }
public async Task <int> Handle(CreateProductCommand command, CancellationToken cancellationToken) { var product = new Product { Name = command.Name }; _context.Products.Add(product); try { await _context.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e); } return(product.Id); }
public async Task SaveChanges() { await _dbContext.SaveChangesAsync(); }
private static async Task DeleteAllProductsAsync(ProductsDbContext ctx) { ctx.Products.RemoveRange(ctx.Products.ToList()); await ctx.SaveChangesAsync(); }
/// <summary> /// Adds a product to the database /// </summary> /// <param name="product">product</param> /// <returns>Task</returns> public async Task AddProduct(Products product) { await _context.Products.AddAsync(product); await _context.SaveChangesAsync(); }
public async Task CompleteAsync() { await _context.SaveChangesAsync(); }
private static async Task InsertProductAsync(ProductsDbContext ctx, Product apple) { ctx.Products.Add(apple); await ctx.SaveChangesAsync(); }
public async void SaveAndUpdateContext() { await _context.SaveChangesAsync(); }