Exemplo n.º 1
0
        public Result <Posts> UpdatePost(
            int postId,
            UpdatePostOptions options)
        {
            var result = new Result <Posts>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var post = SearchPost(new SearchPostOptions()
            {
                PostId = postId
            }).SingleOrDefault();

            if (post == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Post with id {postId} was not found";

                return(result);
            }

            if (!string.IsNullOrWhiteSpace(options.Post))
            {
                post.Post = options.Post;
            }

            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Posts> .ActionFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Posts> .ActionFailed(
                           StatusCode.InternalServerError,
                           "Post could not be updated"));
            }

            return(Result <Posts> .ActionSuccessful(post));
        }
Exemplo n.º 2
0
        public IActionResult Update(int id,
                                    [FromBody] UpdatePostOptions options)
        {
            var result = postService.UpdatePost(id,
                                                options);

            if (!result.Success)
            {
                return(StatusCode((int)result.ErrorCode,
                                  result.ErrorText));
            }

            return(Json(result.Data));;
        }
Exemplo n.º 3
0
        public Result <Post> UpdatePost(Post postToUpdate, UpdatePostOptions options)
        {
            options.Title = options.Title?.Trim();
            options.Text  = options.Text?.Trim();

            if (!string.IsNullOrWhiteSpace(options.Text))
            {
                postToUpdate.Text = options.Text;
            }

            if (!string.IsNullOrWhiteSpace(options.Title))
            {
                postToUpdate.Title = options.Title;
            }

            return(Result <Post> .Succeed(postToUpdate));
        }
        public Result <bool> UpdatePost(int?postId, int?userId, int?projectId, UpdatePostOptions updatePostOptions)
        {
            if (postId == null ||
                projectId == null ||
                userId == null)
            {
                return(Result <bool> .Failed(StatusCode.BadRequest, "Options Not Valid"));
            }

            if (Helpers.UserOwnsProject(_context, userId, projectId) == false)
            {
                return(Result <bool> .Failed(StatusCode.BadRequest, "Can Not Access A Project You Don't Own"));
            }

            var project = GetProjectById(projectId);

            var post = project.Data.Posts.SingleOrDefault(p => p.PostId == postId);

            if (post != null)
            {
                _postService.UpdatePost(post, updatePostOptions);
            }

            var rows = 0;

            try
            {
                rows = _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <bool> .Failed(StatusCode.InternalServerError, ex.Message));
            }

            return(rows <= 0
                ? Result <bool> .Failed(StatusCode.InternalServerError, "Post Could Not Be Updated")
                : Result <bool> .Succeed(true));
        }