コード例 #1
0
ファイル: AuthController.cs プロジェクト: enay92/Tweetify
        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");
                }
            }
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: enay92/Tweetify
        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"));
            }
        }
コード例 #3
0
        // 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();
        }
コード例 #4
0
        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());
            }
        }
コード例 #5
0
        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"));
            }
        }
コード例 #6
0
        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());
        }
コード例 #7
0
        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());
            }
        }
コード例 #8
0
ファイル: HomeController.cs プロジェクト: enay92/Tweetify
        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"));
            }
        }
コード例 #9
0
ファイル: HomeController.cs プロジェクト: enay92/Tweetify
        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());
        }
コード例 #10
0
        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");
                }
            }
        }