Ejemplo n.º 1
0
        public ActionResult AddComment(Comment newComment)
        {
            // Get the PostId that the comment will be attached to in order
            // to redirect back to the correct post.
            var postId = newComment.PostId;

            using (var redditCloneContext = new RedditCloneContext())
            {
                // Build a new comment to store in the database
                var comment = new Comment
                {
                    CommentBody   = newComment.CommentBody,
                    PostId        = newComment.PostId,
                    OwnerId       = User.Identity.GetUserId(),
                    OwnerUserName = User.Identity.GetUserName(),
                    Date          = DateTime.Now
                };

                // Add the comment to the database and save the changes.
                redditCloneContext.Comments.Add(comment);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", "Post", new { id = postId }));
            }
        }
Ejemplo n.º 2
0
        // GET: Post
        public ActionResult Detail(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Retrieve the correct Post from the database and build a post view model with the data
                var post = redditCloneContext.Posts.Select(p => new PostViewModel
                {
                    PostId        = p.PostId,
                    Title         = p.Title,
                    Body          = p.Body,
                    ImageLink     = p.ImageLink,
                    Comments      = p.Comments,
                    Date          = p.Date,
                    OwnerId       = p.OwnerId,
                    OwnerUserName = p.OwnerUserName,
                    SubredditId   = p.SubredditId
                }).SingleOrDefault(p => p.PostId == id);

                if (post == null)
                {
                    return(new HttpNotFoundResult());
                }

                return(View(post));
            }
        }
        public ActionResult AddSubreddit(SubredditViewModel subredditViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var subreddit = new Subreddit
                {
                    SubredditName = subredditViewModel.SubredditName
                };

                redditCloneContext.Subreddits.Add(subreddit);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", new { id = subreddit.SubredditId }));
            }
        }
 // GET: Subreddit
 public ActionResult Index()
 {
     using (var subredditContext = new RedditCloneContext())
     {
         var subredditList = new SubredditListViewModel
         {
             Subreddits = subredditContext.Subreddits.Select(s => new SubredditViewModel
             {
                 SubredditId   = s.SubredditId,
                 SubredditName = s.SubredditName,
                 Posts         = s.Posts
             }).ToList()
         };
         return(View(subredditList));
     }
 }
        public ActionResult DeleteSubreddit(SubredditViewModel subredditViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var subreddit = redditCloneContext.Subreddits.SingleOrDefault(s => s.SubredditId == subredditViewModel.SubredditId);

                if (subreddit != null)
                {
                    redditCloneContext.Subreddits.Remove(subreddit);
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 6
0
        public ActionResult AddSubreddit(SubredditViewModel subredditViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Build a new subreddit and save it to the DB.
                var newSubreddit = new Subreddit
                {
                    SubredditName = subredditViewModel.SubredditName,
                    OwnerId       = User.Identity.GetUserId()
                };

                redditCloneContext.Subreddits.Add(newSubreddit);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", new { id = newSubreddit.SubredditId }));
            }
        }
Ejemplo n.º 7
0
 // GET: Subreddit
 public ActionResult Index()
 {
     using (var subredditContext = new RedditCloneContext())
     {
         // Build a list of the subreddits currently in the database.
         var subredditList = new SubredditListViewModel
         {
             Subreddits = subredditContext.Subreddits.Select(s => new SubredditViewModel
             {
                 SubredditId   = s.SubredditId,
                 SubredditName = s.SubredditName,
                 Posts         = s.Posts
             }).ToList()
         };
         return(View(subredditList));
     }
 }
Ejemplo n.º 8
0
        public ActionResult DeleteSubreddit(SubredditViewModel subredditViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var subreddit = redditCloneContext.Subreddits.SingleOrDefault(s => s.SubredditId == subredditViewModel.SubredditId);

                // Ensure that the user owns the subreddit and delet it from the DB.
                if (subreddit != null && User.Identity.GetUserId() == subreddit.OwnerId)
                {
                    redditCloneContext.Subreddits.Remove(subreddit);
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Index"));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 9
0
        public ActionResult AddPost(PostViewModel postViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = new Post
                {
                    Title       = postViewModel.Title,
                    Body        = postViewModel.Body,
                    ImageLink   = postViewModel.ImageLink,
                    Date        = DateTime.Now,
                    SubredditId = postViewModel.SubredditId
                };

                redditCloneContext.Posts.Add(post);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", new { id = post.PostId }));
            }
        }
Ejemplo n.º 10
0
        public ActionResult EditPost(PostViewModel postViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == postViewModel.PostId);

                if (post != null)
                {
                    post.Title     = postViewModel.Title;
                    post.Body      = postViewModel.Body;
                    post.ImageLink = postViewModel.ImageLink;
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", new { id = postViewModel.PostId }));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult EditComment(Comment currentComment)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var postId = currentComment.PostId;

                var comment = redditCloneContext.Comments.SingleOrDefault(c => c.CommentId == currentComment.CommentId);

                if (comment != null)
                {
                    comment.CommentBody = currentComment.CommentBody;
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", "Post", new { id = postId }));
                }
            }

            return(new HttpNotFoundResult());
        }
        public ActionResult AddComment(Comment newComment)
        {
            var postId = newComment.PostId;

            using (var redditCloneContext = new RedditCloneContext())
            {
                var comment = new Comment
                {
                    CommentBody = newComment.CommentBody,
                    PostId      = newComment.PostId,
                    Date        = DateTime.Now
                };

                redditCloneContext.Comments.Add(comment);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", "Post", new { id = postId }));
            }
        }
        public ActionResult Detail(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var subreddit = redditCloneContext.Subreddits.Select(s => new SubredditViewModel
                {
                    SubredditId   = s.SubredditId,
                    SubredditName = s.SubredditName,
                    Posts         = s.Posts
                }).SingleOrDefault(s => s.SubredditId == id);

                if (subreddit == null)
                {
                    return(new HttpNotFoundResult());
                }

                return(View(subreddit));
            }
        }
Ejemplo n.º 14
0
        public ActionResult DeletePost(PostViewModel postViewModel)
        {
            int subredditId = postViewModel.SubredditId;

            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == postViewModel.PostId);

                if (post != null)
                {
                    redditCloneContext.Posts.Remove(post);
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", "Subreddit", new { id = subredditId }));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 15
0
        public ActionResult DeletePost(PostViewModel postViewModel)
        {
            int subredditId = postViewModel.SubredditId;

            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == postViewModel.PostId);

                //Ensure that the user owns the post, and delete post from the DB.
                if (post != null && User.Identity.GetUserId() == post.OwnerId)
                {
                    redditCloneContext.Posts.Remove(post);
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", "Subreddit", new { id = subredditId }));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 16
0
        public ActionResult PostEdit(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == id);
                if (post != null)
                {
                    var postViewModel = new PostViewModel
                    {
                        PostId = post.PostId,
                        Title  = post.Title,
                        Body   = post.Body
                    };

                    return(View("AddEditPost", postViewModel));
                }
            }

            return(new HttpNotFoundResult());
        }
Ejemplo n.º 17
0
        public ActionResult EditComment(Comment currentComment)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var postId = currentComment.PostId;

                // Retrieve the existing comment from the database.
                var comment = redditCloneContext.Comments.SingleOrDefault(c => c.CommentId == currentComment.CommentId);

                // Ensure that the user owns the comment, apply the changes and save the database
                if (comment != null && User.Identity.GetUserId() == comment.OwnerId)
                {
                    comment.CommentBody = currentComment.CommentBody;
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", "Post", new { id = postId }));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 18
0
        public ActionResult EditPost(PostViewModel postViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Retrieve the correct post from the database.
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == postViewModel.PostId);

                // Ensure that the user owns the post, make the appropriate changes and save the DB.
                if (post != null && User.Identity.GetUserId() == post.OwnerId)
                {
                    post.Title     = postViewModel.Title;
                    post.Body      = postViewModel.Body;
                    post.ImageLink = postViewModel.ImageLink;
                    redditCloneContext.SaveChanges();

                    return(RedirectToAction("Detail", new { id = postViewModel.PostId }));
                }

                return(new HttpNotFoundResult());
            }
        }
Ejemplo n.º 19
0
        public ActionResult Detail(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Query the DB for the subreddit with the given Id and build a view model.
                var subreddit = redditCloneContext.Subreddits.Select(s => new SubredditViewModel
                {
                    SubredditId   = s.SubredditId,
                    SubredditName = s.SubredditName,
                    Posts         = s.Posts,
                    OwnerId       = s.OwnerId
                }).SingleOrDefault(s => s.SubredditId == id);

                if (subreddit == null)
                {
                    return(new HttpNotFoundResult());
                }

                return(View(subreddit));
            }
        }
Ejemplo n.º 20
0
        public ActionResult AddPost(PostViewModel postViewModel)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Build a new post
                var post = new Post
                {
                    Title         = postViewModel.Title,
                    Body          = postViewModel.Body,
                    ImageLink     = postViewModel.ImageLink,
                    Date          = DateTime.Now,
                    OwnerId       = User.Identity.GetUserId(),
                    OwnerUserName = User.Identity.GetUserName(),
                    SubredditId   = postViewModel.SubredditId
                };

                // Add the post to the database and save the changes.
                redditCloneContext.Posts.Add(post);
                redditCloneContext.SaveChanges();

                return(RedirectToAction("Detail", new { id = post.PostId }));
            }
        }
Ejemplo n.º 21
0
        // GET: Post
        public ActionResult Detail(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                var post = redditCloneContext.Posts.Select(p => new PostViewModel
                {
                    PostId      = p.PostId,
                    Title       = p.Title,
                    Body        = p.Body,
                    ImageLink   = p.ImageLink,
                    Comments    = p.Comments,
                    Date        = p.Date,
                    SubredditId = p.SubredditId
                }).SingleOrDefault(p => p.PostId == id);

                if (post == null)
                {
                    return(new HttpNotFoundResult());
                }

                return(View(post));
            }
        }
Ejemplo n.º 22
0
        public ActionResult PostEdit(int id)
        {
            using (var redditCloneContext = new RedditCloneContext())
            {
                // Retrieve existing post from the database
                var post = redditCloneContext.Posts.SingleOrDefault(p => p.PostId == id);
                if (post != null)
                {
                    // Build a PostViewModel to populate the Edit form.
                    var postViewModel = new PostViewModel
                    {
                        PostId      = post.PostId,
                        Title       = post.Title,
                        ImageLink   = post.ImageLink,
                        Body        = post.Body,
                        SubredditId = post.SubredditId
                    };

                    return(View("AddEditPost", postViewModel));
                }

                return(new HttpNotFoundResult());
            }
        }