public RequestPO MapDOtoPO(RequestDO requestDO) { RequestPO requestPO = new RequestPO(); requestPO.RequestID = requestDO.RequestID; requestPO.RequestText = requestDO.RequestText; requestPO.Username = requestDO.Username; requestPO.Date = requestDO.Date; return(requestPO); }
public ActionResult CreateRequest(RequestPO requestPO) { ActionResult response; //check if logged in if (Session["RoleID"] != null) { //if logged in, check the model state try { //check model if (ModelState.IsValid) { //if model is valid, get user information for username UserDO user = _UserDataAccess.ViewUserByID((int)Session["UserID"]); //map from view model to a DO RequestDO request = new RequestDO() { RequestText = requestPO.RequestText, Username = user.Username, Date = DateTime.Now.ToString(), }; //access database _RequestDataAccess.CreateRequest(request); //show confirmation screen response = RedirectToAction("RequestSubmitted", "Request"); } else { //if model is not valid, return to form response = View(requestPO); } } catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); response = View(requestPO); } finally { } } else { //if not logged in, redirect to login page response = RedirectToAction("Login", "Account"); } return(response); }
/// <summary> /// Reads one record from the Requests table in the GAMEGROOVE database. Runs the VIEW_REQUEST_BY_ID stored procedure. /// </summary> /// <param name="requestID">ID of the request wanting to be viewed.</param> /// <returns>Returns a RequestDO filled with information retrieved from the database.</returns> public RequestDO ViewRequestByID(int requestID) { RequestDO request = new RequestDO(); //catch errors while accessing the database try { //connect to sql, run stored procedure using (SqlConnection connection = new SqlConnection(_ConnectionString)) using (SqlCommand command = new SqlCommand("VIEW_REQUEST_BY_ID", connection)) { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 60; //set parameter for the stored procedure command.Parameters.AddWithValue("@RequestID", requestID); connection.Open(); //read from the database, uses [if] statement to only read one record using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { request = _RequestMapper.MapReaderToSingle(reader); } } } } //catch sqlexceptions for accurate logging catch (SqlException ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } //catch further errors catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } finally { } return(request); }
/// <summary> /// Reads all records in the Requests table in the GAMEGROOVE database. Runs the VIEW_ALL_REQUESTS stored procedure. /// </summary> /// <returns>Returns a list of RequestDOs filled with information retrieved from the database.</returns> public List <RequestDO> ViewRequests() { List <RequestDO> requests = new List <RequestDO>(); //catch errors while accessing the database try { //connect to sql, run stored procedure using (SqlConnection connection = new SqlConnection(_ConnectionString)) using (SqlCommand command = new SqlCommand("VIEW_ALL_REQUESTS", connection)) { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 60; connection.Open(); //read from the database using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { RequestDO request = _RequestMapper.MapReaderToSingle(reader); requests.Add(request); } } } } //catch sqlexceptions for accurate logging catch (SqlException ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } // catch further exceptions catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } finally { } return(requests); }
/// <summary> /// Deletes a request from the database /// </summary> /// <param name="id">ID of the request needing to be deleted</param> /// <returns>Returns a result based on status</returns> public ActionResult DeleteRequest(int id) { ActionResult response; //check for admin if (Session["RoleID"] != null && (int)Session["RoleID"] == 6) { //if the user is an admin, check id if (id > 0) { //if id is valid, access the database try { //pull request data RequestDO requestDO = _RequestDataAccess.ViewRequestByID(id); //delete from database _RequestDataAccess.DeleteRequest(requestDO.RequestID); response = RedirectToAction("Index", "Request"); } catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); response = RedirectToAction("Error", "Home"); } finally { } } else { //if id is not valid, return to request details response = RedirectToAction("RequestDetails", "Request"); } } else { //if the user is not an admin, redirect to the login page response = RedirectToAction("Login", "Account"); } return(response); }
/// <summary> /// Displays the information for one specific request /// </summary> /// <param name="id">ID of the request needing to be displayed</param> /// <returns>Returns a result based on status</returns> public ActionResult RequestDetails(int id) { ActionResult response; //check for admin if (Session["RoleID"] != null && (int)Session["RoleID"] == 6) { //if user is an admin, check id if (id > 0) { //if id is valid, access database try { //access database, map to view model RequestDO requestDO = _RequestDataAccess.ViewRequestByID(id); RequestPO requestPO = _RequestMapper.MapDOtoPO(requestDO); response = View(requestPO); } catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); response = RedirectToAction("Error", "Home"); } finally { } } else { //if id is not valid, return to list of requests response = RedirectToAction("Index", "Request"); } } else { //if user is not an admin, redirect to login page response = RedirectToAction("Login", "Account"); } return(response); }
/// <summary> /// Filters data while reading from the database /// </summary> /// <param name="reader">SqlDataReader to read from the database</param> /// <returns>Returns a RequestDO with no null values</returns> public RequestDO MapReaderToSingle(SqlDataReader reader) { RequestDO requestDO = new RequestDO(); if (reader["RequestID"] != DBNull.Value) { requestDO.RequestID = (int)reader["RequestID"]; } if (reader["RequestText"] != DBNull.Value) { requestDO.RequestText = (string)reader["RequestText"]; } if (reader["Username"] != DBNull.Value) { requestDO.Username = (string)reader["Username"]; } if (reader["Date"] != DBNull.Value) { requestDO.Date = (string)reader["Date"]; } return(requestDO); }
/// <summary> /// Writes a record in the Requests table in the GAMEGROOVE database. Runs the CREATE_REQUEST stored procedure. /// </summary> /// <param name="request">RequestDO filled out with information supplied by the user</param> public void CreateRequest(RequestDO request) { //catch errors while accessing the database try { //connect to SQL, use stored procedure using (SqlConnection connection = new SqlConnection(_ConnectionString)) using (SqlCommand command = new SqlCommand("CREATE_REQUEST", connection)) { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 60; //set parameters for the stored procedure command.Parameters.AddWithValue("@RequestText", request.RequestText); command.Parameters.AddWithValue("@Username", request.Username); command.Parameters.AddWithValue("@Date", request.Date); connection.Open(); command.ExecuteNonQuery(); } } //catch sqlexceptions for accurate logging catch (SqlException ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } //catch further errors catch (Exception ex) { //log error _Logger.ErrorLog(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex); throw ex; } finally { } }