2010-01-07 09:27:09 +08:00
|
|
|
using System;
|
2009-11-21 08:38:41 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Orchard.Blogs.Models;
|
2009-11-25 09:34:23 +08:00
|
|
|
using Orchard.Core.Common.Records;
|
2009-12-22 04:29:53 +08:00
|
|
|
using Orchard.ContentManagement;
|
2009-11-21 08:38:41 +08:00
|
|
|
|
|
|
|
namespace Orchard.Blogs.Services {
|
|
|
|
public class BlogPostService : IBlogPostService {
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
2010-01-12 02:27:42 +08:00
|
|
|
public BlogPostService(IContentManager contentManager) {
|
2009-11-21 08:38:41 +08:00
|
|
|
_contentManager = contentManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlogPost Get(Blog blog, string slug) {
|
2010-01-12 02:27:42 +08:00
|
|
|
return Get(blog, slug, VersionOptions.Published);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlogPost Get(Blog blog, string slug, VersionOptions versionOptions) {
|
2010-01-07 09:27:09 +08:00
|
|
|
return
|
2010-01-12 05:05:24 +08:00
|
|
|
_contentManager.Query(versionOptions, "blogpost").Join<RoutableRecord>().Where(rr => rr.Slug == slug).
|
2010-01-07 09:27:09 +08:00
|
|
|
Join<CommonRecord>().Where(cr => cr.Container == blog.Record.ContentItemRecord).List().
|
2010-01-12 05:05:24 +08:00
|
|
|
SingleOrDefault().As<BlogPost>();
|
2009-11-21 08:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<BlogPost> Get(Blog blog) {
|
2010-01-12 02:27:42 +08:00
|
|
|
return Get(blog, VersionOptions.Published);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<BlogPost> Get(Blog blog, VersionOptions versionOptions) {
|
2010-01-12 05:05:24 +08:00
|
|
|
return GetBlogQuery(blog, versionOptions).List().Select(ci => ci.As<BlogPost>());
|
2010-01-07 09:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<BlogPost> Get(Blog blog, ArchiveData archiveData) {
|
2010-01-12 02:27:42 +08:00
|
|
|
var query = GetBlogQuery(blog, VersionOptions.Published);
|
2010-01-07 09:27:09 +08:00
|
|
|
|
|
|
|
if (archiveData.Day > 0)
|
2010-01-12 02:27:42 +08:00
|
|
|
query =
|
|
|
|
query.Where(
|
|
|
|
cr =>
|
|
|
|
cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, archiveData.Day) &&
|
|
|
|
cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month, archiveData.Day + 1));
|
2010-01-07 09:27:09 +08:00
|
|
|
else if (archiveData.Month > 0)
|
2010-01-12 02:27:42 +08:00
|
|
|
query =
|
|
|
|
query.Where(
|
|
|
|
cr =>
|
|
|
|
cr.CreatedUtc >= new DateTime(archiveData.Year, archiveData.Month, 1) &&
|
|
|
|
cr.CreatedUtc < new DateTime(archiveData.Year, archiveData.Month + 1, 1));
|
2010-01-07 09:27:09 +08:00
|
|
|
else
|
2010-01-12 02:27:42 +08:00
|
|
|
query =
|
|
|
|
query.Where(
|
|
|
|
cr =>
|
|
|
|
cr.CreatedUtc >= new DateTime(archiveData.Year, 1, 1) &&
|
|
|
|
cr.CreatedUtc < new DateTime(archiveData.Year + 1, 1, 1));
|
2010-01-07 09:27:09 +08:00
|
|
|
|
2010-01-12 05:05:24 +08:00
|
|
|
return query.List().Select(ci => ci.As<BlogPost>());
|
2009-11-25 09:34:23 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 03:50:57 +08:00
|
|
|
public void Delete(BlogPost blogPost) {
|
2010-01-12 02:27:42 +08:00
|
|
|
_contentManager.Remove(blogPost.ContentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Publish(BlogPost blogPost) {
|
|
|
|
_contentManager.Publish(blogPost.ContentItem);
|
2010-01-12 09:04:11 +08:00
|
|
|
//TODO: (erikpo) Not sure if this is needed or not
|
2010-01-12 02:27:42 +08:00
|
|
|
blogPost.Published = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
2010-01-12 09:04:11 +08:00
|
|
|
public void Publish(BlogPost blogPost, DateTime publishDate) {
|
|
|
|
//TODO: (erikpo) This logic should move out of blogs and pages and into content manager
|
|
|
|
if (blogPost.Published != null && blogPost.Published.Value >= DateTime.UtcNow)
|
|
|
|
_contentManager.Unpublish(blogPost.ContentItem);
|
|
|
|
blogPost.Published = publishDate;
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:27:42 +08:00
|
|
|
public void Unpublish(BlogPost blogPost) {
|
|
|
|
_contentManager.Unpublish(blogPost.ContentItem);
|
2010-01-12 09:04:11 +08:00
|
|
|
//TODO: (erikpo) Not sure if this is needed or not
|
2010-01-12 02:27:42 +08:00
|
|
|
blogPost.Published = null;
|
2009-12-04 03:50:57 +08:00
|
|
|
}
|
2010-01-07 09:27:09 +08:00
|
|
|
|
2010-01-12 05:05:24 +08:00
|
|
|
private IContentQuery<ContentItem, CommonRecord> GetBlogQuery(ContentPart<BlogRecord> blog, VersionOptions versionOptions) {
|
2010-01-07 09:27:09 +08:00
|
|
|
return
|
2010-01-12 05:05:24 +08:00
|
|
|
_contentManager.Query(versionOptions, "blogpost").Join<CommonRecord>().Where(
|
2010-01-07 09:27:09 +08:00
|
|
|
cr => cr.Container == blog.Record.ContentItemRecord).OrderByDescending(cr => cr.CreatedUtc);
|
|
|
|
}
|
2009-11-21 08:38:41 +08:00
|
|
|
}
|
|
|
|
}
|