示例#1
0
        public ActionResult UpdateEnemy(EnemyUpdateVM form)
        {
            ActionResult response;

            //checks to make sure that all required fields are filled out
            if (ModelState.IsValid)
            {
                //if all fields are filled out, try to connect to the server
                try
                {
                    //if a file was submited, update the image of the enemy to the given file, and rename the file to the
                    //enemies name
                    if (form.File != null && form.File.ContentLength > 0)
                    {
                        //if the enemy already has an image, delete that image
                        if (System.IO.File.Exists(Server.MapPath(form.Enemy.ImagePath)))
                        {
                            System.IO.File.Delete(Server.MapPath(form.Enemy.ImagePath));
                        }
                        //find the filepath for the enemies image
                        string path = Server.MapPath(form.Enemy.ImagePath);
                        //saves the new image to the enemies image filepath
                        form.File.SaveAs(path);
                    }

                    //map the new enemy information to an EnemyDO
                    EnemyDO enemy = Mapper.Mapper.EnemyPOtoDO(form.Enemy);

                    //sending enemydo object to enemy dao, passing in the EnemyDo object
                    eDAO.UpdateEnemy(enemy);

                    //instantiate enemy_itemDAO

                    //collecting old item drop information and deleting it
                    List <EnemyItemIDLink> dropList = Mapper.Mapper.DetailsDOtoPO(linkDAO.ViewByEnemyID(form.Enemy.EnemyID));

                    //for every item linked to the enemy, delete that link. Destroys old enemy drop information
                    foreach (EnemyItemIDLink item in dropList)
                    {
                        linkDAO.DeleteEnemyItems(item.LinkID);
                    }

                    //if item1 = item2, default item2 to 0. prevents enemy from having 2 links to 1 item.
                    if (form.Item1 == form.Item2)
                    {
                        form.Item2 = 0;
                    }

                    //instantiats a new EnemyItemIDLink (object holds the information about which item drops form the enemy)
                    EnemyItemIDLink newLink = new EnemyItemIDLink();
                    //sets the newLinks enemyid to the id of the id of the enemy being updated
                    newLink.EnemyID = form.Enemy.EnemyID;

                    //creating new links for item 1 if one was selected. if the item does not exist, no link is made
                    if (form.Item1 != 0)
                    {
                        //sets the newLinks item id to the new item id found in Item1
                        newLink.ItemID = form.Item1;
                        //creates a new link between the enemy and the item. Enemy will now display the item as a drop
                        linkDAO.CreateEnemyDetails(Mapper.Mapper.DetailsPOtoDO(newLink));
                    }

                    //creating new links for item 2 if one was selected. if the item does not exist, no link is made
                    if (form.Item2 != 0)
                    {
                        //sets the newLink item id to the id found in Item2
                        newLink.ItemID = form.Item2;
                        //creates a new link between the enemy and item. Enemy will now display the item as a drop
                        linkDAO.CreateEnemyDetails(Mapper.Mapper.DetailsPOtoDO(newLink));
                    }

                    //set response to redirect to enemies homepage
                    response = RedirectToAction("Index", "Enemy");
                }
                //log any sql exceptions encountered
                catch (SqlException sqlEx)
                {
                    //checks to see if the exception has been logged, and logs it if it hasn't been
                    if (!sqlEx.Data.Contains("Logged") || (bool)sqlEx.Data["Logged"] == false)
                    {
                        Logger.LogSqlException(sqlEx);
                    }
                    //redirects to the enemy list page
                    response = RedirectToAction("Index", "Enemy");
                }
                //catches any exceptions that occure due to mapping or otherwise
                catch (Exception ex)
                {
                    //checks to see if the exception has been logged, and logs it if it hasn't been
                    if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false)
                    {
                        Logger.LogException(ex);
                    }
                    //redirects to the enemy list page
                    response = RedirectToAction("Index", "Enemy");
                }
            }
            //if modelstate is false
            else
            {
                //refills the item list for the drop down menus
                form.itemList = Mapper.Mapper.ItemDOListToPO(iDAO.ViewAllItems());

                //adds teh default item of "None" to the list in case the enemy doesn't drop an itme
                ItemPO @default = new ItemPO
                {
                    Name        = "None",
                    ItemID      = 0,
                    Description = ""
                };
                form.itemList.Add(@default);

                //creates a modelstate error stating that the form is missing information
                ModelState.AddModelError("Validated", "Missing information. Please fill in all fields.");
                //sets response to the View() and passes in the old form information including the newly populated item list
                response = View(form);
            }

            return(response);
        }
示例#2
0
        public ActionResult UpdateEnemy(int id)
        {
            ActionResult  response;
            EnemyUpdateVM enemy = new EnemyUpdateVM();

            try
            {
                //mapping an enemypo to our VM
                enemy.Enemy = Mapper.Mapper.EnemyDOtoPO(eDAO.ViewSingleEnemy(id));

                //instantiating an enemy_itemDAO
                EnemyItemDAO linkDAO = new EnemyItemDAO(connectionString, logPath);

                //collecting item id's linked to the enemy id
                List <EnemyItemDO> itemDrops = linkDAO.ViewByEnemyID(id);

                //assigning items 1 and 2 based on what was returned from the enemy_itemDAO
                if (itemDrops.Count >= 1)
                {
                    //sets Item1 to the itemid from the link table in the database
                    enemy.Item1 = itemDrops[0].ItemID;
                    //checks to see if there was a second item linked to the enemy
                    if (itemDrops.Count == 2)
                    {
                        //sets Item2 to the second items id
                        enemy.Item2 = itemDrops[1].ItemID;
                    }
                }
                //setting default values to 0 if no items where linked to enemy
                else
                {
                    enemy.Item1 = 0;
                    enemy.Item2 = 0;
                }


                //creating and adding default item "none" to drop down list
                ItemPO @default = new ItemPO
                {
                    Name        = "None",
                    ItemID      = 0,
                    Description = ""
                };

                enemy.itemList.Add(@default);

                //adds list to existing list, does not delete old information.
                enemy.itemList.AddRange(Mapper.Mapper.ItemDOListToPO(iDAO.ViewAllItems()));

                response = View(enemy);
            }
            //catches any slqexceptions that occur during our DAO call
            catch (SqlException sqlEx)
            {
                //logs the exception IF it has not already been marked as logged
                if (!sqlEx.Data.Contains("Logged") || (bool)sqlEx.Data["Logged"] == false)
                {
                    Logger.LogSqlException(sqlEx);
                }
                //redirects to the index page of the enemy controller. The page containing the full list of enemies.
                response = RedirectToAction("Index", "Enemy");
            }
            //catches any non-sqlexceptions that may occure during mapping or otherwise
            catch (Exception ex)
            {
                //logs the exception if it has not already been marked as logged
                if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false)
                {
                    Logger.LogException(ex);
                }
                //redirects to the index page of the enemy controller. The page containing the full list of enemies.
                response = RedirectToAction("Index", "Enemy");
            }
            //passing enemy view model to view
            return(response);
        }