public async Task <IActionResult> Add([FromBody] ProductImageModelForm obj) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dataModel = _mapper.Map <ProductImage>(obj); _context.ProductImages.Add(dataModel); await _context.SaveChangesAsync(); return(CreatedAtAction("GetById", new { Id = obj.Id }, obj)); }
public async Task <IActionResult> UpdateImages([FromRoute] Guid proudctId, [FromForm] ProductImageModelForm obj) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var time = DateTime.Now.Ticks.ToString(); var product = _context.Products.FirstOrDefault(e => e.Id == proudctId); if (product == null) { product = new Product(); } //Getting Image var imageCover = obj.ImageCoverForm; //Saving Image on Server var imagePath = Path.Combine(_webHostEnvironment.WebRootPath, @"images"); var fullPathName = ""; if (imageCover != null) { var fileName = "img" + time + imageCover?.FileName; string fullPath = Path.Combine(imagePath, fileName); fullPathName = Path.Combine(@"images", fileName); var imageId = Guid.NewGuid(); using (var fileStream = new FileStream(fullPath, FileMode.Create)) { ProductImage productImage = new ProductImage { Id = imageId, Path = fullPathName }; product.ProductImages.Add(productImage); } } _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok(fullPathName)); }