예제 #1
0
        public async Task <NewsPost> EditPostAsync(NewsPost post, bool publish)
        {
            VerifyManagementPermission();

            if (post == null)
            {
                throw new GraException("Could not add post: post was empty.");
            }

            var currentPost = await _newsPostRepository.GetByIdAsync(post.Id);

            currentPost.Title        = post.Title.Trim();
            currentPost.Content      = post.Content.Trim();
            currentPost.EmailSummary = post.EmailSummary?.Trim();
            currentPost.CategoryId   = post.CategoryId;

            bool sendSubscriptionEmails = false;

            if (publish && !currentPost.PublishedAt.HasValue)
            {
                currentPost.PublishedAt = _dateTimeProvider.Now;
                sendSubscriptionEmails  = true;
            }

            currentPost = await _newsPostRepository.UpdateSaveAsync(GetClaimId(ClaimType.UserId),
                                                                    currentPost);

            if (sendSubscriptionEmails)
            {
                currentPost.CategoryName =
                    (await _newsCategoryRepository.GetByIdAsync(currentPost.CategoryId)).Name;
            }

            if (publish)
            {
                await _cache.RemoveAsync($"s{GetClaimId(ClaimType.SiteId)}.{CacheKey.LatestNewsPostId}");
            }

            return(currentPost);
        }
        public async Task <NewsPost> EditPostAsync(NewsPost post, string postUrl,
                                                   bool publish = false)
        {
            VerifyManagementPermission();

            var currentPost = await _newsPostRepository.GetByIdAsync(post.Id);

            currentPost.Title      = post.Title.Trim();
            currentPost.Content    = post.Content.Trim();
            currentPost.CategoryId = post.CategoryId;

            bool sendSubscriptionEmails = false;

            if (publish && !currentPost.PublishedAt.HasValue)
            {
                currentPost.PublishedAt = _dateTimeProvider.Now;
                sendSubscriptionEmails  = true;
            }

            currentPost = await _newsPostRepository.UpdateSaveAsync(GetClaimId(ClaimType.UserId),
                                                                    currentPost);

            if (sendSubscriptionEmails)
            {
                currentPost.CategoryName =
                    (await _newsCategoryRepository.GetByIdAsync(currentPost.CategoryId)).Name;
                await SendSubscriptionEmailsAsync(currentPost, postUrl);
            }

            if (publish)
            {
                _cache.Remove($"s{GetClaimId(ClaimType.SiteId)}.{CacheKey.LatestNewsPostId}");
            }

            return(currentPost);
        }