Exemplo n.º 1
0
        public IHttpActionResult GetComments([FromUri] int id)
        {
            var post = this.Data.Posts.Find(id);

            if (post == null)
            {
                return(this.NotFound());
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            var commentsPreviews = post.Comments
                                   .OrderBy(c => c.PostedOn)
                                   .AsQueryable()
                                   .Select(p => CommentViewModel.CreatePreview(currentUser, p))
                                   .ToList();

            if (!commentsPreviews.Any())
            {
                return(this.StatusCode(HttpStatusCode.NoContent));
            }

            return(this.Ok(commentsPreviews));
        }
Exemplo n.º 2
0
        public async Task <HttpResponseMessage> GetPostComments([FromUri] int postId)
        {
            var post = this.Data.GroupPosts.Find(postId);

            if (post == null)
            {
                return(await this.NotFound().ExecuteAsync(new CancellationToken()));
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (currentUser == null)
            {
                return(await this.BadRequest("Invalid user token! Please login again!").ExecuteAsync(new CancellationToken()));
            }

            var commentsPreviews = post.Comments
                                   .OrderByDescending(c => c.PostedOn)
                                   .AsQueryable()
                                   .Select(p => CommentViewModel.CreatePreview(currentUser, p))
                                   .ToList();

            return(await this.Ok(commentsPreviews).ExecuteAsync(new CancellationToken()));
        }