コード例 #1
0
 /// <summary>
 /// updating the users writting
 /// </summary>
 /// <param name="writtings"></param>
 public void UpdateWrittings(WrittingsDO writtings)
 {
     try
     {
         // connectiong to sql
         using (SqlConnection connectionStrongerTogether = new SqlConnection(ConnectionString))
             using (SqlCommand updateWrittings = new SqlCommand("Update_Writtings", connectionStrongerTogether))
             {
                 // calling on the stored procedures and setting the timeout
                 updateWrittings.CommandType    = CommandType.StoredProcedure;
                 updateWrittings.CommandTimeout = 60;
                 // adding value for the stored paramaters
                 updateWrittings.Parameters.AddWithValue("@WrittingId", writtings.WrittingId);
                 updateWrittings.Parameters.AddWithValue("@Title", writtings.Title);
                 updateWrittings.Parameters.AddWithValue("@Content", writtings.Content);
                 updateWrittings.Parameters.AddWithValue("@UserId", writtings.UserId);
                 updateWrittings.Parameters.AddWithValue("@WordCount", writtings.WordCount);
                 // open the connection and execute
                 connectionStrongerTogether.Open();
                 updateWrittings.ExecuteNonQuery();
             }
     }
     catch (SqlException Sqlex)
     {
         //Log exception for sql
         logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
     }
     catch (Exception ex)
     {
         // log other exceptions
         logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
     }
 }
コード例 #2
0
        // Make appropriate comment
        public WrittingsDO MapReaderToSingle(SqlDataReader reader)
        {
            WrittingsDO result = new WrittingsDO();

            // Make appropriate comment - Overview of this
            if (reader["WrittingId"] != DBNull.Value)
            {
                result.WrittingId = (long)reader["WrittingId"];
            }
            if (reader["Username"] != DBNull.Value)
            {
                result.Username = (string)reader["Username"];
            }
            if (reader["Title"] != DBNull.Value)
            {
                result.Title = (string)reader["Title"];
            }
            if (reader["DatePublished"] != DBNull.Value)
            {
                result.DatePublished = (DateTime)reader["DatePublished"];
            }
            if (reader["Content"] != DBNull.Value)
            {
                result.Content = (string)reader["Content"];
            }
            if (reader["UserId"] != DBNull.Value)
            {
                result.UserId = (long)reader["UserId"];
            }
            if (reader["WordCount"] != DBNull.Value)
            {
                result.WordCount = (int)reader["WordCount"];
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// PO to DO
        /// </summary>
        /// <param name="from"></param>
        /// <returns>information out</returns>
        public WrittingsDO MapPoToDo(Writtings from)
        {
            WrittingsDO to = new WrittingsDO();

            to.WrittingId    = from.WrittingId;
            to.Username      = from.Username;
            to.Title         = from.Title;
            to.DatePublished = from.DatePublished;
            to.Content       = from.Content;
            to.UserId        = from.UserId;
            to.WordCount     = from.WordCount;

            return(to);
        }
        public ActionResult DeleteWrittings(long writtingId)
        {
            // response variable
            ActionResult response;

            // checking to see if the writting ids match or admin
            if (writtingId != (long)Session["UserId"] && (long)Session["Role"] == 1)
            {
                response = RedirectToAction("AllUsers", "Account");
            }
            else
            {
                // creating variables for writtings and mapping them Do to PO
                WrittingsDO writtings        = WrittingsDataAccess.ViewWrittingsById(writtingId);
                Writtings   deletedWrittings = mapper.MapDoToPo(writtings);
                long        writtingID       = deletedWrittings.WrittingId;


                try
                {
                    //check to see if id is valid
                    if (writtingId > 0)
                    {
                        // id is valid
                        WrittingsDataAccess.DeleteWritting(writtingId);
                        response = RedirectToAction("AllWrittings", "Writtings");
                    }
                    else
                    {
                        // not valid
                        response = RedirectToAction("AllWrittings", "Writtings");
                    }
                }
                catch (SqlException Sqlex)
                {
                    //Log exception for sql
                    logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
                    response            = View(writtings);
                    TempData["Message"] = "Connection Error";
                }
                catch (Exception ex)
                {
                    // log exception for other exceptions
                    logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
                    response            = View(writtings);
                    TempData["Message"] = "Error Try Again";
                }
            }
            return(response);
        }
コード例 #5
0
        /// <summary>
        /// DO to BO
        /// </summary>
        /// <param name="from"></param>
        /// <returns> mapped info </returns>
        public WrittingsBO MapDoToBo(WrittingsDO from)
        {
            WrittingsBO to = new WrittingsBO();

            to.WrittingId    = from.WrittingId;
            to.Username      = from.Username;
            to.Title         = from.Title;
            to.DatePublished = from.DatePublished;
            to.Content       = from.Content;
            to.UserId        = from.UserId;
            // number of words
            to.WordCount = from.WordCount;

            return(to);
        }
        public ActionResult UpdateWrittings(long writtingId)
        {
            // response variable
            ActionResult response;

            // checking if id matches or admin
            if (writtingId != (long)Session["UserId"] && (long)Session["Role"] == 1)
            {
                response = RedirectToAction("AllUsers", "Account");
            }
            else
            {
                // mapping what needs to be updated DO to PO
                WrittingsDO writtings       = WrittingsDataAccess.ViewWrittingsById(writtingId);
                Writtings   updateWrittings = mapper.MapDoToPo(writtings);
                long        WrittingId      = updateWrittings.WrittingId;

                try
                {
                    //check if valid
                    if (writtingId > 0)
                    {
                        // view update writtings
                        response = View(updateWrittings);
                    }
                    else
                    {
                        // redirect if not valid
                        response = RedirectToAction("AllWrittings", "Writtings");
                    }
                }
                catch (SqlException Sqlex)
                {
                    //Log exception for sql
                    logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
                    response            = View(writtings);
                    TempData["Message"] = "Connection Error";
                }
                catch (Exception ex)
                {
                    // log exception for other exceptions
                    logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
                    response            = View(writtings);
                    TempData["Message"] = "Error Try Again";
                }
            }
            return(response);
        }
コード例 #7
0
        /// <summary>
        /// view by writting id
        /// </summary>
        /// <param name="writtingId"></param>
        /// <returns> the completed writtings </returns>
        public WrittingsDO ViewWrittingsById(long writtingId)
        {
            // instatiating writtingsDO
            WrittingsDO writtings = new WrittingsDO();

            try
            {   // instantiating sql connection and command
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                    using (SqlCommand command = new SqlCommand("View_Writtings_By_ID", connection))
                    {
                        // calling on the stored procedures and setting a timeout value
                        command.CommandType    = CommandType.StoredProcedure;
                        command.CommandTimeout = 60;
                        // adding value to the paramater WrittingId
                        command.Parameters.AddWithValue("@WrittingId", writtingId);
                        // opening sql connection
                        connection.Open();
                        // Execute the command and pull the data from sql
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            // reads entries from database
                            if (reader.Read())
                            {
                                // pulls the info from the database and maps it out
                                writtings.WrittingId    = reader["WrittingId"] != DBNull.Value ? (long)reader["WrittingId"] : 0;
                                writtings.Title         = reader["Title"] != DBNull.Value ? (string)reader["Title"] : null;
                                writtings.DatePublished = reader["DatePublished"] != DBNull.Value ? (DateTime)reader["DatePublished"] : DateTime.MinValue;
                                writtings.Content       = reader["Content"] != DBNull.Value ? (string)reader["Content"] : null;
                                writtings.UserId        = reader["UserId"] != DBNull.Value ? (long)reader["UserId"] : 0;
                                writtings.WordCount     = reader["WordCount"] != DBNull.Value ? (int)reader["WordCount"] : 0;
                            }
                        }
                    }
            }
            catch (SqlException Sqlex)
            {
                //Log exception for sql
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
            }
            catch (Exception ex)
            {
                // log other exceptions
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
            }
            return(writtings);
        }
        public ActionResult CreateWrittings(Writtings form)
        {
            // creating the date for the date published
            form.DatePublished = DateTime.Now.Date;

            // creating our response variable
            ActionResult response;

            try
            {
                // checking to see if valid
                if (ModelState.IsValid)
                {
                    //pulling the user id from session
                    form.UserId = (long)Session["UserId"];
                    // using the logic layer for wordcount
                    form.WordCount = logic.CountWords(form.Content);
                    //mapping the writtings
                    WrittingsDO writtings = mapper.MapPoToDo(form);
                    WrittingsDataAccess.CreateWrittings(writtings);

                    response = RedirectToAction("AllWrittings", "Writtings");
                }
                else
                {
                    // if not valid
                    response = View(form);
                }
            }
            catch (SqlException Sqlex)
            {
                //Log exception for sql
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
                response            = View(form);
                TempData["Message"] = "Connection Error";
            }
            catch (Exception ex)
            {
                // log exception for other exceptions
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
                response            = View(form);
                TempData["Message"] = "Error Try Again";
            }
            return(response);
        }
コード例 #9
0
        /// <summary>
        /// view all writtings
        /// </summary>
        /// <returns> writtings </returns>
        public List <WrittingsDO> ViewAllWrittings()
        {
            // instantiate writtings
            List <WrittingsDO> returnWrittings = new List <WrittingsDO>();

            try
            {
                // instantiate sql connection and command
                using (SqlConnection connectionStrongerTogether = new SqlConnection(ConnectionString))
                    using (SqlCommand viewWrittings = new SqlCommand("View_All_Writtings", connectionStrongerTogether))
                    {
                        // calling on stored procedures with default 30sec timeout
                        viewWrittings.CommandType = CommandType.StoredProcedure;

                        //opening the connection
                        connectionStrongerTogether.Open();

                        // Execute the command and pull the data from sql
                        using (SqlDataReader sqlDataReader = viewWrittings.ExecuteReader())
                        {
                            //read entries from the database
                            while (sqlDataReader.Read())
                            {
                                // map each entry and adding to the return list
                                WrittingsDO writting = Mapper.MapReaderToSingle(sqlDataReader);
                                returnWrittings.Add(writting);
                            }
                        }
                        // attempt to close the connection
                        connectionStrongerTogether.Close();
                    }
            }
            catch (SqlException Sqlex)
            {
                //Log exception for sql
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
            }
            catch (Exception ex)
            {
                // log other exceptions
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
            }
            return(returnWrittings);
        }
        public ActionResult WrittingDetails(long writtingId)
        {
            // creating response variable
            ActionResult response;

            // viewing by writting id and mapping the writtings
            WrittingsDO writtings       = WrittingsDataAccess.ViewWrittingsById(writtingId);
            Writtings   detailWrittings = mapper.MapDoToPo(writtings);

            // writtingid variable
            long WrittingID = detailWrittings.WrittingId;

            try
            {
                //check if valid
                if (writtingId > 0)
                {
                    // if valid view details
                    response = View(detailWrittings);
                }
                else
                {
                    // if not valid redirect
                    response = RedirectToAction("AllWrittings", "Writtings");
                }
            }
            catch (SqlException Sqlex)
            {
                //Log exception for sql
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
                response            = View(writtings);
                TempData["Message"] = "Connection Error";
            }
            catch (Exception ex)
            {
                // log exception for other exceptions
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
                response            = View(writtings);
                TempData["Message"] = "Error Try Again";
            }
            return(response);
        }
コード例 #11
0
 /// <summary>
 /// creating a writting
 /// </summary>
 /// <param name="createWritting"></param>
 public void CreateWrittings(WrittingsDO createWritting)
 {
     // instantiating the sql connection and command
     using (SqlConnection connection = new SqlConnection(ConnectionString))
         using (SqlCommand command = new SqlCommand("Create_Writtings", connection))
         {
             // calling on stored procedures and setting the timeout length
             command.CommandType    = CommandType.StoredProcedure;
             command.CommandTimeout = 60;
             // adding value for the stored paramaters
             command.Parameters.AddWithValue("@Title", createWritting.Title);
             command.Parameters.AddWithValue("@DatePublished", createWritting.DatePublished);
             command.Parameters.AddWithValue("@Content", createWritting.Content);
             command.Parameters.AddWithValue("@UserId", createWritting.UserId);
             command.Parameters.AddWithValue("@WordCount", createWritting.WordCount);
             // opening conection executing and closing the connection
             connection.Open();
             command.ExecuteNonQuery();
             connection.Close();
         }
 }
        public ActionResult UpdateWrittings(Writtings form)
        {
            // response variables
            ActionResult response;

            try
            {
                // checking to see if valid
                if (ModelState.IsValid)
                {
                    //updates wordcount
                    form.WordCount = logic.CountWords(form.Content);
                    // mapping the updated writting PO to DO
                    WrittingsDO writtingsDO = mapper.MapPoToDo(form);
                    WrittingsDataAccess.UpdateWrittings(writtingsDO);

                    // redirect
                    response = RedirectToAction("AllWrittings", "Writtings");
                }
                else
                {
                    response = View(form);
                }
            }
            catch (SqlException Sqlex)
            {
                //Log exception for sql
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, Sqlex);
                response            = View(form);
                TempData["Message"] = "Connection Error";
            }
            catch (Exception ex)
            {
                // log exception for other exceptions
                logger.ErrorLogger(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ex);
                response            = View(form);
                TempData["Message"] = "Error Try Again";
            }
            return(response);
        }
コード例 #13
0
        /// <summary>
        /// DO to PO
        /// </summary>
        /// <param name="from"></param>
        /// <returns> out information</returns>
        public Writtings MapDoToPo(WrittingsDO from)
        {
            Writtings to = new Writtings();

            to.WrittingId    = from.WrittingId;
            to.Username      = from.Username;
            to.Title         = from.Title;
            to.DatePublished = from.DatePublished;
            // creates a summory of the content
            to.Content = from.Content;
            if (from.Content.Length > 64)
            {
                to.Blurb = from.Content.Substring(0, 64) + "...";
            }
            else
            {
                to.Blurb = from.Content;
            }
            to.UserId    = from.UserId;
            to.WordCount = from.WordCount;

            return(to);
        }