示例#1
0
        public PostDTO AddPost(Guid ownerId, PostPostDTO post)
        {
            Post p = new Post()
            {
                Id         = Guid.NewGuid(),
                OwnerId    = ownerId,
                Body       = post.Body,
                Likes      = 0,
                Comments   = 0,
                Retweets   = 0,
                IsDeleted  = false,
                ModifiedBy = "John Doe",
                ModifiedOn = DateTime.Now,
                CreatedBy  = "John Doe",
                CreatedOn  = DateTime.Now
            };

            _context.Post.Add(p);

            return(new PostDTO()
            {
                Id = p.Id,
                Body = p.Body,
                Likes = p.Likes,
                Comments = p.Comments,
                Retweets = p.Retweets,
                CreatedBy = p.CreatedBy,
                CreatedOn = p.CreatedOn
            });
        }
        public async Task <ApiResponse> Post([FromBody] PostPostDTO arg)
        {
            try
            {
                var data = await postService.CreateAsync(arg);

                return(new ApiResponse(InfoMessages.PostAdded, data, HttpStatusCode.OK.ToInt()));
            }
            catch (ValidationException ex)
            {
                throw new ApiException(ex.Errors, ex.StatusCode);
            }
            catch (CustomException ex)
            {
                throw new ApiException(ex, ex.StatusCode);
            }
            catch (Exception ex)
            {
                throw new ApiException(ex);
            }
        }
示例#3
0
        public IActionResult AddPost(Guid ownerId, [FromBody] PostPostDTO post)
        {
            if (post == null)
            {
                return(BadRequest());
            }

            // Model state is a Dictionary - the property IsValid will be false if the requirements on the PostPostDTO are not met
            if (!ModelState.IsValid)
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            PostDTO pdto = _postService.AddPost(ownerId, post);

            if (!_postService.Save())
            {
                return(StatusCode(500));
            }

            return(CreatedAtRoute("GetPost", new { id = pdto.Id }, pdto));
        }