Beispiel #1
0
        public int InsertSuperhero(SuperheroEditViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new InvalidInputException("null input");
            }

            if (viewModel.Id != 0)
            {
                throw new InvalidInputException("non-zero id");
            }

            var maxId = _superheroes.Count == 0
                ? 0
                : _superheroes.Max(x => x.Id);

            var model = new Superhero
            {
                AssetsValue = viewModel.AssetsValue,
                CanFly      = viewModel.CanFly,
                HeroName    = viewModel.HeroName,
                Powers      = viewModel.Powers,
                SecretName  = viewModel.SecretName,
                Strength    = viewModel.Strength,
                Enemies     = new List <Villain>(),
            };

            model.Id = maxId + 1;

            _superheroes.Add(model);

            return(model.Id);
        }
Beispiel #2
0
        public void UpdateSuperhero(int id, SuperheroEditViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new InvalidInputException("null input");
            }

            if (viewModel.Id == 0 || id != viewModel.Id)
            {
                throw new InvalidInputException("invalid id");
            }

            var old = _superheroes.FirstOrDefault(x => x.Id == viewModel.Id);

            if (old == null)
            {
                throw new NotFoundException("superhero not found");
            }

            old.AssetsValue = viewModel.AssetsValue;
            old.CanFly      = viewModel.CanFly;
            old.HeroName    = viewModel.HeroName;
            old.Powers      = viewModel.Powers;
            old.SecretName  = viewModel.SecretName;
            old.Strength    = viewModel.Strength;
        }
        public int Post([FromBody] SuperheroEditViewModel viewModel)
        {
            CheckInput();
            var newId = Repository.Instance.InsertSuperhero(viewModel);

            return(newId);
        }
        public async Task <IActionResult> Edit(SuperheroEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var toUpdate = await _context.Superheroes
                                   .Include(x => x.Enemies)
                                   .FirstOrDefaultAsync(x => x.Id == viewModel.Id);

                    toUpdate.AssetsValue = viewModel.AssetsValue;
                    toUpdate.CanFly      = viewModel.CanFly;
                    toUpdate.HeroName    = viewModel.HeroName;
                    toUpdate.Powers      = viewModel.Powers;
                    toUpdate.SecretName  = viewModel.SecretName;
                    toUpdate.Strength    = viewModel.Strength;

                    toUpdate.Enemies.Clear();

                    if (viewModel.EnemiesIds != null)
                    {
                        foreach (var enemyId in viewModel.EnemiesIds)
                        {
                            var villain = await _context.Villains
                                          .FirstOrDefaultAsync(x => x.Id == enemyId);

                            toUpdate.Enemies.Add(villain);
                        }
                    }

                    await _context.SaveChangesAsync();

                    TempData["MessageText"]     = "Superhero successfully updated!";
                    TempData["MessageSeverity"] = MessageSeverity.Ok;
                }
                catch (NotFoundException)
                {
                    TempData["MessageText"]     = "Superhero not found!";
                    TempData["MessageSeverity"] = MessageSeverity.Error;
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                ViewData["villains"] = await GetVillainNamesAsync();

                return(View(viewModel));
            }
        }
Beispiel #5
0
        public void UpdateSuperhero(SuperheroEditViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new InvalidInputException("null input");
            }

            var toUpdate = _superheroes.FirstOrDefault(x => x.Id == viewModel.Id);

            if (toUpdate == null)
            {
                throw new NotFoundException("superhero not found");
            }

            toUpdate.AssetsValue = viewModel.AssetsValue;
            toUpdate.CanFly      = viewModel.CanFly;
            toUpdate.HeroName    = viewModel.HeroName;
            toUpdate.Powers      = viewModel.Powers;
            toUpdate.SecretName  = viewModel.SecretName;
            toUpdate.Strength    = viewModel.Strength;

            var oldEnemies = toUpdate.Enemies;

            foreach (var oe in oldEnemies)
            {
                oe.Nemesis = null;
            }
            toUpdate.Enemies.Clear();

            foreach (var enemyId in viewModel.EnemiesIds)
            {
                var villain = _villains.FirstOrDefault(x => x.Id == enemyId);
                if (villain == null)
                {
                    throw new NotFoundException("selected enemy not found");
                }
                villain.Nemesis = toUpdate;
                toUpdate.Enemies.Add(villain);
            }
        }
 public IActionResult Edit(SuperheroEditViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         try
         {
             Repository.Instance.UpdateSuperhero(viewModel);
             TempData["MessageText"]     = "Superhero successfully updated!";
             TempData["MessageSeverity"] = MessageSeverity.Ok;
         }
         catch (NotFoundException)
         {
             TempData["MessageText"]     = "Superhero not found!";
             TempData["MessageSeverity"] = MessageSeverity.Error;
         }
         return(RedirectToAction(nameof(Index)));
     }
     else
     {
         return(View(viewModel));
     }
 }
Beispiel #7
0
        public SuperheroEditViewModel GetSuperhero(int id)
        {
            var model = _superheroes.FirstOrDefault(x => x.Id == id);

            if (model == null)
            {
                throw new NotFoundException("superhero not found");
            }

            var viewModel = new SuperheroEditViewModel
            {
                Id          = model.Id,
                SecretName  = model.SecretName,
                HeroName    = model.HeroName,
                AssetsValue = model.AssetsValue,
                CanFly      = model.CanFly,
                Powers      = model.Powers,
                Strength    = model.Strength,
            };

            return(viewModel);
        }
Beispiel #8
0
        public void UpdateSuperhero(SuperheroEditViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new InvalidInputException("null input");
            }

            var old = _superheroes.FirstOrDefault(x => x.Id == viewModel.Id);

            if (old == null)
            {
                throw new NotFoundException("superhero not found");
            }

            old.AssetsValue = viewModel.AssetsValue;
            old.CanFly      = viewModel.CanFly;
            old.HeroName    = viewModel.HeroName;
            old.Powers      = viewModel.Powers;
            old.SecretName  = viewModel.SecretName;
            old.Strength    = viewModel.Strength;
            old.Enemies     = _villains.Where(x => viewModel.Enemies.Contains(x.Id))
                              .ToList();
        }
 public void Put(int id, [FromBody] SuperheroEditViewModel viewModel)
 {
     CheckInput();
     Repository.Instance.UpdateSuperhero(id, viewModel);
 }