Esempio n. 1
0
        /// <summary>
        /// Retrieves from session.
        /// </summary>
        /// <returns></returns>
        public Lab5ViewModel RetrieveFromSession()
        {
            //Read the session cookie
            Lab5ViewModel item;

            if(!string.IsNullOrEmpty(UserID))
            {
                item = m_RavenSession.Load<Lab5ViewModel>(UserID);

                if(item == null || item.ID < 0)
                {
                    item = new Lab5ViewModel();

                    CreateSessions(item);
                }
            }
            else
            {
                //initialize a link list with max 20 operations
                item = new Lab5ViewModel();

                CreateSessions(item);
            }

            return item;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the sessions.
        /// </summary>
        /// <param name="item">The item.</param>
        private void CreateSessions(Lab5ViewModel item)
        {
            DateTime SessionExpiration = DateTime.UtcNow.AddMonths(SessionTimeOutValue);

            m_RavenSession.Store(item);
            m_RavenSession.SaveChanges();

            //Save user ID cookie
            HttpCookie authCookie = new HttpCookie(SessionKey, m_RavenSession.Advanced.GetDocumentId(item))
            {
                Expires = SessionExpiration
            };

            HttpContext.Current.Response.Cookies.Add(authCookie);
        }
Esempio n. 3
0
        public ActionResult Lab5(Lab5ViewModel model)
        {
            string chartSrc = string.Format("http://ichart.finance.yahoo.com/{0}={1}", "b?s", model.StockSymbol);
            string url = string.Format("http://finance.yahoo.com/q/cp?s={0}&ql=1", model.StockSymbol);
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "GET";
            httpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);

            //httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream);
            string response = streamReader.ReadToEnd();

            string start = @"<div class=""rtq_leaf"">";
            string end = @"<div id=""yfi_toolbox_mini_rtq"">";

            int startIndex = response.IndexOf(start);
            int endIndex = response.IndexOf(end);

            if(startIndex > 0 && endIndex > 0 && (endIndex > startIndex))
            {
                string stockSummaryHTML = response.Substring(startIndex, endIndex - startIndex);
                stockSummaryHTML = stockSummaryHTML + @"</div></div></div></div>";

                model.StockSummary = stockSummaryHTML;
                model.StockChart = chartSrc;
                model.LastUpdated = DateTime.Now;

                //Save the information
                Lab5DataAccess dataAccess = new Lab5DataAccess(RavenSession);
                Lab5ViewModel lab5Model = dataAccess.RetrieveFromSession();
                lab5Model.InjectFrom(model);
            }
            else
            {
                using(StringWriter sw = new StringWriter())
                {
                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "_StockSummaryPartial");
                    ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                    viewResult.View.Render(viewContext, sw);

                    model.StockSummary = sw.GetStringBuilder().ToString();
                }
            }

            return View("Lab5", model);
        }