public async Task <NewsPost> CreatePostAsync(NewsPost post, string postUrl,
                                                     bool publish = false)
        {
            VerifyManagementPermission();

            post.Title   = post.Title.Trim();
            post.Content = post.Content.Trim();
            if (publish)
            {
                post.PublishedAt = _dateTimeProvider.Now;
                await _newsCategoryRepository.SetLastPostDate(post.CategoryId, _dateTimeProvider.Now);
            }

            var addedPost = await _newsPostRepository
                            .AddSaveAsync(GetClaimId(ClaimType.UserId), post);

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

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

            return(addedPost);
        }
예제 #2
0
        public async Task <NewsPost> CreatePostAsync(NewsPost post, bool publish)
        {
            VerifyManagementPermission();

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

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

            if (publish)
            {
                post.PublishedAt = _dateTimeProvider.Now;
                await _newsCategoryRepository.SetLastPostDate(post.CategoryId, _dateTimeProvider.Now);
            }

            var addedPost = await _newsPostRepository
                            .AddSaveAsync(GetClaimId(ClaimType.UserId), post);

            if (publish)
            {
                var category = await _newsCategoryRepository.GetByIdAsync(addedPost.CategoryId);

                addedPost.CategoryName = category.Name;

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

            return(addedPost);
        }