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 }));
        }
예제 #2
0
 public ViewNinjasViewModel(INavigation navigation)
 {
     _ninjaRepository = new NinjaRepository();
     _model           = new ObservableCollection <Ninja>(_ninjaRepository.Get());
     this._navigation = navigation;
 }