Beispiel #1
0
 public PostModel(BlogPost post, string categoriesScheme)
 {
     Id = post.Id.ToIntId();
     Title = post.Title;
     Slug = post.DefaultSlug.Path;
     Summary = post.BriefInfo;
     ContentType = "text/html";
     Content = post.Content;
     Tags = post.Tags.Select(tag => tag.Name).ToArray();
     PublishDate = post.CreatedOn;
     LastUpdated = post.LastUpdatedOn;
     CategoriesScheme = categoriesScheme;
 }
        // POST api/posts
        public async Task<HttpResponseMessage> Post(AddPostCommand command)
        {
            HttpResponseMessage result;
            ClaimsPrincipal user = User as ClaimsPrincipal;
            Claim userIdClaim = user.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.NameIdentifier);

            if (userIdClaim == null || string.IsNullOrEmpty(userIdClaim.Value))
            {
                result = Request.CreateResponse(HttpStatusCode.InternalServerError);
            }
            else
            {
                BlogPost post = new BlogPost
                {
                    AuthorId = userIdClaim.Value,
                    Language = "en-US",
                    Title = command.Title,
                    BriefInfo = command.Summary,
                    Content = command.Content,
                    Tags = new Collection<Tag>(command.Tags.Select(tag => new Tag { Name = tag, Slug = tag.ToSlug() }).ToList()),
                    CreatedOn = command.PublishDate ?? DateTimeOffset.Now,
                    LastUpdatedOn = command.PublishDate ?? DateTimeOffset.Now,
                    AllowComments = true,
                    IsApproved = true
                };

                post.Slugs.Add(new Slug
                {
                    IsDefault = true,
                    Path = command.Slug ?? command.Title.ToSlug(),
                    CreatedOn = command.PublishDate ?? DateTimeOffset.Now
                });

                await RavenSession.StoreAsync(post);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, new PostModel(post, GetCategoryScheme()));
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "posts", id = post.Id.ToIntId() }));
                result = response;
            }

            return result;
        }
        private BlogPostComment ConstructBlogPostComment(BlogPost blogPost, CommentPostRequestModel requestModel, bool isSpam)
        {
            BlogPostComment blogPostComment = _mapper.Map<CommentPostRequestModel, BlogPostComment>(requestModel);
            blogPostComment.BlogPostId = blogPost.Id;
            blogPostComment.IsApproved = !isSpam;
            blogPostComment.IsSpam = isSpam;
            blogPostComment.IsByAuthor = false;
            blogPostComment.CreationIp = Request.UserHostAddress;
            blogPostComment.LastUpdateIp = Request.UserHostAddress;
            blogPostComment.CreatedOn = DateTimeOffset.Now;
            blogPostComment.LastUpdatedOn = DateTimeOffset.Now;

            if (Identity != null && Identity.IsAuthenticated)
            {
                string userId = RetrieveUserId(Identity);
                if (userId == blogPost.AuthorId)
                {
                    blogPostComment.IsByAuthor = true;
                }
            }

            return blogPostComment;
        }
        private async Task<BlogPostPageViewModel> ConstructBlogPostViewModelWithCommentsAsync(BlogPost blogPost, string defaultSlug)
        {
            IEnumerable<BlogPostComment> comments = await DocumentSession.Query<BlogPostComment>()
                .OrderBy(comment => comment.CreatedOn)
                .Where(comment => comment.BlogPostId == blogPost.Id && comment.IsSpam == false && comment.IsApproved == true)
                .ToListAsync();

            BlogPostPageViewModel viewModel = new BlogPostPageViewModel
            {
                BlogPost = _mapper.Map<BlogPost, BlogPostModelLight>(blogPost),
                Comments = _mapper.Map<IEnumerable<BlogPostComment>, IEnumerable<BlogPostCommentModel>>(comments)
            };

            viewModel.BlogPost.Slug = defaultSlug;

            return viewModel;
        }