public async Task <ActionResult <Publication> > GetByIdAsync([FromRoute] string id)
        {
            var publication = await publicationService.GetByIdAsync(id);

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

            return(Ok(publication));
        }
示例#2
0
        public async Task <IActionResult> GetAsync(int id)
        {
            var result = await _publicationService.GetByIdAsync(id);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }
            var publicationResource = _mapper.Map <Publication, PublicationResource>(result.Resource);

            return(Ok(publicationResource));
        }
        public async Task <IActionResult> PostAsync(int commenterId, int publicationId, [FromBody] SaveCommentResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var existingAccount = await _accountService.GetByIdAsync(commenterId);

            if (!existingAccount.Success)
            {
                return(BadRequest(existingAccount.Message));
            }

            var existingPublication = await _publicationService.GetByIdAsync(publicationId);

            if (!existingPublication.Success)
            {
                return(BadRequest(existingPublication.Message));
            }

            var comment = _mapper.Map <SaveCommentResource, Comment>(resource);

            comment.Commenter   = existingAccount.Resource;
            comment.Publication = existingPublication.Resource;

            var result = await _commentService.SaveAsync(comment);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var commentResource = _mapper.Map <Comment, CommentResource>(result.Resource);

            return(Ok(commentResource));
        }