adding a model and a form and other stuff to make it right in a smallish way

This commit is contained in:
Dan Buch 2011-07-31 22:49:25 -04:00
parent 168b2d24eb
commit 29141a90fb
4 changed files with 49 additions and 20 deletions

View File

@ -1,8 +1,5 @@
using System; using System.Web.Mvc;
using System.Configuration; using TwitterPractice.Models;
using System.Net;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
namespace TwitterPractice.Controllers namespace TwitterPractice.Controllers
@ -10,12 +7,13 @@ namespace TwitterPractice.Controllers
public class HomeController : Controller public class HomeController : Controller
{ {
private static string title = "Twitter Practice!"; private static string title = "Twitter Practice!";
private static string requestTemplate; private string requestTemplate;
private TwitterModels _twitterModels;
public HomeController() public HomeController()
: base() : base()
{ {
requestTemplate = ConfigurationManager.AppSettings["TwitterRequestTemplate"]; _twitterModels = new TwitterModels();
} }
public ActionResult Index() public ActionResult Index()
@ -26,18 +24,17 @@ namespace TwitterPractice.Controllers
string username = Request.QueryString["username"]; string username = Request.QueryString["username"];
if (null == username) if (null == username)
{ {
username = "meatballhat"; ViewBag.UserTimeline = null;
ViewBag.Username = null;
} }
ViewBag.UserTimeline = GetTwitterStream(username); else
{
ViewBag.UserTimeline = _twitterModels.GetTwitterStream(username);
ViewBag.Username = username; ViewBag.Username = username;
}
return View(); return View();
} }
private JArray GetTwitterStream(string username="meatballhat")
{
string requestUrl = String.Format(requestTemplate, username);
WebClient req = new WebClient();
return JArray.Parse(req.DownloadString(requestUrl));
}
} }
} }

View File

@ -0,0 +1,23 @@
using System;
using System.Configuration;
using System.Net;
using Newtonsoft.Json.Linq;
namespace TwitterPractice.Models
{
public class TwitterModels
{
private string requestTemplate;
public TwitterModels()
{
requestTemplate = ConfigurationManager.AppSettings["TwitterRequestTemplate"];
}
public JArray GetTwitterStream(string username = "meatballhat")
{
string requestUrl = String.Format(requestTemplate, username);
WebClient req = new WebClient();
return JArray.Parse(req.DownloadString(requestUrl));
}
}
}

View File

@ -71,6 +71,7 @@
<DependentUpon>Global.asax</DependentUpon> <DependentUpon>Global.asax</DependentUpon>
</Compile> </Compile>
<Compile Include="Models\AccountModels.cs" /> <Compile Include="Models\AccountModels.cs" />
<Compile Include="Models\TwitterModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,6 +1,12 @@
@using Newtonsoft.Json.Linq; @using Newtonsoft.Json.Linq;
@{ViewBag.Title = "Home Page";} @{ViewBag.Title = "Home Page";}
<form action="" method="get">
<label for="username-input">username:</label>
<input type="text" id="username-input" name="username" />
<input type="submit" name="submit" value="Submit" />
</form>
<h2>@ViewBag.Message</h2> <h2>@ViewBag.Message</h2>
@if (ViewBag.HasQsVars) @if (ViewBag.HasQsVars)
{ {
@ -13,9 +19,12 @@
</ul> </ul>
} }
@if (null != ViewBag.Username)
{
<div id="user-timeline"> <div id="user-timeline">
<h4>user timeline for @ViewBag.Username</h4> <h4>user timeline for @ViewBag.Username</h4>
<ul id="timeline-entries"> <ul id="timeline-entries">
@foreach (JObject entry in ViewBag.UserTimeline) @foreach (JObject entry in ViewBag.UserTimeline)
{ {
@ -24,6 +33,5 @@
</li> </li>
} }
</ul> </ul>
<!-- @ViewBag.UserTimeline -->
</div> </div>
}