public async Task <string> PostProduct() { string issuccess = null; try { string dbPath = null; IFormFile file = null; var Name = Request.Form["Name"]; var Price = decimal.Parse(Request.Form["Price"]); var CategoryId = 1; if (Request.Form.Files.Count > 0) { file = Request.Form.Files[0]; } if (file != null) { var folderName = Path.Combine(@"Images"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); dbPath = Path.Combine(folderName, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } } } else { dbPath = @"Images/favicon.ico"; } Product item = new Product { Name = Name, Active = true, CreatedDT = DateTime.Now, Price = Price, Photo = dbPath }; _context.Product.Add(item); await _context.SaveChangesAsync(); Cat_Product item2 = new Cat_Product { CategoryId = CategoryId, ProductId = item.Id, Active = true, }; _context.Cat_Product.Add(item2); await _context.SaveChangesAsync(); issuccess = "success"; } catch { issuccess = "failed"; } return(issuccess); }
public async Task <string> PutProduct() { string issuccess = null; try { var Name = Request.Form["Name"]; var Price = decimal.Parse(Request.Form["Price"]); var CategoryId = 1; var id = int.Parse(Request.Form["id"]); string dbPath = null; IFormFile file = null; var ProductById = _context.Product.FirstOrDefault(x => x.Id == id); if (ProductById == null) { } if (Request.Form.Files.Count > 0) { file = Request.Form.Files[0]; } if (file != null) { if (System.IO.File.Exists(ProductById.Photo)) { System.IO.File.Delete(ProductById.Photo); } var folderName = Path.Combine(@"Images"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); dbPath = Path.Combine(folderName, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } } else { dbPath = ProductById.Photo; } if (dbPath.Count() > 0) { ProductById.Photo = dbPath; } } ProductById.Name = Name; ProductById.Price = Price; ProductById.ModifiedDT = DateTime.Now; await _context.SaveChangesAsync(); var list = _context.Cat_Product.ToList(); var listOfCat_Products = _context.Cat_Product.Where(x => x.ProductId == ProductById.Id).ToList(); List <int> ListCatProduct = listOfCat_Products.Select(s => (int)s.ProductId).ToList(); var ListCat_ProductById = _context.Cat_Product.Where(x => x.Id == id).ToList(); if (ListCat_ProductById != null) { foreach (var item in ListCat_ProductById) { item.Active = false; await _context.SaveChangesAsync(); } } Cat_Product item2 = new Cat_Product { CategoryId = CategoryId, ProductId = ProductById.Id, Active = true, }; _context.Cat_Product.Add(item2); await _context.SaveChangesAsync(); issuccess = "success"; } catch { issuccess = "failed"; } return(issuccess); }