Beispiel #1
0
        public ActionResult ThumberUp(int articleId)
        {
            int     i           = 1;
            Person  currentUser = Session["CurrentUserData"] as Person;
            Article article     = db.ArticleSet.Where(a => a.Id == articleId).FirstOrDefault();

            if (!db.ArticleThumberUpSet.Where(tu => (tu.PersonId == currentUser.Id) && (tu.ArticleId == articleId)).Any())
            {
                db.ArticleThumberUpSet.Add(new ArticleThumberUp {
                    PersonId = currentUser.Id, ArticleId = articleId
                });
                article.ThumbUpNumber += 1;
                int tmpN = 0;
                switch (article.ThumbUpNumber)
                {
                case 10:
                    tmpN = 10;
                    break;

                case 5:
                    tmpN = 5;
                    break;

                case 1:
                    tmpN = 1;
                    break;
                }
                db.NotificationSet.Add(new Notification {
                    PersonId = article.PersonId, Time = DateTime.Now.ToLocalTime(), Content = $"You got {tmpN} ThumbUp(s) in {article.Title}"
                });
                db.People.Where(p => p.Id == currentUser.Id).FirstOrDefault().HasNewNotification = true;
                db.SaveChanges();
            }
            return(Json(new { n = article.ThumbUpNumber }));
        }
Beispiel #2
0
        public ActionResult Create([Bind(Include = "Id,SendPersonId,MessageContent,Time")] Message message)
        {
            if (ModelState.IsValid)
            {
                Person me = Session["CurrentUserData"] as Person;
                message.SendPersonId = me.Id;
                message.SendPerson   = db.People.Where(p => p.Id == me.Id).FirstOrDefault();
                //message.SendPerson = me;// cannot use this, EF would treat me instance as a new one and add it to db.
                Person friend = TempData["ToFriend"] as Person;
                message.ReceivePerson = db.People.Where(p => p.Id == friend.Id).FirstOrDefault();
                message.Time          = DateTime.Now.ToLocalTime();
                (db.Friends.Where(f => f.PersonA.Id == me.Id && f.PersonB.Id == friend.Id).FirstOrDefault()).LastMessage = message.MessageContent;
                (db.Friends.Where(f => f.PersonA.Id == friend.Id && f.PersonB.Id == me.Id).FirstOrDefault()).LastMessage = message.MessageContent;
                db.Messages.Add(message);
                db.NotificationSet.Add(new Notification {
                    PersonId = friend.Id, Time = DateTime.Now.ToLocalTime(), Content = $"You got a new message from {me.Name}"
                });
                db.People.Where(p => p.Id == friend.Id).FirstOrDefault().HasNewNotification = true;
                db.SaveChanges();
                return(RedirectToAction("Index", new { id = friend.Id }));
            }

            //ViewBag.SendPersonId = new SelectList(db.People, "Id", "Name", message.SendPersonId);
            return(View(message));
        }
Beispiel #3
0
        public ActionResult Create([Bind(Include = "Id,Name")] Board board)
        {
            if (ModelState.IsValid)
            {
                db.BoardSet.Add(board);
                db.SaveChanges();
                db.ArticleSet.Add(new Article()
                {
                    Title = "版規", Time = DateTime.Now.ToLocalTime(), BoardId = board.Id, PersonId = 1
                });
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(board));
        }
Beispiel #4
0
        public async Task <ActionResult> Register(RegisterViewModel model, Person newUser)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "UnConfirmedUser");
                    using (MyCardContainer db = new MyCardContainer())
                    {
                        newUser.authenticationId = user.Id;
                        db.People.Add(newUser);
                        db.SaveChanges();
                    }

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Boards"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #5
0
        public ActionResult ThumberUp(int commentId)
        {
            int     i           = 1;
            Person  currentUser = Session["CurrentUserData"] as Person;
            Comment comment     = db.CommentSet.Where(c => c.Id == commentId).Include(c => c.Article).FirstOrDefault();

            if (!db.CommentThumberUpSet.Where(tu => (tu.PersonId == currentUser.Id) && (tu.CommentId == commentId)).Any())
            {
                db.CommentThumberUpSet.Add(new CommentThumberUp {
                    PersonId = currentUser.Id, CommentId = commentId
                });
                comment.ThumberUpNumber += 1;
                int tmpN = 0;
                switch (comment.ThumberUpNumber)
                {
                case 100:
                    tmpN = 100;
                    break;

                case 60:
                    tmpN = 60;
                    break;

                case 30:
                    tmpN = 30;
                    break;

                case 15:
                    tmpN = 15;
                    break;

                case 5:
                    tmpN = 5;
                    break;

                case 1:
                    tmpN = 1;
                    break;
                }
                db.NotificationSet.Add(new Notification {
                    PersonId = comment.PersonId, Time = DateTime.Now.ToLocalTime(), Content = $"You got {tmpN} ThumbUp(s) in {comment.Article.Title}"
                });
                db.People.Where(p => p.Id == currentUser.Id).FirstOrDefault().HasNewNotification = true;
                db.SaveChanges();
            }
            return(Json(new { n = comment.ThumberUpNumber }));
        }
Beispiel #6
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl, Person newUser)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        /////////////
                        using (MyCardContainer db = new MyCardContainer())
                        {
                            newUser.authenticationId = user.Id;
                            db.People.Add(newUser);
                            db.SaveChanges();
                        }
                        /////////////
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Beispiel #7
0
        public ActionResult ToggleComfirmedUser(int id)
        {
            ApplicationUserManager userMgr = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
            Person targetUser = db.People.Find(id);
            var    target     = db.People.Find(id);

            target.ComfirmedUser = !target.ComfirmedUser;
            if (target.ComfirmedUser)
            {
                userMgr.AddToRole(targetUser.authenticationId, "ConfirmedUser");
            }
            else
            {
                userMgr.RemoveFromRole(targetUser.authenticationId, "ConfirmedUser");
            }
            db.SaveChanges();
            return(Json(target.ComfirmedUser, JsonRequestBehavior.AllowGet));
        }