Пример #1
0
        public IActionResult UpdatePost(int profileId, int postId, [FromBody] JsonPatchDocument <PostPatch> patchDocument)
        {
            Post initialPost = _profileRepository.GetPost(profileId, postId);

            if (initialPost == null)
            {
                return(NotFound());
            }

            PostPatch patchPost = new PostPatch
            {
                Content = initialPost.Content
            };

            patchDocument.ApplyTo(patchPost, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            initialPost.Content = patchPost.Content;
            _profileRepository.UpdatePost(profileId, postId, initialPost);

            return(NoContent());
        }
        public IActionResult PatchPostInProfileID(int profileID, int postID, [FromBody] JsonPatchDocument <PostPatch> postPatch)
        {
            Post post = _postsRepository.GetProfilePostByID(profileID, postID);

            if (!post)
            {
                return(NotFound());
            }

            // TODO: All of this seems like unnecessary clutter
            PostPatch patchPost = new PostPatch
            {
                Content = post.Content
            };

            postPatch.ApplyTo(patchPost, ModelState); // This is not working as expected, does not invalidate the model

            if (patchPost.Content.Length > PostPatch.MAX_LEN ||
                patchPost.Content.Length < PostPatch.MIN_LEN)
            {
                ModelState.AddModelError("Description", $"Content length must be between {PostPatch.MIN_LEN} and {PostPatch.MAX_LEN}");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            post.Content = patchPost.Content;

            return(NoContent());
        }
Пример #3
0
        public IActionResult EditPost(PostPatch parameters)
        {
            var userID = JWTUtility.GetUserID(HttpContext);
            var role   = JWTUtility.GetRole(HttpContext);

            var post = (from posts in _context.Posts
                        where posts.Id == parameters.ID
                        select posts).Single();

            if (role != RoleType.Admin && post.Userid != userID)
            {
                return(BadRequest(new { error = "You do not have permission to edit this post" }));
            }

            if (parameters.Post.Count() > _maxPostCharacterCount)
            {
                return(BadRequest(new { error = "Your post has too many characters" }));
            }

            post.Editdate = DateTime.Now;
            post.Post     = parameters.Post;

            _context.SaveChanges();

            return(Ok());
        }