Пример #1
0
        // DELETE: api/Comments/5
        public async Task <string> Delete(int id)
        {
            var success = await CommentGenericFacade.DeleteAsync(id);

            if (!success)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return($"Deleted Comment with id: {id}");
        }
Пример #2
0
        // GET: api/Comments/2
        public async Task <CommentDto> Get(int id)
        {
            var comment = await CommentGenericFacade.GetAsync(id);

            if (comment == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return(comment);
        }
Пример #3
0
        // PUT: api/Comments/5
        public async Task <string> Put(int id, CommentDto entity)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            await CommentGenericFacade.UpdateAsync(entity);

            return($"Updated Comment with id: {id}");
        }
Пример #4
0
        public async Task <IEnumerable <CommentDto> > GetPostComments(int postId)
        {
            var post = await PostGenericFacade.GetAsync(postId);

            if (post == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var comments = await CommentGenericFacade.GetPostCommentsAsync(postId);

            return(comments);
        }
Пример #5
0
        // POST: api/Comments
        public async Task <string> Post(CommentDto entity)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            var commentId = await CommentGenericFacade.CreateAsync(entity);

            if (commentId.Equals(0))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            return($"Created Comment with id: {commentId}");
        }
Пример #6
0
        public async Task <ActionResult> AddComment(PostListModel model)
        {
            try
            {
                var newComment = new CommentDto()
                {
                    Text          = model.NewCommentText,
                    PostedAt      = DateTime.Now.ToUniversalTime(),
                    StayAnonymous = model.StayAnonymous,
                    UserId        = model.AuthenticatedUser.Id,
                    PostId        = model.PostId
                };

                await CommentGenericFacade.CreateAsync(newComment);

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }