public async Task <IActionResult> Create(AddBikeMakerViewModel model) { if (ModelState.IsValid) { var path = string.Empty; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile, "Makers"); } var bikeMakerEntity = _converterHelper.ToBikeMakerEntity(model, path); try { _context.BikeMakers.Add(bikeMakerEntity); } catch (System.Exception err) { ModelState.AddModelError(string.Empty, err.Message); return(View(model)); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> Edit(int id, AddBikeMakerViewModel model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { var path = model.ImageUrl; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile, "Makers"); //TODO: investigar borrar imagen anterior _imageHelper.DeleteImageAsync(model.ImageUrl); } var bikeMakerEntity = _converterHelper.ToBikeMakerEntity(model, path); try { _context.BikeMakers.Update(bikeMakerEntity); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException err) { ModelState.AddModelError(string.Empty, err.Message); } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public BikeMakerEntity ToBikeMakerEntity(AddBikeMakerViewModel model, string path) { return(new BikeMakerEntity { Id = model.Id, Name = model.Name, ImageUrl = path }); }