public async Task <IActionResult> Create(ProductViewmodel model) { try { var division = await context.Divisions.FindAsync(model.DivisionId); if (division == null) { return(NotFound(new { message = $"Company division with ID {model.DivisionId} was not found." })); } var product = new Product { Division = division, Duration = model.Duration, Name = model.Name, Price = model.Price }; await context.Products.AddAsync(product); await context.SaveChangesAsync(); return(Ok(product)); } catch (Exception ex) { _logger.LogError($"Unable to create product", ex); return(BadRequest(ex.ToString())); } }
public async Task <IActionResult> Edit([FromBody] ProductViewmodel model, int id) { try { var division = await context.Divisions.FindAsync(model.DivisionId); if (division == null) { return(NotFound(new { message = $"Company division with ID {model.DivisionId} was not found." })); } var product = await context.Products.FindAsync(id); if (product == null) { return(NotFound(new { message = $"Product with ID {id} was not found." })); } product.Division = division; product.Duration = model.Duration; product.Name = model.Name; product.Price = model.Price; await context.SaveChangesAsync(); return(Ok(product)); } catch (Exception ex) { _logger.LogError($"Unable to update product", ex); return(BadRequest(ex.ToString())); } }
private async Task <ProductViewmodel> GetProductAsync(int id) { var product = await context.Products .Include(x => x.Division) .Include(x => x.Division.Company).FirstOrDefaultAsync(x => x.Id == id); var model = new ProductViewmodel { Company = new CompanyViewmodel { Id = product.Division.Company.Id, Name = product.Division.Company.Name }, CompanyId = product.Division.Company.Id, Division = new DivisionViewmodel { Id = product.Division.Id, Name = product.Division.Name }, DivisionId = product.Division.Id, Duration = product.Duration, Id = product.Id, Name = product.Name, Price = product.Price }; return(model); }
public ActionResult Edit(ProductViewmodel Model) { if (ModelState.IsValid) { try { #region Product Save var _product = _repository.GetById(Model.ProductID); _product.ManuFacturerID = Model.ManufacturerID; _product.ProductCode = Model.ProductCode; _product.ProductName = Model.ProductName; _product.CateogryID = Model.CategoryID; _product.SupplierID = Model.SupplierID; _product.ManuFacturerID = Model.ManufacturerID; _product.SKU = Model.SKU; _product.UpdatedDate = DateTime.Now; _product.UnitOfMeasuresId = Model.UnitOfMeasureID; _product.Description = Model.Description; _repository.Save(); #endregion return(Json(new { Success = true, ex = "", data = "", id = _product.Id })); } catch (Exception ex) { return(Json(new { Success = false, ex = ex.Message.ToString(), data = "" })); } } return(Json(new { Success = true, ex = "", data = "" })); }
public IActionResult Edit(ProductViewmodel productViewmodel) { var files = HttpContext.Request.Form.Files; string webRootPath = _webHostEnvironment.WebRootPath; var objProduct = _appDbContext.Products.AsNoTracking().FirstOrDefault(pro => pro.Id == productViewmodel.Product.Id); if (files.Count > 0) { string upload = webRootPath + CommonDefault.ImagePath; string fileName = Guid.NewGuid().ToString(); string extension = Path.GetExtension(files[0].FileName); using (var fileStream = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } productViewmodel.Product.ImageUrl = fileName + extension; } else { productViewmodel.Product.ImageUrl = objProduct.ImageUrl; } _appDbContext.Products.Update(productViewmodel.Product); _appDbContext.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(ProductViewmodel Model) { try { #region Product Save var _product = new Product(); _product.ManuFacturerID = Model.ManufacturerID; _product.ProductCode = Model.ProductCode; _product.ProductName = Model.ProductName; _product.CateogryID = Model.CategoryID; _product.SupplierID = CurrentUserData.SupplierID; _product.ManuFacturerID = Model.ManufacturerID; _product.SKU = Model.SKU; _product.CreatedDate = DateTime.Now; _product.UpdatedDate = DateTime.Now; _product.UnitOfMeasuresId = Model.UnitOfMeasureID; _product.Description = Model.Description; _repository.Insert(_product); _repository.Save(); SaveProductDetail(_product); #endregion return(Json(new { Success = true, ex = "", data = "", id = _product.Id })); } catch (Exception ex) { return(Json(new { Success = false, ex = ex.Message.ToString(), data = "", id = 0 })); } }
// GET: Product public ActionResult Index() { ProductViewmodel model = new ProductViewmodel(); model.products = _context.Products.ToList(); return(View(model)); }
// GET: Admin // =============== Add new product ========== public ActionResult create() { var c = db.Category.ToList(); ProductViewmodel p = new ProductViewmodel { Category = c }; return(View(p)); }
public IActionResult Edit(int?id) { ProductViewmodel productViewmodel = new ProductViewmodel() { Product = _appDbContext.Products.Find(id), CategoryeSelectList = _appDbContext.Categories.Select(item => new SelectListItem { Text = item.CategoryName, Value = item.Id.ToString() }) }; return(View(productViewmodel)); }
public async Task OnGetAsync(int?id) { try { if (id != null) { _ = int.TryParse(id.ToString(), out int productId); Product = await GetProductAsync(productId); } } catch (Exception ex) { _logger.LogError(ex, "[Products DeleteModel] OnGet failed"); } }
public IActionResult Create(ProductViewmodel productViewmodel) { var files = HttpContext.Request.Form.Files; string webRootPath = _webHostEnvironment.WebRootPath; string upload = webRootPath + CommonDefault.ImagePath; string fileName = Guid.NewGuid().ToString(); string extension = Path.GetExtension(files[0].FileName); using (var fileStrem = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create)) { files[0].CopyTo(fileStrem); } productViewmodel.Product.ImageUrl = fileName + extension; _appDbContext.Products.Add(productViewmodel.Product); _appDbContext.SaveChanges(); return(RedirectToAction("Index")); }
public ProductViewmodel GetProduct(int productID) { var _ProductViewModel = new ProductViewmodel(); var _Product = _repository.GetById(productID); _ProductViewModel.ProductID = _Product.Id; _ProductViewModel.SupplierID = _Product.SupplierID; _ProductViewModel.UnitOfMeasureID = _Product.UnitOfMeasuresId; _ProductViewModel.ManufacturerID = _Product.ManuFacturerID; _ProductViewModel.SKU = _Product.SKU; _ProductViewModel.CategoryID = _Product.CateogryID; _ProductViewModel.ProductName = _Product.ProductName; _ProductViewModel.ProductCode = _Product.ProductCode; _ProductViewModel.Description = _Product.Description; _ProductViewModel.IDSKU = _Product.IDSKU; return(_ProductViewModel); }
public ActionResult create(ProductViewmodel p, HttpPostedFileBase imgfile) { if (ModelState.IsValid) { if (imgfile != null) { string pic = System.IO.Path.GetFileName(imgfile.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/upload/"), pic); imgfile.SaveAs(path); p.Product.image = "~/Content/upload/" + pic; } db.Product.Add(p.Product); db.SaveChanges(); var category = db.Category.SingleOrDefault(c => c.id == p.Product.category_id); category.number_of_products++; db.Entry(category).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("ViewProduct")); }
public async Task <IActionResult> Get(int id) { try { var product = await context.Products .Include(x => x.Division) .Include(x => x.Division.Company).FirstOrDefaultAsync(x => x.Id == id); if (product == null) { return(NotFound(new { message = $"Product with ID {id} was not found." })); } var model = new ProductViewmodel { Company = new CompanyViewmodel { Id = product.Division.Company.Id, Name = product.Division.Company.Name }, CompanyId = product.Division.Company.Id, Division = new DivisionViewmodel { Id = product.Division.Id, Name = product.Division.Name }, DivisionId = product.Division.Id, Duration = product.Duration, Id = product.Id, Name = product.Name, Price = product.Price }; return(Ok(model)); } catch (Exception ex) { _logger.LogError($"Unable to get product", ex); return(BadRequest(ex.ToString())); } }
public ActionResult ProductDetails(ProductViewmodel Model) { try { // DeleteOldAttributes(Model.ProductID); var _ProductAttributes = _Attributerepository.GetAll().Where(x => x.ProductId == Model.ProductID).ToList(); var _productRel = new ProductAttributeWithQuantity(); #region Product Attributes Model.allAttributes = (Model.allAttributes == null ? new List <ProductAttributeModel>() : Model.allAttributes); if (Model.allAttributes.Count() > 0) { foreach (var _item in Model.allAttributes) { var _tempProductRel = _ProductAttributes != null && _ProductAttributes.Count() > 0 ? _ProductAttributes.Where(x => x.Id == _item.Id).FirstOrDefault() : new ProductAttributeWithQuantity(); _tempProductRel = _tempProductRel == null ? new ProductAttributeWithQuantity() : _tempProductRel; _productRel = _tempProductRel; _productRel.IsAvailable = true; _productRel.AttributeValues = GetAttributeValues(_item.ColumnsData); _productRel.ProductPrice = _item.price; _productRel.ProductQuantity = _item.Quantity; _productRel.ProductId = Model.ProductID; _productRel.UnitInStock = _item.Quantity; _productRel.Weight = _item.weight; _productRel.UnitWeight = _item.weight; _productRel.StatusId = _item.StatusId; _productRel.IsFeatured = _item.IsFeatured; _productRel.lowQuantityThreshold = _item.lowQuantityThreshold; _productRel.highQuantityThreshold = _item.highQuantityThreshold; _productRel.IsActive = true; if (_productRel.Id == 0) { _Attributerepository.Insert(_productRel); } _Attributerepository.Save(); #region Product Image _item.Images = (_item.Images == null ? new List <ProductImagesViewModel>() : _item.Images); if (_item.Images.Count() > 0) { foreach (var _image in _item.Images) { if (_image.ID == 0) { byte[] bitmap = _image.bytestring; var _imageName = ""; if (bitmap != null) { _imageName = Guid.NewGuid().ToString() + ".png"; var _Path = System.Web.Hosting.HostingEnvironment.MapPath("~/ProductImages/"); using (Image image = Image.FromStream(new MemoryStream(bitmap))) { var _CompletePath = _Path + _imageName; image.Save(_CompletePath, ImageFormat.Png); // Or Png } } var _productImage = new ProductImages(); _productImage.ImageName = _image.FileName; _productImage.ImagePath = _imageName; _productImage.ProductQuantityId = _productRel.Id; //_productImage.ProductId = Model.ProductID; _productImage.CreatedDate = DateTime.Now; _productImage.UpdatedDate = DateTime.Now; _Imagesrepository.Insert(_productImage); _Imagesrepository.Save(); } } } #endregion } } #endregion return(Json(new { Success = true, ex = "", data = "" })); } catch (Exception ex) { return(Json(new { Success = false, ex = ex.Message.ToString(), data = "" })); } }