Permalink
Browse files

Adding some API calls...

  • Loading branch information...
1 parent e40f27e commit d4ac1652cb74c9adcf0c2457ab0b2ead84938a77 @lgadi committed Feb 24, 2011
@@ -1,14 +1,45 @@
using System.Web.Mvc;
+using ZeeBi.UI.Services;
namespace ZeeBi.UI.Controllers
{
public class ApiController : Controller
{
+ private readonly UrlsRepository _urlsRespository;
+
+ public ApiController()
+ {
+ _urlsRespository = new UrlsRepository();
+ }
+
[HttpGet]
public ActionResult Index()
{
return View();
}
+ [HttpPost]
+ public JsonResult Create(CreateRequest createRequest)
+ {
+ var target = _urlsRespository.AddUrl(createRequest.LongUrl);
+
+ return Json(target);
+ }
+
+ [HttpGet]
+ public ActionResult Get(string id)
+ {
+ var url = _urlsRespository.FindOneById(id);
+ if (url == null)
+ {
+ return new HttpNotFoundResult("Could not find matching record for " + id);
+ }
+ return Json(url,JsonRequestBehavior.AllowGet);
+ }
+
+ public class CreateRequest
+ {
+ public string LongUrl { get; set; }
+ }
}
}
@@ -40,7 +40,7 @@ public ActionResult Add(Url url)
{
try
{
- _urlsRespository.AddUrl(url);
+ _urlsRespository.AddUrl(url.LongUrl, url.Id);
}
catch (IdAlreadyTakenException)
{
@@ -22,7 +22,8 @@ public static void RegisterRoutes(RouteCollection routes)
routes.MapRoute("Home", "", new { controller = Name<UrlsController>(), action = "Index" });
- routes.MapRoute("API", "api/v1/{method}", new { controller = Name<ApiController>(), action = "Index" , method = UrlParameter.Optional });
+ routes.MapRoute("APIHome", "api/v1", new { controller = Name<ApiController>(), action = "Index" });
+ routes.MapRoute("API", "api/v1/{action}", new { controller = Name<ApiController>() });
routes.MapRoute("Default", "-/{controller}/{action}/{id}", new { id = UrlParameter.Optional });
@@ -15,8 +15,14 @@ public UrlsRepository()
{
_idGenerator = new IdGenerator();
}
- public void AddUrl(Url url)
+ public Url AddUrl(string longUrl, string id = null)
{
+ var url = new Url
+ {
+ LongUrl = longUrl,
+ Id = id
+ };
+
if (url.Id == null)
{
url.Id = _idGenerator.Generate();
@@ -35,6 +41,7 @@ public void AddUrl(Url url)
url.ClickCount = 0;
DB.Urls.Insert(url, SafeMode.FSyncTrue);
+ return url;
}
public void RecordAnalytics(Url url, HttpRequestBase request)
@@ -84,6 +84,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\IdGenerator.cs" />
<Compile Include="Services\UrlNormalizer.cs" />
+ <Compile Include="Services\UrlsRepository.cs" />
<Compile Include="ViewModels\Stats\StatsViewModel.cs" />
<Compile Include="Views\WebViewPage.cs" />
</ItemGroup>

0 comments on commit d4ac165

Please sign in to comment.