/// <summary> /// New Enemy Method, takes in an enemydo object provides that information to the databases create new enemy procedure /// </summary> /// <param name="enemy">text</param> public void NewEnemy(EnemyDO enemy) { //try catch to capture sql errors try { //creating an sqlConnection and SqlCommand for our server connection using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (SqlCommand createEnemy = new SqlCommand("ENEMIES_CREATE", sqlConnection))//name change { //marking the SqlCommands command type to stored procedure so the database knows what to look for createEnemy.CommandType = System.Data.CommandType.StoredProcedure; //creating parameters to pass information back to the stored procedure createEnemy.Parameters.AddWithValue("Name", enemy.Name); createEnemy.Parameters.AddWithValue("Location", enemy.Location); createEnemy.Parameters.AddWithValue("Description", enemy.Description); createEnemy.Parameters.AddWithValue("ImagePath", enemy.ImagePath); createEnemy.Parameters.AddWithValue("Validated", enemy.Validated); //opening connecction to the server sqlConnection.Open(); //executing the command with the given paramters createEnemy.ExecuteNonQuery(); } } //catches any sqlexceptions that occure due to our connection or command catch (SqlException sqlEx) { //logging any sql errors that occure and creates a Logged key in the exceptions data dictionary, marking it as true Logger.LogSqlException(sqlEx); sqlEx.Data["Logged"] = true; //throws the exception throw sqlEx; } }
/// <summary> /// connect to sql server, provides the id to sort by, and pull back a single enemies information /// </summary> /// <param name="id"></param> /// <returns>EnemyDO</returns> public EnemyDO ViewSingleEnemy(int id) { EnemyDO enemy = new EnemyDO(); //uses try catch to capture sql errors try { //creating an sqlconnection and sqlcommand to access the database using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (SqlCommand pullSingleEnemy = new SqlCommand("ENEMIES_VIEW_SINGLE", sqlConnection)) { //setting the sqlcommands commandtype to storedprocedure to the server knows that its looking //for a stored procedure and not running the text as a command pullSingleEnemy.CommandType = System.Data.CommandType.StoredProcedure; //providing the method parameter id to the sqlcommand parameter for the stored procedure pullSingleEnemy.Parameters.AddWithValue("EnemyID", id); //opening slqconnection sqlConnection.Open(); //using sqldatareader to pull information from the server, row by row using (SqlDataReader reader = pullSingleEnemy.ExecuteReader()) { //check to see if the storedprocedure returned anything, and if so, passing that information to the mapper if (reader.Read()) { enemy = Mapper.MapSingleEnemy(reader); } } } } //any exceptions occured due to the server connection or command are caught here catch (SqlException sqlEx) { //logs any SqlExceptions and creates a Logged key in the exceptions data dictionary and sets it to true Logger.LogSqlException(sqlEx); sqlEx.Data["Logged"] = true; throw sqlEx; } //any exceptions occuring for any reason other then a sql exception are caught here, including mapping exceptions catch (Exception ex) { //logs any non-SqlExceptions, creates a Logged key in the exceptions data dictionary and sets it to true. Logger.LogException(ex); ex.Data["Logged"] = true; //then throws the exception throw ex; } //returns enemy information return(enemy); }
/// <summary> /// connect to sql server and pulls back a list of enemy information /// </summary> /// <returns>List<EnemyDO></returns> public List <EnemyDO> ViewAllEnemies() { //creating a list of Type EnemyDO to store all the enemies in the database. this will be returned later. List <EnemyDO> enemy = new List <EnemyDO>(); //uses a try catch to capture any sql errors try { //creates a sqlconnection and slqcommand to connect to the server using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (SqlCommand pullAllEnemies = new SqlCommand("ENEMIES_VIEW_ALL", sqlConnection)) { //sets SqlCommands CommandType to stored procedure so the database knows what its looking at pullAllEnemies.CommandType = System.Data.CommandType.StoredProcedure; //opens connection sqlConnection.Open(); //uses SqlDataReader to pull information form the server, row by row using (SqlDataReader reader = pullAllEnemies.ExecuteReader()) { //so long as the server is sending information, we'll pass that information to the mapper while (reader.Read()) { //creating a temporary EnemyDO variable to store the mapped information to it EnemyDO tempEnemy = Mapper.MapSingleEnemy(reader); //and adding that enemy data to the list created above enemy.Add(tempEnemy); } } } } //catching any exceptions thrown due to our server connection or command catch (SqlException sqlEx) { //logs the sql errors that occure and creates a key Logged in the exceptions data dictionary, setting it to true Logger.LogSqlException(sqlEx); sqlEx.Data["Logged"] = true; //throws the exception throw sqlEx; } //catching any exception that occure for reasons other than our server connection or command catch (Exception ex) { //logs the thrown exception, creates a key Logged in the exceptions data dictionary, and sets it to true Logger.LogException(ex); ex.Data["Logged"] = true; //throws the exception throw ex; } //returns our enemy information list return(enemy); }
public static EnemyPO EnemyDOtoPO(EnemyDO from) { EnemyPO to = new EnemyPO(); to.EnemyID = from.EnemyID; to.Name = from.Name; to.Location = from.Location; to.Description = from.Description; to.ImagePath = from.ImagePath; to.Validated = from.Validated; return(to); }
//mapper to map a single enemys data from the server to an EnemyDO object public static EnemyDO MapSingleEnemy(SqlDataReader from) { EnemyDO to = new EnemyDO(); to.EnemyID = (int)from["EnemyID"]; to.Name = from["Name"] as string; to.Location = from["Location"] as string; to.Description = from["Description"] as string; to.ImagePath = from["ImagePath"] as string; to.Validated = (bool)from["Validated"]; return(to); }
public EnemyDO ViewEnemyByName(string name) { EnemyDO enemyInfo = new EnemyDO(); try { using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (SqlCommand pullEnemyByName = new SqlCommand("ENEMIES_VIEW_BY_NAME", sqlConnection)) { pullEnemyByName.CommandType = System.Data.CommandType.StoredProcedure; pullEnemyByName.Parameters.AddWithValue("Name", name); sqlConnection.Open(); using (SqlDataReader reader = pullEnemyByName.ExecuteReader()) { if (reader.Read()) { enemyInfo = Mapper.MapSingleEnemy(reader); } } } } catch (SqlException sqlEx) { Logger.LogSqlException(sqlEx); sqlEx.Data["Logged"] = true; throw; } catch (Exception ex) { Logger.LogException(ex); ex.Data["Logged"] = true; throw ex; } return(enemyInfo); }
//Update Enemy Method takes in an enemyDO object and provides that information to the server //filters by the enemy objects id, to prevent all enemies being updated with the same information /// <summary> /// takes in an EnemyDO and updates the the enemy in the database based on the EnemyID inside the EnemyDO /// </summary> /// <param name="enemy"></param> public void UpdateEnemy(EnemyDO enemy) { //try catch to capture sql errors try { //creating a connection and command for our server connection using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (SqlCommand UpdateEnemy = new SqlCommand("ENEMIES_UPDATE", sqlConnection)) { //setting SqlCommands CommandType to stored procedure so the database knows what its looking for UpdateEnemy.CommandType = System.Data.CommandType.StoredProcedure; //creating parameters to pass information back to the stored procedure UpdateEnemy.Parameters.AddWithValue("EnemyID", enemy.EnemyID); UpdateEnemy.Parameters.AddWithValue("Name", enemy.Name); UpdateEnemy.Parameters.AddWithValue("Location", enemy.Location); UpdateEnemy.Parameters.AddWithValue("Description", enemy.Description); UpdateEnemy.Parameters.AddWithValue("ImagePath", enemy.ImagePath); UpdateEnemy.Parameters.AddWithValue("Validated", enemy.Validated); //opening the connection sqlConnection.Open(); //executes the stored procedure with the given paramters UpdateEnemy.ExecuteNonQuery(); } } //catchs any errors caused by the server connection or command catch (SqlException sqlEx) { //logging any sql errors that occur, and creates a key Logged in the exceptions data dictionary, marking it as true Logger.LogSqlException(sqlEx); sqlEx.Data["Logged"] = true; //throws the exception throw sqlEx; } }
public void init() { _enemyDO = new EnemyDO(); }
public ActionResult NewEnemy(CreateEnemyVM form) { ActionResult response; //checking for valid form entry if (ModelState.IsValid) { //pulling enemy info from server based on name entered in the view form EnemyDO enemyExists = eDAO.ViewEnemyByName(form.Enemy.Name); //if the enemy info pulled contains an id that is not 0(meaning it does not exist) then it jumps to the else statement if (enemyExists.EnemyID == 0) { //try catch to capture any sql errors try { //setting default enemy.validated value to false, prevents it showing up on the enemy list until validated form.Enemy.Validated = false; //checks to see if a image was uploaded during enemy creation if (form.File != null && form.File.ContentLength > 0) { //setts enemys image path for the database form.Enemy.ImagePath = "~/Images/Enemies/" + form.Enemy.Name + ".png"; //creates a filepath based on the enemies imagepath string path = Server.MapPath(form.Enemy.ImagePath); //saves the uploaded image to the filepath provided form.File.SaveAs(path); } //if no image was uploaded else { //finds the filepath for the default image string oldPath = System.Web.HttpContext.Current.Server.MapPath("~/Images/Enemies/heartless_emblem.png"); //sets the filepath for the new enemies image string newPath = System.Web.HttpContext.Current.Server.MapPath("~/Images/Enemies/" + form.Enemy.Name + ".png"); //if a filealready exists named the same as the created enemy, delete that image if (System.IO.File.Exists(newPath)) { System.IO.File.Delete(newPath); } //copy the default image and resave it under the enemies name System.IO.File.Copy(oldPath, newPath); //set the new enemies image path for the database form.Enemy.ImagePath = "~/Images/Enemies/" + form.Enemy.Name + ".png"; } //mapping po object to do and passing it to dao EnemyDO enemy = Mapper.Mapper.EnemyPOtoDO(form.Enemy); eDAO.NewEnemy(enemy); //redirecting to enemy page response = RedirectToAction("Index", "Enemy"); } catch (SqlException sqlEx) { //logging any sql exceptions caught if (!sqlEx.Data.Contains("Logged") || (bool)sqlEx.Data["Logged"] == false) { Logger.LogSqlException(sqlEx); } response = View(form); //update response } catch (Exception ex) { if (!ex.Data.Contains("Logged") || (bool)ex.Data["Logged"] == false) { Logger.LogException(ex); } response = View(form); } } //if the enemy name entered in the view form already exists else { //set a modelstate error telling the user the enemy already exists ModelState.AddModelError("Name", "Enemy already exists!"); //and return to the view with all the form information response = View(form); } } else { //if form was not valid, returns to form, supplying the information entered previously ModelState.AddModelError("Description", "Missing information, please fill in all fields."); response = View(form); } return(response); }
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); }