Beispiel #1
0
        public bool PublishArticleRevision(ArticleId articleId, ArticleRevisionDate revisionDate)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }
            if (revisionDate == null)
            {
                throw new ArgumentNullException("revisionDate");
            }

            var article = this.GetArticle(articleId);

            if (article == null)
            {
                return(false);
            }

            var articleRevision = this.GetArticleRevision(articleId, revisionDate);

            if (articleRevision == null)
            {
                return(false);
            }

            articleRevision.LastPublishedAt = DateTime.UtcNow;
            article.ActiveRevisionId        = articleRevision.Id;

            this.CurrentSession.SaveChanges();

            return(true);
        }
Beispiel #2
0
        public bool DeleteArticleRevision(ArticleId articleId, ArticleRevisionDate revisionDate)
        {
            var articleRevision = this.GetArticleRevision(articleId, revisionDate);

            if (articleRevision == null)
            {
                return(false);
            }

            this.CurrentSession.Delete(articleRevision);

            var article = this.GetArticle(articleId);

            if (article.ActiveRevisionId == articleRevision.Id)
            {
                var latestRevision =
                    this.CurrentSession.Query <ArticleRevision, ArticleRevisionsIndex>()
                    .OrderByDescending(x => x.CreatedAt)
                    .FirstOrDefault();

                article.ActiveRevisionId = (latestRevision != null) ? latestRevision.Id : null;
            }

            this.CurrentSession.SaveChanges();
            return(true);
        }
Beispiel #3
0
        public bool DeleteArticle(ArticleId articleId)
        {
            var article = this.GetArticle(articleId);

            if (article == null)
            {
                return(false);
            }

            this.CurrentSession.Delete(article);

            var articleRevisions =
                this.CurrentSession.Query <ArticleRevision, ArticleRevisionsIndex>()
                .Where(x => x.ArticleId == article.Id);

            var articleRedirects =
                this.CurrentSession.Query <ArticleRedirect, ArticleRedirectsIndex>()
                .Where(x => x.RedirectToArticleSlug == article.Id);

            articleRevisions.ToList().ForEach(x => this.CurrentSession.Delete(x));
            articleRedirects.ToList().ForEach(x => this.CurrentSession.Delete(x));

            this.CurrentSession.SaveChanges();

            return(true);
        }
Beispiel #4
0
        public ArticleResult GetArticleResult(ArticleId articleId, DateTime?revisionDate = null)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }

            ArticleRedirect articleRedirect = null;

            var article = this.CurrentSession.Include <Article>(x => x.ActiveRevisionId).Load(articleId.Id);

            if (article == null)
            {
                articleRedirect = this.GetArticleRedirect(articleId);
                if (articleRedirect != null)
                {
                    article = this.GetArticle(articleRedirect.RedirectToArticleSlug);
                }
            }

            string articleRevisionId = revisionDate.HasValue
                                           ? IdUtility.CreateArticleRevisionId(articleId, revisionDate.Value)
                                           : ((article != null) ? article.ActiveRevisionId : null);

            var articleRevision = this.GetArticleRevision(articleRevisionId);

            return(new ArticleResult(article, articleRevision, articleRedirect));
        }
Beispiel #5
0
        public ArticleRevision GetArticleRevision(ArticleId articleId, ArticleRevisionDate revisionDate)
        {
            string id = (articleId != null) ? articleId.Id : null;

            string articleRevisionId = IdUtility.CreateArticleRevisionId(id, revisionDate);

            return(this.GetArticleRevision(articleRevisionId));
        }
Beispiel #6
0
        public static string CreateArticleId(ArticleId articleId)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }

            return(String.Format("{0}{1}", ArticleIdPrefix, articleId.Slug));
        }
Beispiel #7
0
        public static string CreateArticleRedirectId(ArticleId originalArticleId)
        {
            if (originalArticleId == null)
            {
                throw new ArgumentNullException("originalArticleId");
            }

            return(String.Format("{0}{1}", ArticleRedirectIdPrefix, originalArticleId.Slug));
        }
Beispiel #8
0
        public static string CreateArticleId(Article article)
        {
            if (article == null)
            {
                throw new ArgumentNullException("article");
            }

            var articleId = new ArticleId(article.Slug);

            return(CreateArticleId(articleId));
        }
Beispiel #9
0
        public Article(ArticleId articleId, IEnumerable <string> tags = null, ArticleAccess articleAccess = null)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }

            this.Slug = articleId.Slug;

            this.Tags          = tags ?? Enumerable.Empty <string>();
            this.ArticleAccess = articleAccess ?? ArticleAccess.Default();
        }
Beispiel #10
0
        public IReadOnlyCollection <string> SaveArticleTags(ArticleId articleId, IEnumerable <string> tags)
        {
            var normalizedTags = GetNormalizedCollection(tags);

            var article = this.GetArticle(articleId) ?? new Article(articleId);

            article.Tags = normalizedTags;

            this.CurrentSession.SaveChanges();

            return(normalizedTags);
        }
Beispiel #11
0
        public ArticleRevision GetArticleRevisionLatest(ArticleId articleId)
        {
            if (articleId == null || !articleId.HasValue)
            {
                return(null);
            }

            var query = this.GetArticleRevisionsQuery <ArticleRevisionsIndex>(articleId);

            var result = query.OrderByDescending(x => x.CreatedAt);

            return(result.As <ArticleRevision>().FirstOrDefault());
        }
Beispiel #12
0
        public static string CreateArticleRevisionId(ArticleId articleId, ArticleRevisionDate articleRevisionDate)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }
            if (articleRevisionDate == null)
            {
                throw new ArgumentNullException("articleRevisionDate");
            }

            return(CreateArticleRevisionId(articleId, articleRevisionDate.DateTime));
        }
Beispiel #13
0
        private IReadOnlyCollection <ArticleRedirect> SaveArticleRedirectsInternal(
            ArticleId redirectToArticleId,
            IEnumerable <ArticleRedirect> articleRedirects)
        {
            if (articleRedirects == null)
            {
                throw new ArgumentNullException("articleRedirects");
            }

            var redirects = articleRedirects.ToList();

            if (!redirects.Any())
            {
                return(redirects);
            }

            var articleRedirectIds = redirects.Select(IdUtility.CreateArticleRedirectId).ToList();

            var storedRedirects = this.CurrentSession.Load <ArticleRedirect>(articleRedirectIds);

            var ensuredRedirects = new List <ArticleRedirect>();

            for (int i = 0; i < storedRedirects.Length; i++)
            {
                var redirect = storedRedirects[i] ?? redirects[i];

                bool redirectExistsAsArticle = GetRedirectExistsAsArticle(redirect);
                if (redirectExistsAsArticle)
                {
                    continue;
                }

                this.CurrentSession.Store(redirect);

                ensuredRedirects.Add(redirect);
            }

            var ensuredOriginalArticleSlugs = ensuredRedirects.Select(x => x.RedirectFromArticleSlug).ToList();

            var deletedRedirects =
                this.CurrentSession.Query <ArticleRedirect, ArticleRedirectsIndex>()
                .Where(
                    x =>
                    x.RedirectToArticleSlug == redirectToArticleId.Slug &&
                    !x.RedirectFromArticleSlug.In(ensuredOriginalArticleSlugs))
                .ToList();

            deletedRedirects.ForEach(x => this.CurrentSession.Delete(x));

            return(ensuredRedirects.OrderBy(x => x.RedirectFromArticleSlug).ToList());
        }
Beispiel #14
0
        public IReadOnlyCollection <ArticleId> GetArticleRedirectsList(ArticleId redirectToArticleId)
        {
            if (redirectToArticleId == null || !redirectToArticleId.HasValue)
            {
                return(Enumerable.Empty <ArticleId>().ToList());
            }

            var result = (from redirect in this.CurrentSession.Query <ArticleRedirect, ArticleRedirectsIndex>()
                          where redirect.RedirectToArticleSlug == redirectToArticleId.Slug
                          orderby redirect.RedirectFromArticleSlug
                          select redirect).As <ArticleRedirect>().ToList();

            return(result.Select(x => new ArticleId(x.RedirectFromArticleSlug)).ToList());
        }
Beispiel #15
0
        private IRavenQueryable <ArticleRevisionsIndex.Result> GetArticleRevisionsQuery <TIndexCreator>(
            ArticleId articleId = null) where TIndexCreator : AbstractIndexCreationTask, new()
        {
            var query = from revision in this.CurrentSession.Query <ArticleRevisionsIndex.Result, TIndexCreator>()
                        where (revision.LastPublishedAt != null || revision.CreatedByUserName == this.principalId)
                        orderby revision.CreatedAt descending
                        select revision;

            if (articleId != null && articleId.HasValue)
            {
                query = query.Where(x => x.ArticleId == articleId.Id);
            }

            return(query);
        }
Beispiel #16
0
        public IReadOnlyCollection <ArticleRevisionItem> GetArticleRevisionsList(ArticleId articleId)
        {
            if (articleId == null || !articleId.HasValue)
            {
                return(Enumerable.Empty <ArticleRevisionItem>().ToList());
            }

            var query = this.GetArticleRevisionsQuery <ArticleRevisionsIndex>(articleId);

            var result =
                query.Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(15)))
                .AsProjection <ArticleRevisionItem>();

            return(result.ToList());
        }
Beispiel #17
0
        public IReadOnlyCollection <ArticleRedirect> SaveArticleRedirects(
            ArticleId redirectToArticleId,
            IEnumerable <string> articleRedirects)
        {
            if (redirectToArticleId == null)
            {
                throw new ArgumentNullException("redirectToArticleId");
            }

            var normalizedRedirects = GetNormalizedCollection(articleRedirects);

            var redirects = normalizedRedirects.Select(x => new ArticleRedirect(x, redirectToArticleId));

            return(this.SaveArticleRedirects(redirectToArticleId, redirects));
        }
Beispiel #18
0
        public static string CreateArticleRevisionId(ArticleId articleId, DateTime articleRevisionDate)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }
            if (articleRevisionDate == DateTime.MinValue)
            {
                throw new ArgumentOutOfRangeException("articleRevisionDate");
            }

            string createdAtFormatted = articleRevisionDate.ToString(ArticleRevisionDate.IdDateTimeFormat);

            return(String.Format("{0}/{1}", articleId.Id, createdAtFormatted));
        }
Beispiel #19
0
        public ArticleId GetArticleExists(ArticleId articleId)
        {
            var article = this.GetArticle(articleId);

            if (article != null)
            {
                return(article.Id);
            }

            var redirect = this.GetArticleRedirect(articleId);

            if (redirect != null)
            {
                return(redirect.RedirectToArticleSlug);
            }

            return(null);
        }
Beispiel #20
0
        public ArticleResult SaveArticleRevision(
            ArticleId articleId,
            ArticleRevision articleRevision,
            bool publishRevision = false)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }
            if (articleRevision == null)
            {
                throw new ArgumentNullException("articleRevision");
            }

            var article = this.GetArticle(articleId) ?? new Article(articleId);

            return(this.SaveArticle(article, articleRevision, publishRevision));
        }
Beispiel #21
0
        public IReadOnlyCollection <ArticleRedirect> SaveArticleRedirects(
            ArticleId redirectToArticleId,
            IEnumerable <ArticleRedirect> articleRedirects)
        {
            if (redirectToArticleId == null)
            {
                throw new ArgumentNullException("redirectToArticleId");
            }
            if (articleRedirects == null)
            {
                throw new ArgumentNullException("articleRedirects");
            }

            var savedArticleRedirects = this.SaveArticleRedirectsInternal(redirectToArticleId, articleRedirects);

            this.CurrentSession.SaveChanges();

            return(savedArticleRedirects);
        }
Beispiel #22
0
        public bool RevertArticleToDraft(ArticleId articleId)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }


            var article = this.GetArticle(articleId);

            if (article == null)
            {
                return(false);
            }

            article.ActiveRevisionId = null;

            this.CurrentSession.SaveChanges();

            return(true);
        }
Beispiel #23
0
        public ArticleRevision(
            ArticleId articleId,
            IPrincipal principal,
            string markdownContent,
            string editSummary = null)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }
            if (principal == null)
            {
                throw new ArgumentNullException("principal");
            }

            this.ArticleId         = articleId.Id;
            this.CreatedByUserName = principal.Identity.Name;
            this.EditSummary       = editSummary;
            this.MarkdownContent   = markdownContent;
            this.TextContent       = MarkdownService.MakeTextFlat(this.MarkdownContent);

            this.CreatedAt = DateTime.UtcNow;
        }
Beispiel #24
0
        public static bool GetIsEnsured(ArticleId articleId)
        {
            string ensuredSlug = articleId.Slug;

            return(articleId.OriginalSlug == ensuredSlug);
        }
Beispiel #25
0
        public ArticleRedirect GetArticleRedirect(ArticleId originalArticleId)
        {
            string articleRedirectId = IdUtility.CreateArticleRedirectId(originalArticleId);

            return(this.CurrentSession.Load <ArticleRedirect>(articleRedirectId));
        }
Beispiel #26
0
        public ArticleResult GetArticleResult(string articleId, DateTime?revisionDate = null)
        {
            var articleIdValue = new ArticleId(articleId);

            return(this.GetArticleResult(articleIdValue, revisionDate));
        }
Beispiel #27
0
 public Article GetArticle(ArticleId articleId)
 {
     return((articleId != null && articleId.HasValue) ? this.CurrentSession.Load <Article>(articleId.Id) : null);
 }
Beispiel #28
0
 public ArticleRedirect(ArticleId redirectFromArticleId, ArticleId redirectToArticleId)
 {
     this.RedirectFromArticleSlug = redirectFromArticleId.Slug;
     this.RedirectToArticleSlug   = redirectToArticleId.Slug;
 }