public IActionResult Edit(VillainEditViewModel viewModel) { if (ModelState.IsValid) { try { Repository.Instance.UpdateVillain(viewModel); TempData["MessageText"] = "Villain successfully updated!"; TempData["MessageSeverity"] = MessageSeverity.Ok; } catch (NotFoundException) { TempData["MessageText"] = "Villain not found!"; TempData["MessageSeverity"] = MessageSeverity.Error; } catch (InvalidInputException) { TempData["MessageText"] = "Villain with invalid fields!"; TempData["MessageSeverity"] = MessageSeverity.Error; } return(RedirectToAction(nameof(Index))); } else { return(View(viewModel)); } }
public int Post([FromBody] VillainEditViewModel viewModel) { CheckInput(); var newId = Repository.Instance.InsertVillain(viewModel); return(newId); }
public void UpdateVillain(int id, VillainEditViewModel viewModel) { if (viewModel == null) { throw new InvalidInputException("null input"); } if (viewModel.Id == 0 || id != viewModel.Id) { throw new InvalidInputException("invalid id"); } if (viewModel.NemesisId != 0 && !_superheroes.Any(x => x.Id == viewModel.NemesisId)) { throw new InvalidInputException("nemesisId not valid"); } var old = _villains.FirstOrDefault(x => x.Id == viewModel.Id); if (old == null) { throw new NotFoundException("villain not found"); } old.KidnappedPeople = viewModel.KidnappedPeople; old.KilledPeople = viewModel.KilledPeople; old.SecretName = viewModel.SecretName; old.Strength = viewModel.Strength; old.VillainName = viewModel.VillainName; if (viewModel.NemesisId != 0) { if (old.Nemesis == null) { var superhero = _superheroes.First(x => x.Id == viewModel.NemesisId); old.Nemesis = superhero; superhero.Enemies.Add(old); } else if (old.Nemesis.Id != viewModel.NemesisId) { var superhero = _superheroes.First(x => x.Id == viewModel.NemesisId); old.Nemesis = superhero; superhero.Enemies.Add(old); var oldSuperhero = _superheroes.First(x => x.Id == old.Nemesis.Id); oldSuperhero.Enemies.Remove(old); } } else { if (old.Nemesis != null) { var oldSuperhero = _superheroes.First(x => x.Id == old.Nemesis.Id); oldSuperhero.Enemies.Remove(old); old.Nemesis = null; } } }
public int InsertVillain(VillainEditViewModel viewModel) { if (viewModel == null) { throw new InvalidInputException("null input"); } if (viewModel.Id != 0) { throw new InvalidInputException("non-zero id"); } if (viewModel.NemesisId != 0 && !_superheroes.Any(x => x.Id == viewModel.NemesisId)) { throw new InvalidInputException("nemesisId not valid"); } var maxId = _villains.Count == 0 ? 0 : _villains.Max(x => x.Id); viewModel.Id = maxId + 1; var model = new Villain { Id = viewModel.Id, SecretName = viewModel.SecretName, VillainName = viewModel.VillainName, KilledPeople = viewModel.KilledPeople, KidnappedPeople = viewModel.KidnappedPeople, FirstDate = viewModel.FirstDate, Strength = viewModel.Strength, Characteristics = viewModel.Characteristics, }; if (viewModel.NemesisId != 0) { var superhero = _superheroes.First(x => x.Id == viewModel.NemesisId); model.Nemesis = superhero; superhero.Enemies.Add(model); } _villains.Add(model); return(viewModel.Id); }
public void UpdateVillain(VillainEditViewModel viewModel) { if (viewModel == null) { throw new InvalidInputException("null input"); } var old = _villains.FirstOrDefault(x => x.Id == viewModel.Id); if (old == null) { throw new NotFoundException("villain not found"); } old.KidnappedPeople = viewModel.KidnappedPeople; old.KilledPeople = viewModel.KilledPeople; old.SecretName = viewModel.SecretName; old.Strength = viewModel.Strength; old.VillainName = viewModel.VillainName; }
public async Task <IActionResult> Edit(VillainEditViewModel viewModel) { if (ModelState.IsValid) { try { var toUpdate = await _context.Villains .Include(x => x.Nemesis) .FirstOrDefaultAsync(x => x.Id == viewModel.Id); toUpdate.Characteristics = viewModel.Characteristics; toUpdate.FirstDate = viewModel.FirstDate; toUpdate.KidnappedPeople = viewModel.KidnappedPeople; toUpdate.KilledPeople = viewModel.KilledPeople; toUpdate.NemesisId = viewModel.NemesisId; toUpdate.SecretName = viewModel.SecretName; toUpdate.Strength = viewModel.Strength; toUpdate.VillainName = viewModel.VillainName; await _context.SaveChangesAsync(); TempData["MessageText"] = "Villain successfully updated!"; TempData["MessageSeverity"] = MessageSeverity.Ok; } catch (NotFoundException) { TempData["MessageText"] = "Villain not found!"; TempData["MessageSeverity"] = MessageSeverity.Error; } return(RedirectToAction(nameof(Index))); } else { ViewData["superheroes"] = await GetSuperheroNamesAsync(); return(View(viewModel)); } }
public VillainEditViewModel GetVillain(int id) { var model = _villains.FirstOrDefault(x => x.Id == id); if (model == null) { throw new NotFoundException("villain not found"); } var viewModel = new VillainEditViewModel { Id = model.Id, SecretName = model.SecretName, VillainName = model.VillainName, KilledPeople = model.KilledPeople, KidnappedPeople = model.KidnappedPeople, FirstDate = model.FirstDate, Strength = model.Strength, Characteristics = model.Characteristics, NemesisId = model.Nemesis == null ? 0 : model.Nemesis.Id, }; return(viewModel); }
public void Put(int id, [FromBody] VillainEditViewModel viewModel) { CheckInput(); Repository.Instance.UpdateVillain(id, viewModel); }