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;
|
2010-01-21 05:55:49 +08:00
|
|
|
using Orchard.Blogs.Controllers;
|
2009-11-21 08:38:41 +08:00
|
|
|
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-21 05:55:49 +08:00
|
|
|
_contentManager.Query(versionOptions, BlogPostDriver.ContentType.Name).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
|
|
|
|
2010-01-15 06:00:11 +08:00
|
|
|
if (archiveData.Day > 0) {
|
|
|
|
var dayDate = new DateTime(archiveData.Year, archiveData.Month, archiveData.Day);
|
|
|
|
|
|
|
|
query = query.Where(cr => cr.CreatedUtc >= dayDate && cr.CreatedUtc < dayDate.AddDays(1));
|
|
|
|
}
|
2010-01-07 09:27:09 +08:00
|
|
|
else if (archiveData.Month > 0)
|
2010-01-15 06:00:11 +08:00
|
|
|
{
|
|
|
|
var monthDate = new DateTime(archiveData.Year, archiveData.Month, 1);
|
|
|
|
|
|
|
|
query = query.Where(cr => cr.CreatedUtc >= monthDate && cr.CreatedUtc < monthDate.AddMonths(1));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var yearDate = new DateTime(archiveData.Year, 1, 1);
|
|
|
|
|
|
|
|
query = query.Where(cr => cr.CreatedUtc >= yearDate && cr.CreatedUtc < yearDate.AddYears(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
|
|
|
}
|
|
|
|
|
2010-01-15 06:00:11 +08:00
|
|
|
public IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(Blog blog) {
|
|
|
|
return new List<KeyValuePair<ArchiveData, int>> {
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2010/1"), 5),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/12"), 23),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/11"), 4),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/9"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/8"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/7"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/6"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/5"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/4"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/3"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/2"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2009/1"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/12"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/11"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/10"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/9"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/7"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/6"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/5"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/4"), 1),
|
|
|
|
new KeyValuePair<ArchiveData, int>(new ArchiveData("2008/3"), 1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-21 05:55:49 +08:00
|
|
|
_contentManager.Query(versionOptions, BlogPostDriver.ContentType.Name).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
|
|
|
}
|
|
|
|
}
|