Exemplo n.º 1
0
        public ActionResult Create(PostViewModel post)
        {
            var response = new JsonResponse<PostViewModel>();
            var newPost = new Post();

            try
            {
                newPost.Board = _repository.GetBoardById(post.BoardId);
                newPost.Owner = _repository.GetCompetitorByUserName(post.BoardId, User.Identity.Name);
                newPost.Body = post.Body;

                _repository.Add(newPost);
                _repository.CommitChanges();

                newPost.Owner.LastViewedPostId = newPost.PostId;
                _repository.CommitChanges();

                response.Result = new PostViewModel(newPost);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Error = true;
            }

            return (Json(response));
        }
Exemplo n.º 2
0
        public ActionResult Edit(PostViewModel post)
        {
            var response = new JsonResponse<string>();

            try
            {
                var editPost = _repository.GetPostById(post.PostId);

                if (!editPost.IsOwner(User.Identity.Name))
                    throw (new Exception("You are not the owner of this post"));

                editPost.Body = post.Body;
                editPost.Edited = DateTime.Now;

                _repository.CommitChanges();

                response.Result = String.Format("{0:g}", editPost.Edited);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Error = true;
            }

            return (Json(response));
        }