public IActionResult BuyArmour([FromRoute] int id, int ninjaId) { if (id <= 0) { return(NotFound()); } var selectedNinja = _ninjaRepository.GetDetailed(ninjaId); var justBoughtArmour = _armourRepository.Get(id); var equippedArmour = selectedNinja.EquippedArmour.Select(na => na.Armour); // Dispose of detailed relations to prevent issues when deleting related data selectedNinja = _ninjaRepository.Get(ninjaId); foreach (var armour in equippedArmour) { // Check if ninja already has this armour type if (armour.ArmourType != justBoughtArmour.ArmourType) { continue; } // Sell old armour DeleteNinjaArmour(ninjaId, armour.Id); break; } _ninjaArmourRepository.Add(new NinjaArmour { ArmourId = justBoughtArmour.Id, NinjaId = selectedNinja.Id }); selectedNinja.Gold -= justBoughtArmour.Price; _ninjaRepository.Update(selectedNinja); _ninjaArmourRepository.Save(); _ninjaRepository.Save(); return(RedirectToAction(nameof(Index), new { ninjaId = ninjaId })); }
public override IActionResult DeleteConfirmed(int id) { var ninjaHasArmour = _ninjaArmourRepository.GetNinjaFromArmour(id); var armourValue = _armourRepository.Get(id).Price; foreach (var ninja in ninjaHasArmour) { ninja.Gold += armourValue; _ninjaRepository.Update(ninja); } _ninjaRepository.Save(); return(base.DeleteConfirmed(id)); }
public void UpdateNinjaAndClan() { var ninjaRepository = new NinjaRepository(); var ninjaClanRepository = new NinjaClanRepository(); var ninja = ninjaRepository.GetById(1); var ninjaClan = ninjaClanRepository.GetById(ninja.NinjaClanId); ninja.NinjaName = "Murilo Cesar"; ninjaRepository.Update(ninja); ninjaRepository.Save(); ninjaClan.ClanName = "Gafanhotos"; ninjaClanRepository.Update(ninjaClan); ninjaClanRepository.Save(); }