Shifting BlogPart

This commit is contained in:
Sebastien Ros 2014-02-21 10:56:40 -08:00
parent ce535797aa
commit 6f7d2b3ba1
12 changed files with 36 additions and 64 deletions

View File

@ -29,7 +29,7 @@ namespace Orchard.Blogs.Drivers {
} }
var blogPosts = _contentManager.Query(VersionOptions.Published, "BlogPost") var blogPosts = _contentManager.Query(VersionOptions.Published, "BlogPost")
.Join<CommonPartRecord>().Where(cr => cr.Container == blog.Record.ContentItemRecord) .Join<CommonPartRecord>().Where(cr => cr.Container.Id == blog.Id)
.OrderByDescending(cr => cr.CreatedUtc) .OrderByDescending(cr => cr.CreatedUtc)
.Slice(0, part.Count) .Slice(0, part.Count)
.Select(ci => ci.As<BlogPostPart>()); .Select(ci => ci.As<BlogPostPart>());

View File

@ -27,28 +27,6 @@ namespace Orchard.Blogs.Handlers {
_workContextAccessor = workContextAccessor; _workContextAccessor = workContextAccessor;
_contentManager = contentManager; _contentManager = contentManager;
//OnVersioning<BlogPostPart>((context, bp1, bp2) => {
// var commonPart = bp1.As<ICommonPart>();
// if (commonPart == null || !commonPart.CreatedUtc.HasValue)
// return;
// if (context.BuildingItemVersionRecord.Published == context.ExistingContentItem.IsPublished()) {
// return;
// }
// var previousPublishedVersion = contentManager.Get(commonPart.Id, VersionOptions.Published);
// // retrieve the creation date when it was published
// if (previousPublishedVersion != null) {
// var versionCommonPart = previousPublishedVersion.As<ICommonPart>();
// if (versionCommonPart.VersionCreatedUtc.HasValue) {
// _previousCreatedUtc[commonPart.Id] = versionCommonPart.VersionCreatedUtc.Value;
// }
// }
//});
//OnUpdating<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
OnPublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id)); OnPublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
OnRemoving<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id)); OnRemoving<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
OnUnpublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id)); OnUnpublishing<BlogPostPart>((context, bp) => SavePreviousPublishDate(context.Id));
@ -90,7 +68,7 @@ namespace Orchard.Blogs.Handlers {
var datetime = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone); var datetime = TimeZoneInfo.ConvertTimeFromUtc(commonPart.CreatedUtc.Value, timeZone);
var previousArchiveRecord = _blogArchiveRepository.Table var previousArchiveRecord = _blogArchiveRepository.Table
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
&& x.Month == datetime.Month && x.Month == datetime.Month
&& x.Year == datetime.Year); && x.Year == datetime.Year);
@ -133,7 +111,7 @@ namespace Orchard.Blogs.Handlers {
// decrement previous archive record // decrement previous archive record
var previousArchiveRecord = _blogArchiveRepository var previousArchiveRecord = _blogArchiveRepository
.Table .Table
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
&& x.Month == previousMonth && x.Month == previousMonth
&& x.Year == previousYear); && x.Year == previousYear);
@ -149,13 +127,13 @@ namespace Orchard.Blogs.Handlers {
// increment new archive record // increment new archive record
var newArchiveRecord = _blogArchiveRepository var newArchiveRecord = _blogArchiveRepository
.Table .Table
.FirstOrDefault(x => x.BlogPart == blogPostPart.BlogPart.Record .FirstOrDefault(x => x.BlogPart.Id == blogPostPart.BlogPart.Id
&& x.Month == newMonth && x.Month == newMonth
&& x.Year == newYear); && x.Year == newYear);
// if record can't be found create it // if record can't be found create it
if (newArchiveRecord == null) { if (newArchiveRecord == null) {
newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPostPart.BlogPart.Record, Year = newYear, Month = newMonth, PostCount = 0 }; newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPostPart.BlogPart.ContentItem.Record, Year = newYear, Month = newMonth, PostCount = 0 };
_blogArchiveRepository.Create(newArchiveRecord); _blogArchiveRepository.Create(newArchiveRecord);
} }

View File

@ -9,9 +9,7 @@ namespace Orchard.Blogs.Handlers {
[UsedImplicitly] [UsedImplicitly]
public class BlogPartHandler : ContentHandler { public class BlogPartHandler : ContentHandler {
public BlogPartHandler(IRepository<BlogPartRecord> repository) { public BlogPartHandler() {
Filters.Add(StorageFilter.For(repository));
OnGetDisplayShape<BlogPart>((context, blog) => { OnGetDisplayShape<BlogPart>((context, blog) => {
context.Shape.Description = blog.Description; context.Shape.Description = blog.Description;
context.Shape.PostCount = blog.PostCount; context.Shape.PostCount = blog.PostCount;

View File

@ -15,13 +15,6 @@ namespace Orchard.Blogs {
.Column<int>("BlogPart_id") .Column<int>("BlogPart_id")
); );
SchemaBuilder.CreateTable("BlogPartRecord",
table => table
.ContentPartRecord()
.Column<string>("Description", c => c.Unlimited())
.Column<int>("PostCount")
);
SchemaBuilder.CreateTable("RecentBlogPostsPartRecord", SchemaBuilder.CreateTable("RecentBlogPostsPartRecord",
table => table table => table
.ContentPartRecord() .ContentPartRecord()

View File

@ -2,20 +2,20 @@ using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.Aspects;
namespace Orchard.Blogs.Models { namespace Orchard.Blogs.Models {
public class BlogPart : ContentPart<BlogPartRecord> { public class BlogPart : ContentPart {
public string Name { public string Name {
get { return this.As<ITitleAspect>().Title; } get { return this.As<ITitleAspect>().Title; }
} }
public string Description { public string Description {
get { return Retrieve(x => x.Description); } get { return this.Retrieve(x => x.Description); }
set { Store(x => x.Description, value); } set { this.Store(x => x.Description, value); }
} }
public int PostCount { public int PostCount {
get { return Retrieve(x => x.PostCount); } get { return this.Retrieve(x => x.PostCount); }
set { Store(x => x.PostCount, value); } set { this.Store(x => x.PostCount, value); }
} }
} }
} }

View File

@ -1,7 +1,9 @@
using Orchard.ContentManagement.Records;
namespace Orchard.Blogs.Models { namespace Orchard.Blogs.Models {
public class BlogPartArchiveRecord { public class BlogPartArchiveRecord {
public virtual int Id { get; set; } public virtual int Id { get; set; }
public virtual BlogPartRecord BlogPart { get; set; } public virtual ContentItemRecord BlogPart { get; set; }
public virtual int Year { get; set; } public virtual int Year { get; set; }
public virtual int Month { get; set; } public virtual int Month { get; set; }
public virtual int PostCount { get; set; } public virtual int PostCount { get; set; }

View File

@ -1,10 +0,0 @@
using Orchard.ContentManagement.Records;
using Orchard.Data.Conventions;
namespace Orchard.Blogs.Models {
public class BlogPartRecord : ContentPartRecord {
[StringLengthMax]
public virtual string Description { get; set; }
public virtual int PostCount { get; set; }
}
}

View File

@ -104,7 +104,6 @@
<Compile Include="Handlers\BlogPartHandler.cs" /> <Compile Include="Handlers\BlogPartHandler.cs" />
<Compile Include="Models\BlogPostPart.cs" /> <Compile Include="Models\BlogPostPart.cs" />
<Compile Include="Handlers\BlogPostPartHandler.cs" /> <Compile Include="Handlers\BlogPostPartHandler.cs" />
<Compile Include="Models\BlogPartRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Routes.cs" /> <Compile Include="Routes.cs" />
<Compile Include="Services\BlogPostService.cs" /> <Compile Include="Services\BlogPostService.cs" />

View File

@ -23,13 +23,13 @@ namespace Orchard.Blogs.Services {
public void RebuildArchive(BlogPart blogPart) { public void RebuildArchive(BlogPart blogPart) {
var first = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id).OrderBy<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault(); var first = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Id).OrderBy<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
if (first == null) { if (first == null) {
return; return;
} }
var last = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id).OrderByDescending<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault(); var last = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Id).OrderByDescending<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
DateTime? start = DateTime.MaxValue; DateTime? start = DateTime.MaxValue;
if (first.As<CommonPart>() != null) { if (first.As<CommonPart>() != null) {
@ -42,7 +42,7 @@ namespace Orchard.Blogs.Services {
} }
// delete previous archive records // delete previous archive records
foreach (var record in _blogArchiveRepository.Table.Where(x => x.BlogPart == blogPart.Record)) { foreach (var record in _blogArchiveRepository.Table.Where(x => x.BlogPart.Id == blogPart.Id)) {
_blogArchiveRepository.Delete(record); _blogArchiveRepository.Delete(record);
} }
@ -55,7 +55,7 @@ namespace Orchard.Blogs.Services {
// build a collection of all the post dates // build a collection of all the post dates
var blogPostDates = new List<DateTime>(); var blogPostDates = new List<DateTime>();
var blogPosts = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Record.Id); var blogPosts = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(bp => bp.Container.Id == blogPart.Id);
foreach (var blogPost in blogPosts.List()) { foreach (var blogPost in blogPosts.List()) {
if (blogPost.As<CommonPart>() != null) if (blogPost.As<CommonPart>() != null)
if (blogPost.As<CommonPart>().CreatedUtc.HasValue) { if (blogPost.As<CommonPart>().CreatedUtc.HasValue) {
@ -82,7 +82,7 @@ namespace Orchard.Blogs.Services {
//var count = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count(); //var count = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count();
var count = blogPostDates.Count(bp => bp >= @from && bp < to); var count = blogPostDates.Count(bp => bp >= @from && bp < to);
var newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPart.Record, Year = year, Month = month, PostCount = count }; var newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPart.ContentItem.Record, Year = year, Month = month, PostCount = count };
_blogArchiveRepository.Create(newArchiveRecord); _blogArchiveRepository.Create(newArchiveRecord);
} }
} }

View File

@ -60,7 +60,7 @@ namespace Orchard.Blogs.Services {
public int PostCount(BlogPart blogPart, VersionOptions versionOptions) { public int PostCount(BlogPart blogPart, VersionOptions versionOptions) {
return _contentManager.Query(versionOptions, "BlogPost") return _contentManager.Query(versionOptions, "BlogPost")
.Join<CommonPartRecord>().Where( .Join<CommonPartRecord>().Where(
cr => cr.Container == blogPart.Record.ContentItemRecord) cr => cr.Container.Id == blogPart.Id)
.Count(); .Count();
} }
@ -90,7 +90,7 @@ namespace Orchard.Blogs.Services {
public IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(BlogPart blogPart) { public IEnumerable<KeyValuePair<ArchiveData, int>> GetArchives(BlogPart blogPart) {
var query = var query =
from bar in _blogArchiveRepository.Table from bar in _blogArchiveRepository.Table
where bar.BlogPart == blogPart.Record where bar.BlogPart.Id == blogPart.Id
orderby bar.Year descending, bar.Month descending orderby bar.Year descending, bar.Month descending
select bar; select bar;
@ -124,11 +124,11 @@ namespace Orchard.Blogs.Services {
return (task == null ? null : task.ScheduledUtc); return (task == null ? null : task.ScheduledUtc);
} }
private IContentQuery<ContentItem, CommonPartRecord> GetBlogQuery(ContentPart<BlogPartRecord> blog, VersionOptions versionOptions) { private IContentQuery<ContentItem, CommonPartRecord> GetBlogQuery(BlogPart blog, VersionOptions versionOptions) {
return return
_contentManager.Query(versionOptions, "BlogPost") _contentManager.Query(versionOptions, "BlogPost")
.Join<CommonPartRecord>().Where( .Join<CommonPartRecord>().Where(
cr => cr.Container == blog.Record.ContentItemRecord).OrderByDescending(cr => cr.CreatedUtc) cr => cr.Container.Id == blog.Id).OrderByDescending(cr => cr.CreatedUtc)
; ;
} }
} }

View File

@ -42,7 +42,7 @@ namespace Orchard.Blogs.Services {
} }
public IEnumerable<BlogPart> Get(VersionOptions versionOptions) { public IEnumerable<BlogPart> Get(VersionOptions versionOptions) {
return _contentManager.Query<BlogPart, BlogPartRecord>(versionOptions) return _contentManager.Query<BlogPart>(versionOptions, "Blog")
.Join<TitlePartRecord>() .Join<TitlePartRecord>()
.OrderBy(br => br.Title) .OrderBy(br => br.Title)
.List(); .List();

View File

@ -252,6 +252,18 @@ namespace Upgrade.Controllers {
#endregion #endregion
#region BlogPartRecord
var blogTable = _upgradeService.GetPrefixedTableName("Orchard_Blogs_BlogPartRecord");
if (_upgradeService.TableExists(blogTable)) {
_upgradeService.ExecuteReader("SELECT * FROM " + blogTable,
(reader, connection) => {
site.As<InfosetPart>().Store("BlogPart", "Description", ConvertToString(reader["Description"]));
});
_upgradeService.ExecuteReader("DROP TABLE " + blogTable, null);
}
#endregion
// todo: user records // todo: user records
_orchardServices.Notifier.Information(T("Site Settings migrated successfully")); _orchardServices.Notifier.Information(T("Site Settings migrated successfully"));