Exemple #1
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index", "Posts"));
            }
            var postModel = _postsService.GetPost(id.Value);

            return(View(postModel.Post));
        }
Exemple #2
0
        public ActionResult PostView(int id)
        {
            var post = _postService.GetPost(id);

            //TODO: Add 404 check
            return(View(post));
        }
        public ActionResult Edit(int id)
        {
            var post = _postService.GetPost(id);

            var model = new EditPostViewModel();

            if (post != null)
            {
                model.Id    = post.Id;
                model.Text  = post.Text;
                model.Title = post.Title;
                model.Tags  = JsonConvert.SerializeObject(post.Tags.Select(t => t.Label));
            }

            return(View(model));
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var message     = "";
            var postService = new PostsService(_unitOfWork);
            var action      = filterContext.ActionDescriptor.ActionName.ToLower();
            var id          = -1;

            if (filterContext.ActionParameters.ContainsKey("id"))
            {
                id = (int)filterContext.ActionParameters["id"];
            }

            if (filterContext.ActionParameters.ContainsKey("model"))
            {
                id = ((EditPostViewModel)filterContext.ActionParameters["model"]).Id;
            }

            if (id == 0 && action == "edit") //New post
            {
                message = "";
            }
            else
            {
                var post = postService.GetPost(id);
                if (post == null) //check post exists
                {
                    message = "Unknown post";
                }
                else
                {
                    var isAdmin = HttpContext.Current.User.IsInRole("Admin");
                    if (!isAdmin)
                    {
                        var isAuthor = post.Author.Id == HttpContext.Current.User.Identity.GetUserId();
                        if (action == "delete" && !isAuthor)
                        {
                            message = "Only author or administrator can delete post";
                        }
                        if (action == "edit" && !isAuthor)
                        {
                            message = "Only author or administrator can edit post";
                        }
                    }
                }
            }

            if (message == "")
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                throw new PostEditException(message);
            }
        }
Exemple #5
0
        public CommentViewModel Post([FromBody] CommentViewModel model)
        {
            var user = _usersService.GetUser(User.Identity.GetUserId());
            var post = _postsService.GetPost(model.PostId);
            var c    = new Comment
            {
                Added           = DateTime.UtcNow,
                Author          = user,
                Post            = post,
                Text            = model.Text,
                ParentCommentId = model.ParentCommentId
            };

            _service.AddComment(c);
            _unitOfWork.Save();
            return(new CommentViewModel(c));
        }
Exemple #6
0
        public async Task GetPostShouldReturnTheCorrectPost()
        {
            var dbOptions = new DbContextOptionsBuilder <ApplicationDbContext>()
                            .UseInMemoryDatabase("test");
            var context = new ApplicationDbContext(dbOptions.Options);

            var postsService = new PostsService(context, null);

            var school = new School();
            var user   = new ApplicationUser();
            await context.Schools.AddAsync(school);

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            await postsService.CreatePost("Content", user, school.Id);

            var post  = context.Posts.FirstOrDefault();
            var posts = postsService.GetPost(post.Id);

            Assert.That(posts != null);
        }
 public void OnGet(int id)
 {
     Post = _postsService.GetPost(id);
 }
Exemple #8
0
        public override void OnGet(int id)
        {
            base.OnGet(id);

            Post = _postsService.GetPost(id);
        }