public IActionResult Login(User u) { using (var context = new TweetifyContext()) { User temp = context.Users.FirstOrDefault(x => x.Username == u.Username); if (temp != null) { if (temp.Password == u.Password) { HttpContext.Session.SetInt32("UserId", temp.Id); HttpContext.Session.SetString("UserName", temp.Username); HttpContext.Session.SetInt32("NbFollowers", temp.NbFollowers); return(RedirectToAction("Index", "Home")); } else { throw new Exception("Wrong password"); } } else { throw new Exception("Unknown user"); } } }
public ActionResult Like(int id) { if (!HttpContext.Session.GetInt32("UserId").HasValue) { return(RedirectToAction("Login", "Auth")); } else { using (var con = new TweetifyContext()) { Like query = con.Likes.FirstOrDefault(x => x.User.Id == HttpContext.Session.GetInt32("UserId") && x.Tweet.Id == id); if (query != null) { con.Likes.Remove(query); con.SaveChanges(); } else { Like like = new Like { Tweet = con.Tweets.FirstOrDefault(x => x.Id == id), User = con.Users.FirstOrDefault(x => x.Id == HttpContext.Session.GetInt32("UserId")) }; con.Likes.Add(like); con.SaveChanges(); return(RedirectToAction("Index", "Home")); } } return(RedirectToAction("Index", "Home")); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookiePolicy(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); using (var context = new TweetifyContext()) context.Database.EnsureCreated(); }
public IActionResult Login(User u) { try { using (var context = new TweetifyContext()) { var user = context.Users.FirstOrDefault(x => x.Username == u.Username); if (user.Password == (u.Password)) { HttpContext.Session.SetString("Logged", "true"); HttpContext.Session.SetString("UserName", user.Username); HttpContext.Session.SetInt32("UserID", user.Id); return(RedirectToAction("Index")); } else { return(View(u)); } } } catch (Exception e) { Console.WriteLine(e.Message); return(View()); } }
public async Task <IActionResult> Tweeter(Tweet t) { using (var context = new TweetifyContext()) { t.Content = t.Content.ToLower(); context.Tweets.Add(t); await context.SaveChangesAsync(); return(RedirectToAction("Login")); } }
public IActionResult Index() { if (!HttpContext.Session.GetInt32("UserId").HasValue) { return(RedirectToAction("Login", "Auth")); } else { using (var context = new TweetifyContext()) { var t = context.Tweets.Include("Author").ToList(); ViewData["tweetbox"] = t; } } return(View()); }
public async Task <ActionResult> Index(Tweet t) { try { using (var context = new TweetifyContext()) { t.Author = context.Users.FirstOrDefault(x => x.Id == HttpContext.Session.GetInt32("UserId")); await context.Tweets.AddAsync(t); await context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task <ActionResult> Tweeter(Tweet tweeter) { if (!HttpContext.Session.GetInt32("UserId").HasValue) { return(RedirectToAction("Login", "Auth")); } else { using (var con = new TweetifyContext()) { tweeter.Date = DateTime.Now; tweeter.Author = con.Users.FirstOrDefault(a => a.Id == HttpContext.Session.GetInt32("UserId")); await con.Tweets.AddAsync(tweeter); await con.SaveChangesAsync(); } return(RedirectToAction("Index", "Home")); } }
public IActionResult Index() { if (!HttpContext.Session.GetInt32("UserId").HasValue) { return(RedirectToAction("Login", "Auth")); } else { ViewData["UserName"] = HttpContext.Session.GetString("UserName"); ViewData["NbFollowers"] = HttpContext.Session.GetInt32("NbFollowers"); using (var con = new TweetifyContext()) { ViewData["Tweets"] = con.Tweets.Include("Author").Include("Likes").OrderByDescending(x => x.Date).Take(50).ToList(); } }; return(View()); }
public async Task<IActionResult> Register(User u) { using (var context = new TweetifyContext()) { User temp = context.Users.FirstOrDefault(x => x.Username == u.Username); if (temp != null) { throw new Exception("User all ready exists"); } else { u.Username = u.Username.ToLower(); context.Users.Add(u); await context.SaveChangesAsync(); return RedirectToAction("Login"); } } }