// zobrazení stránky NewBeast
        public async Task <ActionResult> NewBeast()
        {
            List <BeastTypeDO> beastType =
                await BeastTypeDO.GetBeastTypeAsync();

            // hardcoded - allowing to add beasts only outside of towns
            List <LocationsDO> locations =
                await LocationsDO.GetLocationsAsync(1);

            ViewBag.beastType = beastType
                                .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.Idbeasttype.ToString()
            })
                                .ToList();

            ViewBag.Location = locations
                               .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.LocationID.ToString()
            })
                               .ToList();

            BeastModel model = new BeastModel();

            return(View(model));
        }
        // zobrazení stránky Bestiary
        public async Task <ActionResult> Bestiary()
        {
            ViewBag.Message = "Bestiary page.";

            List <BeastTypeDO> beastType =
                await BeastTypeDO.GetBeastTypeAsync();

            List <BeastsDO> bestiary =
                await BeastsDO.GetBeastsAsync(beastType[0].Idbeasttype);

            ViewBag.beastType = beastType
                                .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.Idbeasttype.ToString()
            })
                                .ToList();

            ViewBag.Bestiary = bestiary;

            BeastModel model = new BeastModel();

            model.BeastTypeID = beastType[0].Idbeasttype;

            return(View(model));
        }
        public async Task <ActionResult> NewBeast(BeastModel model)
        {
            List <BeastTypeDO> beastType =
                await BeastTypeDO.GetBeastTypeAsync();

            // hardcoded - povoluje přidat bestie pouze mimo města
            List <LocationsDO> locations =
                await LocationsDO.GetLocationsAsync(0);

            ViewBag.beastType = beastType
                                .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.Idbeasttype.ToString()
            })
                                .ToList();

            ViewBag.Location = locations
                               .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.LocationID.ToString()
            })
                               .ToList();

            if (ModelState.IsValid)
            {
                if (model.BeastName != null && model.BeastBio != null)
                {
                    using (fantasyDbEntities context = new fantasyDbEntities())
                    {
                        // Vytvoření nové bestie a načtení hodnot zvolených uživatelem
                        Bestiary beast = new Bestiary();
                        beast.name        = model.BeastName;
                        beast.bio         = model.BeastBio;
                        beast.hp          = model.BeastHp;
                        beast.attack      = model.BeastAttack;
                        beast.defense     = model.BeastDefense;
                        beast.locationid  = model.BeastLocation;
                        beast.beasttypeid = model.BeastTypeID;
                        // Přidání řádku tabulky a uložení změn
                        context.Bestiary.Add(beast);
                        context.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("Bestiary"));
        }
        // zobrazení stránky EditBeast
        public ActionResult EditBeast(int id)
        {
            using (fantasyDbEntities context = new fantasyDbEntities())
            {
                var beast = context.Bestiary.Where(x => x.idbeast == id).FirstOrDefault();

                // vytvoření instance BeastModelu a načtení aktuálních hodnot
                BeastModel model = new BeastModel();
                model.BeastID     = id;
                model.BeastName   = beast.name;
                model.BeastBio    = beast.bio;
                model.BeastTypeID = beast.beasttypeid;

                return(View(model));
            }
        }
        public ActionResult EditBeast(BeastModel model, int id)
        {
            ModelState.Clear();

            if (ModelState.IsValid)
            {
                try
                {
                    using (fantasyDbEntities context = new fantasyDbEntities())
                    {
                        Bestiary beast = new Bestiary();
                        beast             = context.Bestiary.Find(id);
                        beast.idbeast     = id;
                        beast.beasttypeid = model.BeastTypeID;

                        // kontrola stringových hodnot - nezměněné hodnoty jsou NULL
                        if (model.BeastName != null)
                        {
                            beast.name = model.BeastName;
                        }
                        if (model.BeastBio != null)
                        {
                            beast.bio = model.BeastBio;
                        }
                        try
                        {
                            context.Entry(beast).State = EntityState.Modified;
                            context.SaveChanges();
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            var error = ex.ToString();
                            return(View());
                        }
                    }

                    return(RedirectToAction("Bestiary"));
                }
                catch (Exception exception)
                {
                    var error = exception.ToString();
                    return(View());
                }
            }
            return(View());
        }
        public async Task <ActionResult> Bestiary(BeastModel model)
        {
            List <BeastTypeDO> beastType =
                await BeastTypeDO.GetBeastTypeAsync();

            List <BeastsDO> bestiary =
                await BeastsDO.GetBeastsAsync(model.BeastTypeID);

            ViewBag.beastType = beastType
                                .Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.Idbeasttype.ToString()
            })
                                .ToList();

            ViewBag.Bestiary = bestiary;

            return(View(model));
        }