public async Task <IActionResult> Detail(int id) { ProductFormVm productFormVm = new ProductFormVm(); await _productApiClient.PuttRatingProduct(id, productFormVm); var products = await _productApiClient.GetProductById(id); return(View(products)); }
//[AllowAnonymous] public async Task <ActionResult <ProductVm> > CreateProduct([FromForm] ProductFormVm productFormVm) { var product = new Product { Name = productFormVm.Name, //Id = productVm.Id, CategoryId = productFormVm.CategoryId, Description = productFormVm.Description, Inventory = productFormVm.Inventory, Price = productFormVm.Price }; _context.Products.Add(product); await _context.SaveChangesAsync(); //Add image if ((productFormVm.Images != null) && (productFormVm.Images.Count > 0)) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); foreach (IFormFile file in productFormVm.Images) { string fileName = Guid.NewGuid().ToString() + "_" + file.FileName; string filePath = Path.Combine(uploadsFolder, fileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(fileStream); } Image nFile = new (); nFile.ImagePath = $"/images/{fileName}"; nFile.ProductId = product.Id; _context.Images.Add(nFile); await _context.SaveChangesAsync(); } } return(CreatedAtAction("GetProduct", new { id = product.Id }, new ProductVm { Name = product.Name, AverageStar = product.AverageStar, CategoryId = product.CategoryId, Description = product.Description, Inventory = product.Inventory, Price = product.Price, })); }
public async Task <ActionResult> Create(ProductFormVm model) { var product = new Product { Name = model.Name, Price = model.Price, Description = model.Description, BrandId = model.BrandId }; _myDbContext.Products.Add(product); await _myDbContext.SaveChangesAsync(); return(Accepted()); }
public async Task <ActionResult> Update(int id, ProductFormVm model) { var product = await _myDbContext.Products.FirstOrDefaultAsync(x => x.Id == id); if (product == null) { return(NotFound()); } product.Name = model.Name; product.Price = model.Price; product.Description = model.Description; await _myDbContext.SaveChangesAsync(); return(Accepted()); }
public async Task PuttRatingProduct(int ProId, ProductFormVm model) { var client = new HttpClient(); double avr = await _ratingApiClient.FindRatingByProduct(ProId); var product = await GetProductById(ProId); model = new ProductFormVm() { Name = product.ProductName, Price = product.Price, Image = product.Image, ImageFile = null, Description = product.Description, RatingAVG = avr, CategoryID = product.CategoryID }; await client.PutAsync(_configuration["local:BackEndUrl"] + "api/products/rating/" + ProId, new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json")); }
public async Task <ActionResult> CreateProduct([FromForm] ProductFormVm model) { if (model.ImageFile != null) { model.Image = await SaveImage(model.ImageFile); } var product = new Product { ProductName = model.Name, Description = model.Description, Price = model.Price, CategoryID = model.CategoryID, RatingAverage = model.RatingAVG, ProductImage = model.Image }; _dataContext.Products.Add(product); await _dataContext.SaveChangesAsync(); return(StatusCode(201)); }
public async Task <IActionResult> CreateProduct([FromForm] ProductFormVm productFormVm) { // Create product var result = await Mediator.Send(new Create.Command { product = productFormVm }); if (!result.IsSuccess) { return(HandleResult(result)); } // get id for adding picture var proId = result.Value; // Check if picture empty or not if ((productFormVm.Pictures == null)) { return(HandleResult(result)); } if (productFormVm.Pictures.Count < 1) { return(HandleResult(result)); } // Create picture foreach (var pic in productFormVm.Pictures) { var result2 = await Mediator.Send(new Add.Command { productId = proId, File = pic }); // Check error if (!result2.IsSuccess) { return(HandleResult(result2)); } } return(HandleResult(result)); }
public async Task <IActionResult> EditProduct(int id, [FromForm] ProductFormVm productFormVm) { productFormVm.Id = id; var result = await Mediator.Send(new Edit.Command { Product = productFormVm }); if (!result.IsSuccess) { return(HandleResult(result)); } // Check if picture empty or not if ((productFormVm.Pictures == null)) { return(HandleResult(result)); } if (productFormVm.Pictures.Count < 1) { return(HandleResult(result)); } foreach (var pic in productFormVm.Pictures) { var result2 = await Mediator.Send(new Add.Command { productId = productFormVm.Id, File = pic }); // Check error if (!result2.IsSuccess) { return(HandleResult(result2)); } } return(HandleResult(result)); }
public async Task <ActionResult> UpdateRatingProduct(int id, ProductFormVm model) { if (model.ImageFile != null) { model.Image = await SaveImage(model.ImageFile); } var product = await _dataContext.Products.FirstOrDefaultAsync(x => x.ProductID == id); if (product == null) { return(NotFound()); } product.ProductName = model.Name; product.Description = model.Description; product.Price = model.Price; product.CategoryID = model.CategoryID; product.RatingAverage = model.RatingAVG; product.ProductImage = model.Image; await _dataContext.SaveChangesAsync(); return(NotFound()); }