示例#1
0
        public async Task SaveThumbnail(int postId, byte[] data, string contentType)
        {
            var post = await postRepo.Query()
                       .Include(i => i.ThumbnailImage)
                       .SingleOrDefaultAsync(p => p.Id == postId);

            PmAssert.AssertOrNotFound(post != null, "Post not found");

            var imageId = post.ThumbnailImageId;

            if (imageId.HasValue)
            {
                await imageRepo.Delete(post.ThumbnailImage);
            }

            if (data != null)
            {
                post.ThumbnailImage = new Image
                {
                    Data = data,
                    Mime = contentType
                };
            }
            else
            {
                post.ThumbnailImage = null;
            }

            await postRepo.Update(post);
        }
示例#2
0
        public async Task <PostModel> FetchOnePublicPost(int id, string uvi)
        {
            var post = await postRepo.Query()
                       .WithAuthor()
                       .WithTags()
                       .WithComments()
                       .SingleOrDefaultAsync(p => p.Id == id && p.Published);

            PmAssert.AssertOrNotFound(post != null, "Post not found");

            var model = PostModel.FromPost(post);

            if (uvi != null)
            {
                model.CanVote = true;

                var rating = await ratingRepo.Query()
                             .SingleOrDefaultAsync(r => r.PostId == id && r.Token == uvi);

                if (rating != null)
                {
                    model.Upvoted   = rating.Direction;
                    model.Downvoted = !rating.Direction;
                }
            }

            return(model);
        }
示例#3
0
        public async Task <IActionResult> Vote(int id, string dir)
        {
            PmAssert.AssertOrValidationError(dir == "up" || dir == "down", "Invalid vote direction");
            var uvi = HttpContext.Request.Cookies["uvi"];
            await postService.Vote(id, dir, uvi);

            return(RedirectBack());
        }
示例#4
0
        public async Task SetPublished(int id, bool published)
        {
            var post = await postRepo.FindOne(id);

            PmAssert.AssertOrNotFound(post != null, "Post not found");

            post.Published = published;

            await postRepo.Update(post);
        }
示例#5
0
        public async Task <PostModel> FetchEditablePost(int id)
        {
            var post = await postRepo.Query()
                       .Include(i => i.Tags)
                       .SingleOrDefaultAsync(p => p.Id == id);

            PmAssert.AssertOrNotFound(post != null, "Post not found");


            return(PostModel.FromPost(post));
        }
示例#6
0
        public async Task <int> SavePost(PostModel model, int userId)
        {
            Post post;
            var  now = DateTime.Now;

            if (model.Id.HasValue)
            {
                post = await postRepo.Query()
                       .Include(i => i.Tags)
                       .SingleOrDefaultAsync(p => p.Id == model.Id);

                PmAssert.AssertOrNotFound(post != null, "Post not found");
            }
            else
            {
                post = new Post
                {
                    CreatedAt = now
                };
            }

            post.Title     = model.Title;
            post.Subtitle  = model.Subtitle;
            post.Body      = model.Body;
            post.UpdatedAt = now;
            post.Author    = post.Author ?? await userRepo.FindOne(userId);

            var tags    = model.TagsAggr?.Split(",");
            var tagEnts = new List <PostTag>();

            if (tags != null)
            {
                tagEnts.AddRange(tags.Select(t => new PostTag
                {
                    Tag = t,
                }));
            }
            post.Tags = tagEnts;

            if (model.Id.HasValue)
            {
                await postRepo.Update(post);
            }
            else
            {
                await postRepo.Create(post);
            }

            return(post.Id);
        }
示例#7
0
        public async Task <ThumbnailModel> FetchThumbnail(int id)
        {
            var post = await postRepo.Query()
                       .Include(i => i.ThumbnailImage)
                       .SingleOrDefaultAsync(p => p.Id == id);

            PmAssert.AssertOrNotFound(post != null, "Post not found");
            PmAssert.AssertOrNotFound(post.ThumbnailImageId != null, "Post has no thumbnail");


            return(new ThumbnailModel
            {
                Id = post.ThumbnailImageId,
                Data = post.ThumbnailImage?.Data,
                Mime = post.ThumbnailImage?.Mime
            });
        }
示例#8
0
        public async Task AddComment(int postId, CommentModel model)
        {
            var comment = new Comment
            {
                Name      = model.Name,
                Text      = model.Text,
                CreatedAt = DateTime.Now
            };

            var post = await postRepo.FindOne(postId);

            PmAssert.AssertOrNotFound(post != null, "Post not found");

            post.Comments = post.Comments ?? new List <Comment>();

            post.Comments.Add(comment);

            await postRepo.Update(post);
        }