Ejemplo n.º 1
0
        private ActionResult Rate(int?id, RatingType type)
        {
            using (var cx = new ViralContext())
            {
                var existingRating = cx.Ratings.SingleOrDefault(r => r.User.Id == CurrentUserId && r.Comment.Id == id);
                if (existingRating != null)
                {
                    existingRating.RatedTime = DateTime.UtcNow;
                    existingRating.Type      = type;

                    cx.Entry(existingRating).State = EntityState.Modified;
                }
                else
                {
                    var comment = cx.Comments
                                  .Where(p => p.Id == id)
                                  .Include(p => p.Ratings)
                                  .SingleOrDefault();

                    if (comment == null)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    var user = cx.Users
                               .Where(u => u.Id == CurrentUserId)
                               .Include(u => u.Ratings)
                               .Single();

                    var rating = new Rating
                    {
                        RatedTime = DateTime.UtcNow,
                        Type      = type
                    };

                    if (user.Ratings == null)
                    {
                        user.Ratings = new List <Rating>();
                    }
                    user.Ratings.Add(rating);

                    if (comment.Ratings == null)
                    {
                        comment.Ratings = new List <Rating>();
                    }
                    comment.Ratings.Add(rating);

                    cx.Entry(user).State    = EntityState.Modified;
                    cx.Entry(comment).State = EntityState.Modified;
                }

                cx.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(Comment comment)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                comment.PostedTime = DateTime.UtcNow;

                if (currentUser.PostedComments == null)
                {
                    currentUser.PostedComments = new List <Comment>();
                }
                currentUser.PostedComments.Add(comment);

                cx.Entry(currentUser).State = EntityState.Modified;
                cx.Entry(comment).State     = EntityState.Added;

                cx.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 3
0
        public ActionResult Create(Post post)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                post.PostedTime   = DateTime.UtcNow;
                post.LastEditTime = post.PostedTime;

                if (currentUser.PostedPosts == null)
                {
                    currentUser.PostedPosts = new List <Post>();
                }
                currentUser.PostedPosts.Add(post);

                cx.Entry(currentUser).State = EntityState.Modified;
                cx.Entry(post).State        = EntityState.Added;

                cx.SaveChanges();
            }

            return(View("Create"));
        }
Ejemplo n.º 4
0
        public ActionResult Delete(int?id)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                var savedComment = cx.Comments.SingleOrDefault(c => c.Id == id && c.Author.Id == currentUser.Id);
                if (savedComment == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                savedComment.IsDeleted       = true;
                cx.Entry(savedComment).State = EntityState.Modified;
                cx.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }