예제 #1
0
        /// <summary>
        /// ActionResult for the index view.
        /// </summary>
        /// <param name="sortOrder">send what you want to sort by, "SortMessage" or "SortDate".
        /// It will toggle between Descending and Ascending if you press link.</param>
        /// <param name="searchString">Send the Message that you want to search for.</param>
        /// <returns>Returns a view with the choosen data.</returns>
        public async Task <ActionResult> Index(string sortOrder, string searchString)
        {
            /*Load all the notes from database to an IQuerable collection
             * so we can do querys and return a temporary list to the view.*/
            var notes       = from n in db.Notes select n;
            var notesToSend = new List <Note>();

            #region GetMaxTemps
            foreach (var item in notes)//For every item in Notes we Gets and set the max temp for that day.
            {
                item.MaxTemp = await weather.GetMaxTemp(item.Date);
            }
            #endregion

            #region SearchNotes
            //If the searchString isn't null we search for message containing that text.
            if (!String.IsNullOrEmpty(searchString))
            {
                var arrayWithSearchWords = new string[Regex.Split(searchString, "[^a-zA-Z0-9]").Length];
                arrayWithSearchWords = Regex.Split(searchString.ToLower(), "[^a-zA-Z0-9]");

                foreach (var item in notes)
                {
                    var allWordsInMessage = new string[Regex.Split(item.Message, "[^a-zA-Z0-9]").Length];
                    allWordsInMessage = Regex.Split(item.Message.ToLower(), "[^a-zA-Z0-9]");

                    /*Using an Linq method "Intersect" with which you can compare 2 arrays.
                     * the new array will consist of the words that they have common. And
                     * then i check if the length is the same as the array with SearchWords.
                     * If it's true we have find a message with the same words as we have searched
                     * for in the view.*/
                    var both = allWordsInMessage.Intersect(arrayWithSearchWords);
                    if (both.Count() == arrayWithSearchWords.Length)
                    {
                        notesToSend.Add(item);
                    }
                }
                notes = notesToSend.AsQueryable();
            }
            #endregion

            #region SortNotes
            //Toggle message sort by descending and ascending.
            ViewBag.SortMessage = sortOrder == "Message_Descending" ? "Message_Ascending" : "Message_Descending";
            //Toggle date sort by descending and ascending.
            ViewBag.SortDate = sortOrder == "Date_Descending" ? "Date_Ascending" : "Date_Descending";

            /*Switching the choosen sortOrder and sort the temporary list by the case result
             * and then return it to the view.*/

            switch (sortOrder)
            {
            case "Message_Ascending":
            {
                notes = notes.OrderBy(n => n.Message);
                break;
            }

            case "Message_Descending":
            {
                notes = notes.OrderByDescending(n => n.Message);
                break;
            }

            case "Date_Descending":
            {
                notes = notes.OrderByDescending(n => n.Date);
                break;
            }

            default:
            {
                notes = notes.OrderBy(n => n.Date);         //order by date ascending default.
                break;
            }
            }
            #endregion

            return(View(notes.ToList()));
        }