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.Models;
|
|
|
|
using Orchard.Core.Common.Records;
|
2009-11-21 08:38:41 +08:00
|
|
|
using Orchard.Data;
|
|
|
|
using Orchard.Models;
|
|
|
|
|
|
|
|
namespace Orchard.Blogs.Services {
|
|
|
|
public class BlogPostService : IBlogPostService {
|
|
|
|
private readonly IContentManager _contentManager;
|
2009-11-25 09:34:23 +08:00
|
|
|
private readonly IRepository<RoutableRecord> _routableRepository;
|
2009-11-21 08:38:41 +08:00
|
|
|
|
2009-11-25 09:34:23 +08:00
|
|
|
public BlogPostService(IContentManager contentManager, IRepository<RoutableRecord> routableRepository) {
|
2009-11-21 08:38:41 +08:00
|
|
|
_contentManager = contentManager;
|
2009-11-25 09:34:23 +08:00
|
|
|
_routableRepository = routableRepository;
|
2009-11-21 08:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public BlogPost Get(Blog blog, string slug) {
|
2009-11-25 09:34:23 +08:00
|
|
|
RoutableRecord record =
|
2009-11-27 12:55:05 +08:00
|
|
|
_routableRepository.Get(r => r.ContentItemRecord.ContentType.Name == "blogpost" && r.Slug == slug);
|
2009-11-25 09:34:23 +08:00
|
|
|
BlogPost blogPost = record != null ? _contentManager.Get<BlogPost>(record.Id) : null;
|
2009-11-21 08:38:41 +08:00
|
|
|
|
2009-11-26 07:12:19 +08:00
|
|
|
return blogPost != null && blogPost.Record.Blog.Id == blog.ContentItem.Id ? blogPost : null;
|
2009-11-21 08:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<BlogPost> Get(Blog blog) {
|
2009-11-25 09:34:23 +08:00
|
|
|
//TODO: (erikpo) Figure out how to sort by published date
|
|
|
|
IEnumerable<RoutableRecord> records =
|
2009-11-27 12:55:05 +08:00
|
|
|
_routableRepository.Fetch(rr => rr.ContentItemRecord.ContentType.Name == "blogpost"
|
2009-11-25 09:34:23 +08:00
|
|
|
/*, bpr => bpr.Asc(bpr2 => bpr2.Published.GetValueOrDefault(new DateTime(2099, 1, 1)))*/);
|
2009-11-21 08:38:41 +08:00
|
|
|
|
2009-11-26 07:12:19 +08:00
|
|
|
//TODO: (erikpo) Need to filter by blog in the line above instead of filtering here
|
2009-11-25 09:34:23 +08:00
|
|
|
return
|
|
|
|
records.Select(r => _contentManager.Get(r.Id).As<BlogPost>()).Where(
|
2009-11-26 07:12:19 +08:00
|
|
|
bp => bp.Record.Blog.Id == blog.ContentItem.Id);
|
2009-11-25 09:34:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public BlogPost Create(CreateBlogPostParams parameters) {
|
|
|
|
return _contentManager.Create<BlogPost>("blogpost", bp =>
|
|
|
|
{
|
|
|
|
bp.Record.Blog = parameters.Blog.Record;
|
2009-12-02 04:24:18 +08:00
|
|
|
bp.As<BodyAspect>().Record.Text = parameters.Body;
|
|
|
|
bp.As<BodyAspect>().Record.Format = "html";
|
2009-11-25 09:34:23 +08:00
|
|
|
bp.Record.Published = parameters.Published;
|
|
|
|
bp.As<RoutableAspect>().Record.Title = parameters.Title;
|
|
|
|
bp.As<RoutableAspect>().Record.Slug = parameters.Slug;
|
2009-12-03 09:37:45 +08:00
|
|
|
bp.Record.Blog.PostCount++;
|
2009-11-25 09:34:23 +08:00
|
|
|
});
|
2009-11-21 08:38:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|