Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 6
0
        public static string CreateArticleId(ArticleId articleId)
        {
            if (articleId == null)
            {
                throw new ArgumentNullException("articleId");
            }

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

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

            var articleId = new ArticleId(article.Slug);

            return(CreateArticleId(articleId));
        }
Esempio n. 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();
        }
Esempio n. 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);
        }
Esempio n. 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());
        }
Esempio n. 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));
        }
Esempio n. 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());
        }
Esempio n. 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());
        }
Esempio n. 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);
        }
Esempio n. 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());
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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;
        }
Esempio n. 24
0
        public static bool GetIsEnsured(ArticleId articleId)
        {
            string ensuredSlug = articleId.Slug;

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

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

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