Permalink
Browse files

Add user registration and savor api

  • Loading branch information...
1 parent 4021cc2 commit 67840bfe2ec1ec4bbf238c89082620e1a2a5599a @nikhilk nikhilk committed Jan 8, 2013
Showing with 61 additions and 0 deletions.
  1. BIN App_Data/foodRecommender.sdf
  2. +24 −0 Default.cshtml
  3. +37 −0 Savor.cshtml
Binary file not shown.
View
@@ -33,6 +33,30 @@
var featured = recipes[new Random().Next(recipes.Count)];
}
+@{
+ var cookie = Request.Cookies["UserCookie"];
+ if (cookie == null) {
+ string userGuid = Guid.NewGuid().ToString("N");
+
+ cookie = new HttpCookie("UserCookie", userGuid) {
+ HttpOnly = true,
+ Expires = DateTime.UtcNow.AddYears(1),
+ Path = "/"
+ };
+
+ string referrer = null;
+ if (Request.UrlReferrer != null) {
+ referrer = Request.UrlReferrer.AbsoluteUri;
+ if (referrer.Length > 512) {
+ referrer = referrer.Substring(0, 512);
+ }
+ }
+
+ db.Execute(@"INSERT INTO users (Guid, CreatedOn, IPAddress, Referrer) VALUES (@0, @1, @2, @3)", userGuid, DateTime.UtcNow, Request.UserHostAddress, referrer);
+ Response.Cookies.Add(cookie);
+ }
+}
+
<h1>Welcome to Food Recommender!</h1>
<div id="featuredProduct">
View
@@ -0,0 +1,37 @@
+@{
+ var userCookie = Request.Cookies["UserCookie"];
+ if (userCookie == null) {
+ Response.StatusCode = 403;
+ Response.End();
+ }
+ var userGuid = userCookie.Value;
+
+ var recipeId = 0;
+ var recipe = Request.QueryString["recipe"];
+ if (String.IsNullOrEmpty(recipe) || (Int32.TryParse(recipe, out recipeId) == false)) {
+ Response.StatusCode = 403;
+ Response.End();
+ }
+
+ var db = Database.Open("foodRecommender");
+
+ var userRow = db.Query("SELECT Id FROM users WHERE Guid = @0", userGuid).FirstOrDefault();
+ if (userRow == null) {
+ Response.StatusCode = 403;
+ Response.End();
+ }
+ var userId = userRow.Id;
+
+ var recipeRow = db.Query("SELECT Id FROM recipes WHERE Id = @0", recipeId).FirstOrDefault();
+ if (recipeRow == null) {
+ Response.StatusCode = 403;
+ Response.End();
+ }
+
+ db.Execute(@"INSERT INTO votes (UserId, RecipeId, TimeStamp, Vote) VALUES (@0, @1, @2, @3)", userId, recipeId, DateTime.UtcNow, true);
+
+ Response.ContentType = "text/plain";
+ Response.StatusCode = 200;
+ Response.Write("Thanks!");
+ Response.End();
+}

0 comments on commit 67840bf

Please sign in to comment.