private Person GetUser() { var context = new TweetCloneDB(); String userId = Session["userid"] as String; return(context.People.FirstOrDefault(p => p.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase))); }
private Person GetUser(TweetCloneDB context) { if (context == null) { return(null); } String userId = Session["userid"] as String; return(context.People.FirstOrDefault(p => p.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase))); }
// GET: Tweet Stream home page public ActionResult Stream() { var context = new TweetCloneDB(); var user = GetUser(); var streamModel = new Stream { User = user, Tweets = GetTweets(user) }; return(View(streamModel)); }
public ActionResult Login(Person login) { var context = new TweetCloneDB(); var user = context.People.FirstOrDefault(p => p.UserId.Equals(login.UserId, StringComparison.OrdinalIgnoreCase) && p.Password.Equals(login.Password)); Session["userid"] = string.Empty; Session["fullname"] = "Not logged in"; if (user != null) { Session["userid"] = user.UserId; Session["fullname"] = user.FullName; } return(Json(user != null)); }
public ActionResult Update(string message) { if (!string.IsNullOrEmpty(message)) { var context = new TweetCloneDB(); var user = GetUser(context); user.Tweets.Add(new Tweet { Created = DateTime.Now, Message = message }); context.SaveChanges(); } return(RedirectToAction("RefreshBoard")); }
public ActionResult Save(Person modifiedProfile) { var context = new TweetCloneDB(); try { var existingProfile = context.People.FirstOrDefault(p => p.UserId.Equals(modifiedProfile.UserId, StringComparison.OrdinalIgnoreCase)); existingProfile.FullName = modifiedProfile.FullName; existingProfile.Email = modifiedProfile.Email; existingProfile.Password = modifiedProfile.Password; context.SaveChanges(); return(Json(string.Empty)); } catch (Exception ex) { return(Json(ex.Message)); } }
public ActionResult Delete() { try { var context = new TweetCloneDB(); String userId = Session["userid"] as String; var user = context.People.FirstOrDefault(p => p.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase)); context.Tweets.RemoveRange(user.Tweets); context.Followings.RemoveRange(user.Followers); context.Followings.RemoveRange(user.Followings); context.People.Remove(user); context.SaveChanges(); return(PartialView("RemoveProfileResult")); } catch (Exception ex) { return(Json(false)); } }
public ActionResult Follow(string followId) { if (!string.IsNullOrEmpty(followId)) { var context = new TweetCloneDB(); var user = GetUser(context); var followUser = context.People.FirstOrDefault(p => p.UserId.Equals(followId, StringComparison.OrdinalIgnoreCase)); if (followUser != null) { context.Followings.Add(new Following { UserId = followId, FollowingId = user.UserId }); } context.SaveChanges(); } return(RedirectToAction("RefreshInfo")); }
private List <Tweet> GetTweets(Person user) { var context = new TweetCloneDB(); List <Tweet> tweets = new List <Tweet>(); if (user == null) { return(tweets); } tweets.AddRange(user.Tweets); foreach (var follow in user.Followings) { tweets.AddRange(from tweet in context.Tweets where tweet.UserId.Equals(follow.UserId, StringComparison.InvariantCultureIgnoreCase) select tweet ); } return(tweets.OrderByDescending(t => t.Created).ToList()); }
public ActionResult Signup(Person signup) { var context = new TweetCloneDB(); try { var foundUser = context.People.FirstOrDefault(p => p.UserId.Equals(signup.UserId, StringComparison.OrdinalIgnoreCase)); if (foundUser != null) { return(Json("User already exist!")); } signup.Joined = DateTime.Now; signup.Active = true; context.People.Add(signup); context.SaveChanges(); Session["userid"] = signup.UserId; Session["fullname"] = signup.FullName; return(Json(string.Empty)); } catch (DbEntityValidationException ex) { var errors = new StringBuilder(); foreach (var e in ex.EntityValidationErrors) { foreach (var err in e.ValidationErrors) { errors.AppendFormat("{0}<br>", err.ErrorMessage); } } return(Json(errors.ToString())); } catch (Exception ex) { return(Json(ex.Message)); } }