public async Task <IActionResult> PutProduct(int id, [FromForm] ProductFormDTO dto) { if (id != dto.Id || !ModelState.IsValid) { return(BadRequest(ModelState)); } try { Product product = await _context.Product .Include(_ => _.ProductModifier) .FirstOrDefaultAsync(_ => _.Id == id); _mapper.Map(dto, product); _context.Entry(product).State = EntityState.Modified; _productService.UpsertDeleteProductModifiers(product, dto); await SaveProduct(product, dto); } catch (DbUpdateConcurrencyException) { if (!EntityExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private async Task SaveProduct(Product product, ProductFormDTO dto) { await _context.SaveChangesAsync() .ContinueWith(async(task) => { await productContent.HandleFile(product, dto); }); }
public void Create(ProductFormDTO product) { Product dbModel = mapper.Map <Product>(product); services.Product.Create(dbModel); services.Commit(); }
public static List <ProductModifier> CreateListOfProductModifier(ProductFormDTO dto) { return(dto.ModifierIds .Select(modId => new ProductModifier() { ProductId = dto.Id, ModifierId = modId }).ToList()); }
public void Edit(ProductFormDTO model) { var dbModel = services.Product.Get(model.Id); if (dbModel == null) { throw new Exception("Object not found"); } mapper.Map <ProductFormDTO, Product>(model, dbModel); services.Product.Update(dbModel); services.Commit(); }
public async Task <ActionResult <Product> > PostProduct([FromForm] ProductFormDTO dto) { if (ModelState.IsValid) { Product product = _mapper.Map <Product> (dto); this._context.Product.Add(product); await SaveProduct(product, dto); return(CreatedAtAction(nameof(PostProduct), new { id = product.Id }, product)); } else { return(BadRequest(ModelState)); } }
public async Task HandleFile(Product product, ProductFormDTO dto) { if (dto.ImageDeleted) { new Thread(() => fileContentManager .RemoveFileFromDisk(new string[] { product.MainImage })) .Start(); product.MainImage = null; } else if (dto.MainImageForm != null) { //save to disk var fileNames = await fileContentManager.SaveImages(new IFormFile[] { dto.MainImageForm }); product.MainImage = fileNames[0]; } }
public void UpsertDeleteProductModifiers(Product product, ProductFormDTO dto) { var productModifierClient = ProductMapper.CreateListOfProductModifier(dto); UpsertDeleteProductModifiers(product, productModifierClient); }