public ActionResult UpdateComment(ViewCommentPO view) { ActionResult response = null; //Only users can update their comments. if (ModelState.IsValid && Session["UserID"] != null && (int)Session["UserRole"] <= 3) { try { //Sets comment time to updated time. view.Comment.CommentTime = DateTime.Now; //Sets new comment information to the specific comment via SQL CommentDO commentObject = Mapper.CommentPOtoDO(view.Comment); _dataAccess.UpdateComment(commentObject); response = RedirectToAction("ViewGameComments", "Comments", new { specificGame = view.Comment.GameID }); } catch (Exception ex) { Logger.Log(ex); //Redirects to that game's page if a problem occurs response = RedirectToAction("GameDetails", "Game", new { specificGame = view.Comment.GameID }); } finally { } } else { response = RedirectToAction("GameDetails", "Game", new { specificGame = view.Comment.GameID }); } return(response); }
public List <CommentDO> ViewGameComments(int GameID) { //Setting the variables to be used for this method. SqlConnection connectionToSql = null; SqlCommand storedProcedure = null; SqlDataReader reader = null; List <CommentDO> commentList = new List <CommentDO>(); CommentDO commentObject = new CommentDO(); try { //Set up for connecting to SQL, using the stored procedure and how to access the information. connectionToSql = new SqlConnection(_connectionString); storedProcedure = new SqlCommand("OBTAIN_COMMENTS", connectionToSql); storedProcedure.CommandType = System.Data.CommandType.StoredProcedure; //Applies the game Id given to the method to the stored procedure. storedProcedure.Parameters.AddWithValue("@GameID", GameID); //Opens the connection to SQL. connectionToSql.Open(); //Tell SQLDataReader to gather the information from SQL as determined by the stored procedure. reader = storedProcedure.ExecuteReader(); //Cycle through the database and apply each property in SQL to an object //Then add that object to a list of objects. while (reader.Read()) { commentObject = new CommentDO(); commentObject.CommentID = int.Parse(reader["CommentID"].ToString()); commentObject.CommentTime = DateTime.Parse(reader["CommentTime"].ToString()); commentObject.CommentText = reader["CommentText"].ToString(); commentObject.GameID = int.Parse(reader["GameID"].ToString()); commentObject.UserID = int.Parse(reader["UserID"].ToString()); commentObject.Username = reader["Username"].ToString(); commentObject.GameID = int.Parse(reader["GameID"].ToString()); commentList.Add(commentObject); } } catch (Exception ex) { LoggerDAL.Log(ex, "Fatal"); //If an issue occurs, the result would likely be fatal, throw the exception. throw ex; } finally { //When the connection has been established, close and dispose the connection before finishing. if (connectionToSql != null) { connectionToSql.Close(); connectionToSql.Dispose(); } } return(commentList); }
//Map properties from the DataAccess to the Presentation. public static CommentPO CommentDOtoPO(CommentDO from) { CommentPO to = new CommentPO(); to.CommentID = from.CommentID; to.CommentTime = from.CommentTime; to.CommentText = from.CommentText; to.Rating = from.Rating; to.GameID = from.GameID; to.UserID = from.UserID; to.Username = from.Username; to.GameName = from.GameName; return(to); }
public void AddComment(CommentDO form) { //Setting the variables to be used for this method. SqlConnection connectionToSql = null; SqlCommand storedProcedure = null; try { //Setup for connecting to SQl and accessing the stored procedure. connectionToSql = new SqlConnection(_connectionString); storedProcedure = new SqlCommand("ADD_COMMENT", connectionToSql); storedProcedure.CommandType = System.Data.CommandType.StoredProcedure; //Add the value from the object to the parameters in the stored procedure to SqlCommand. storedProcedure.Parameters.AddWithValue("@CommentTime", form.CommentTime); storedProcedure.Parameters.AddWithValue("@CommentText", form.CommentText); //When the rating given is the default of byte, make it null in the database, otherwise keep it's value. storedProcedure.Parameters.AddWithValue("@Rating", form.Rating == default(byte) ? (object)DBNull.Value : form.Rating); storedProcedure.Parameters.AddWithValue("@GameID", form.GameID); storedProcedure.Parameters.AddWithValue("@UserID", form.UserID); //Open connection to Sql. connectionToSql.Open(); //Applies all the values stored in the SqlCommand to the database. storedProcedure.ExecuteNonQuery(); } catch (Exception ex) { LoggerDAL.Log(ex, "Fatal"); //If an issue occurs, the result would likely be fatal, throw the exception. throw ex; } finally { //When the connection has been established, close and dispose the connection before finishing. if (connectionToSql != null) { connectionToSql.Close(); connectionToSql.Dispose(); } } }
public ActionResult AddComment(ViewCommentPO view, string ratingDropdown) { ActionResult response = null; //Only registered users can submit comments. if (ModelState.IsValid && Session["UserID"] != null && (int)Session["UserRole"] <= 3) { try { //Parse the rating given to a byte, and set the date/time of submit. view.Comment.Rating = byte.Parse(ratingDropdown); view.Comment.CommentTime = DateTime.Now; //Add the comment to the database and refresh the page. CommentDO commentObject = Mapper.CommentPOtoDO(view.Comment); _dataAccess.AddComment(commentObject); response = RedirectToAction("ViewGameComments", "Comments", new { specificGame = view.Comment.GameID }); } catch (Exception ex) { Logger.Log(ex); response = RedirectToAction("Index", "Games"); } finally { } } //If adding the comment failed, give them back their comment at that game. else { view.CommentList = ObtainGameComments(view.Comment.GameID); FillTempData(view.Comment.GameID); if (Session["UserID"] != null) { view.Comment.UserID = (int)Session["UserID"]; } response = View("ViewGameComments", view); } return(response); }