First step at simplifying Tags implementation

--HG--
branch : dev
rename : src/Orchard.Web/Modules/Orchard.Tags/Models/Tag.cs => src/Orchard.Web/Modules/Orchard.Tags/Models/TagRecord.cs
This commit is contained in:
Renaud Paquay
2010-11-25 09:26:44 -08:00
parent a7a6067497
commit a32765d653
14 changed files with 100 additions and 97 deletions

View File

@@ -25,7 +25,7 @@ namespace Orchard.Tags.Controllers {
public Localizer T { get; set; }
public ActionResult Index() {
IEnumerable<Tag> tags = _tagService.GetTags();
IEnumerable<TagRecord> tags = _tagService.GetTags();
var entries = tags.Select(CreateTagEntry).ToList();
var model = new TagsAdminIndexViewModel { Tags = entries };
return View(model);
@@ -50,7 +50,7 @@ namespace Orchard.Tags.Controllers {
return new HttpUnauthorizedResult();
foreach (TagEntry entry in checkedEntries) {
_tagService.DeleteTag(entry.Tag.Id);
_tagService.DeleteTag(entry.TagRecord.Id);
}
break;
@@ -80,15 +80,15 @@ namespace Orchard.Tags.Controllers {
}
public ActionResult Edit(int id) {
Tag tag = _tagService.GetTag(id);
TagRecord tagRecord = _tagService.GetTag(id);
if(tag == null) {
if(tagRecord == null) {
return RedirectToAction("Index");
}
var viewModel = new TagsAdminEditViewModel {
Id = tag.Id,
TagName = tag.TagName,
Id = tagRecord.Id,
TagName = tagRecord.TagName,
};
ViewData["ContentItems"] = _tagService.GetTaggedContentItems(id).ToList();
@@ -116,9 +116,9 @@ namespace Orchard.Tags.Controllers {
if (!Services.Authorizer.Authorize(Permissions.ManageTags, T("Couldn't remove tag")))
return new HttpUnauthorizedResult();
Tag tag = _tagService.GetTag(id);
TagRecord tagRecord = _tagService.GetTag(id);
if (tag == null)
if (tagRecord == null)
return new HttpNotFoundResult();
_tagService.DeleteTag(id);
@@ -129,9 +129,9 @@ namespace Orchard.Tags.Controllers {
return RedirectToAction("Index");
}
private static TagEntry CreateTagEntry(Tag tag) {
private static TagEntry CreateTagEntry(TagRecord tagRecord) {
return new TagEntry {
Tag = tag,
TagRecord = tagRecord,
IsChecked = false,
};
}

View File

@@ -50,7 +50,7 @@ namespace Orchard.Tags.Drivers {
var tagNames = TagHelpers.ParseCommaSeparatedTagNames(model.Tags);
if (part.ContentItem.Id != 0) {
_tagService.UpdateTagsForContentItem(part.ContentItem.Id, tagNames);
_tagService.UpdateTagsForContentItem(part.ContentItem, tagNames);
}
return ContentShape("Parts_Tags_Edit",

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
@@ -10,20 +9,13 @@ using Orchard.Tags.Models;
namespace Orchard.Tags.Handlers {
[UsedImplicitly]
public class TagsPartHandler : ContentHandler {
public TagsPartHandler(IRepository<Tag> tagsRepository, IRepository<TagsContentItems> tagsContentItemsRepository) {
public TagsPartHandler(IRepository<TagRecord> tagsRepository, IRepository<TagsContentItems> tagsContentItemsRepository) {
OnLoading<TagsPart>((context, tags) => {
// provide names of all tags on demand
tags._allTags.Loader(list => tagsRepository.Table.ToList());
// populate list of attached tags on demand
tags._currentTags.Loader(list => {
var tagsContentItems = tagsContentItemsRepository.Fetch(x => x.ContentItemId == context.ContentItem.Id);
foreach (var tagContentItem in tagsContentItems) {
var tag = tagsRepository.Get(tagContentItem.TagId);
list.Add(tag);
}
foreach(var tag in tagsContentItemsRepository.Fetch(x => x.ContentItem == context.ContentItem.Record))
list.Add(tag.Tag);
return list;
});
@@ -32,17 +24,17 @@ namespace Orchard.Tags.Handlers {
OnRemoved<TagsPart>((context, tags) => {
tagsContentItemsRepository.Flush();
TagsPart tagsPart = context.ContentItem.As<TagsPart>();
var tagsPart = context.ContentItem.As<TagsPart>();
// delete orphan tags (for each tag, if there is no other contentItem than the one being deleted, it's an orphan)
foreach ( var tag in tagsPart.CurrentTags ) {
if ( tagsContentItemsRepository.Fetch(x => x.ContentItemId != context.ContentItem.Id).Count() == 0 ) {
if ( tagsContentItemsRepository.Fetch(x => x.ContentItem != context.ContentItem.Record).Count() == 0 ) {
tagsRepository.Delete(tag);
}
}
// delete tag links with this contentItem (tagsContentItems)
foreach ( var tagsContentItem in tagsContentItemsRepository.Fetch(x => x.ContentItemId == context.ContentItem.Id) ) {
foreach ( var tagsContentItem in tagsContentItemsRepository.Fetch(x => x.ContentItem == context.ContentItem.Record) ) {
tagsContentItemsRepository.Delete(tagsContentItem);
}

View File

@@ -6,7 +6,7 @@ namespace Orchard.Tags {
public class TagsDataMigration : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable("Tag",
SchemaBuilder.CreateTable("TagRecord",
table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("TagName")

View File

@@ -1,5 +1,5 @@
namespace Orchard.Tags.Models {
public class Tag {
public class TagRecord {
public virtual int Id { get; set; }
public virtual string TagName { get; set; }
}

View File

@@ -1,7 +1,9 @@
using Orchard.ContentManagement.Records;
namespace Orchard.Tags.Models {
public class TagsContentItems {
public virtual int Id { get; set; }
public virtual int TagId { get; set; }
public virtual int ContentItemId { get; set; }
public virtual TagRecord Tag { get; set; }
public virtual ContentItemRecord ContentItem { get; set; }
}
}

View File

@@ -3,16 +3,13 @@ using Orchard.ContentManagement;
using Orchard.ContentManagement.Utilities;
namespace Orchard.Tags.Models {
public class TagsPart : ContentPart {
public class TagsPart : ContentPart<TagsPartRecord> {
public TagsPart() {
AllTags = new List<Tag>();
CurrentTags = new List<Tag>();
CurrentTags = new List<TagRecord>();
}
public readonly LazyField<IList<Tag>> _allTags = new LazyField<IList<Tag>>();
public readonly LazyField<IList<Tag>> _currentTags = new LazyField<IList<Tag>>();
public readonly LazyField<IList<TagRecord>> _currentTags = new LazyField<IList<TagRecord>>();
public IList<Tag> AllTags { get { return _allTags.Value; } set { _allTags.Value = value; } }
public IList<Tag> CurrentTags { get { return _currentTags.Value; } set { _currentTags.Value = value; } }
public IList<TagRecord> CurrentTags { get { return _currentTags.Value; } set { _currentTags.Value = value; } }
}
}

View File

@@ -0,0 +1,6 @@
using Orchard.ContentManagement.Records;
namespace Orchard.Tags.Models {
public class TagsPartRecord : ContentPartRecord {
}
}

View File

@@ -55,6 +55,7 @@
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Models\TagsContentItems.cs" />
<Compile Include="Models\TagsPartRecord.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\ITagService.cs" />
<Compile Include="Services\XmlRpcHandler.cs" />
@@ -63,7 +64,7 @@
<Compile Include="Drivers\TagsPartDriver.cs" />
<Compile Include="Helpers\TagHelpers.cs" />
<Compile Include="Models\TagsPart.cs" />
<Compile Include="Models\Tag.cs" />
<Compile Include="Models\TagRecord.cs" />
<Compile Include="Handlers\TagsPartHandler.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -4,14 +4,17 @@ using Orchard.Tags.Models;
namespace Orchard.Tags.Services {
public interface ITagService : IDependency {
IEnumerable<Tag> GetTags();
Tag GetTag(int id);
Tag GetTagByName(string tagName);
IEnumerable<TagRecord> GetTags();
TagRecord GetTag(int tagId);
TagRecord GetTagByName(string tagName);
IEnumerable<IContent> GetTaggedContentItems(int tagId);
void CreateTag(string tagName);
void DeleteTag(int id);
void UpdateTag(int id, string tagName);
IEnumerable<IContent> GetTaggedContentItems(int id);
void TagContentItem(int contentItemId, string tagName);
void UpdateTagsForContentItem(int contentItemId, IEnumerable<string> tagNamesForContentItem);
void DeleteTag(int tagId);
void UpdateTag(int tagId, string tagName);
void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem);
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement.Records;
using Orchard.Data;
using Orchard.Localization;
using Orchard.Logging;
@@ -13,13 +14,13 @@ using Orchard.UI.Notify;
namespace Orchard.Tags.Services {
[UsedImplicitly]
public class TagService : ITagService {
private readonly IRepository<Tag> _tagRepository;
private readonly IRepository<TagRecord> _tagRepository;
private readonly IRepository<TagsContentItems> _tagsContentItemsRepository;
private readonly INotifier _notifier;
private readonly IAuthorizationService _authorizationService;
private readonly IOrchardServices _orchardServices;
public TagService(IRepository<Tag> tagRepository,
public TagService(IRepository<TagRecord> tagRepository,
IRepository<TagsContentItems> tagsContentItemsRepository,
INotifier notifier,
IAuthorizationService authorizationService,
@@ -36,15 +37,15 @@ namespace Orchard.Tags.Services {
public ILogger Logger { get; set; }
public Localizer T { get; set; }
public IEnumerable<Tag> GetTags() {
public IEnumerable<TagRecord> GetTags() {
return _tagRepository.Table.ToList();
}
public Tag GetTag(int id) {
return _tagRepository.Get(x => x.Id == id);
public TagRecord GetTag(int tagId) {
return _tagRepository.Get(x => x.Id == tagId);
}
public Tag GetTagByName(string tagName) {
public TagRecord GetTagByName(string tagName) {
return _tagRepository.Get(x => x.TagName == tagName);
}
@@ -52,97 +53,98 @@ namespace Orchard.Tags.Services {
if (_tagRepository.Get(x => x.TagName == tagName) == null) {
_authorizationService.CheckAccess(Permissions.CreateTag, _orchardServices.WorkContext.CurrentUser, null);
Tag tag = new Tag { TagName = tagName };
_tagRepository.Create(tag);
TagRecord tagRecord = new TagRecord { TagName = tagName };
_tagRepository.Create(tagRecord);
}
else {
_notifier.Warning(T("The tag {0} already exists", tagName));
}
}
public void DeleteTag(int id) {
_tagRepository.Delete(GetTag(id));
IEnumerable<TagsContentItems> tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.TagId == id);
public void DeleteTag(int tagId) {
_tagRepository.Delete(GetTag(tagId));
IEnumerable<TagsContentItems> tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.Tag.Id == tagId);
foreach (var tagContentItem in tagsContentItems) {
_tagsContentItemsRepository.Delete(tagContentItem);
}
}
public void UpdateTag(int id, string tagName) {
if ( String.IsNullOrEmpty(tagName) ) {
public void UpdateTag(int tagId, string tagName) {
if (String.IsNullOrEmpty(tagName)) {
_notifier.Warning(T("Couldn't rename tag: name was empty"));
return;
}
Tag tag = GetTagByName(tagName);
if(tag != null) {
// new tag name already existing => merge
IEnumerable<TagsContentItems> tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.TagId == id);
// get contentItems already tagged with the existing one
var taggedContentItems = GetTaggedContentItems(tag.Id);
var tagRecord = GetTagByName(tagName);
foreach ( var tagContentItem in tagsContentItems ) {
var tagContentItemId = tagContentItem.ContentItemId;
if ( !taggedContentItems.Any(c => c.ContentItem.Id == tagContentItemId) ) {
TagContentItem(tagContentItem.ContentItemId, tagName);
// new tag name already existing => merge
if (tagRecord != null) {
var tagsContentItems = _tagsContentItemsRepository.Fetch(x => x.Tag.Id == tagId);
// get contentItems already tagged with the existing one
var taggedContentItems = GetTaggedContentItems(tagRecord.Id);
foreach (var tagContentItem in tagsContentItems) {
if (!taggedContentItems.Any(c => c.ContentItem.Record == tagContentItem.ContentItem)) {
TagContentItem(tagContentItem.ContentItem, tagName);
}
_tagsContentItemsRepository.Delete(tagContentItem);
}
_tagRepository.Delete(GetTag(id));
}
else {
tag = _tagRepository.Get(id);
tag.TagName = tagName;
_tagRepository.Delete(GetTag(tagId));
return;
}
// Create new tag
tagRecord = _tagRepository.Get(tagId);
tagRecord.TagName = tagName;
}
public IEnumerable<IContent> GetTaggedContentItems(int id) {
public IEnumerable<IContent> GetTaggedContentItems(int tagId) {
return _tagsContentItemsRepository
.Fetch(x => x.TagId == id)
.Select(t =>_orchardServices.ContentManager.Get(t.ContentItemId))
.Where(c => c!= null);
.Fetch(x => x.Tag.Id == tagId)
.Select(t => _orchardServices.ContentManager.Get(t.ContentItem.Id))
.Where(c => c != null);
}
public void TagContentItem(int contentItemId, string tagName) {
Tag tag = GetTagByName(tagName);
TagsContentItems tagsContentItems = new TagsContentItems { ContentItemId = contentItemId, TagId = tag.Id };
private void TagContentItem(ContentItemRecord contentItem, string tagName) {
var tagRecord = GetTagByName(tagName);
var tagsContentItems = new TagsContentItems { ContentItem = contentItem, Tag = tagRecord };
_tagsContentItemsRepository.Create(tagsContentItems);
}
public void UpdateTagsForContentItem(int contentItemId, IEnumerable<string> tagNamesForContentItem) {
List<int> tags = new List<int>();
public void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem) {
var tags = new List<TagRecord>();
foreach (var tagName in tagNamesForContentItem) {
Tag tag = GetTagByName(tagName);
if (tag == null) {
TagRecord tagRecord = GetTagByName(tagName);
if (tagRecord == null) {
CreateTag(tagName);
tag = GetTagByName(tagName);
tagRecord = GetTagByName(tagName);
}
tags.Add(tag.Id);
tags.Add(tagRecord);
}
ModifyTagsForContentItem(contentItemId, tags);
ModifyTagsForContentItem(contentItem, tags);
}
private void ModifyTagsForContentItem(int contentItemId, IEnumerable<int> tagsForContentItem) {
List<int> newTagsForContentItem = new List<int>(tagsForContentItem);
IEnumerable<TagsContentItems> currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItemId == contentItemId);
private void ModifyTagsForContentItem(ContentItem contentItem, IEnumerable<TagRecord> tagsForContentItem) {
var newTagsForContentItem = new List<TagRecord>(tagsForContentItem);
var currentTagsForContentItem = _tagsContentItemsRepository.Fetch(x => x.ContentItem == contentItem.Record);
foreach (var tagContentItem in currentTagsForContentItem) {
if (!newTagsForContentItem.Contains(tagContentItem.TagId)) {
if (!newTagsForContentItem.Contains(tagContentItem.Tag)) {
_authorizationService.CheckAccess(Permissions.ApplyTag, _orchardServices.WorkContext.CurrentUser, null);
_tagsContentItemsRepository.Delete(tagContentItem);
}
else {
newTagsForContentItem.Remove(tagContentItem.TagId);
newTagsForContentItem.Remove(tagContentItem.Tag);
}
}
foreach (var newTagForContentItem in newTagsForContentItem) {
_authorizationService.CheckAccess(Permissions.ApplyTag, _orchardServices.WorkContext.CurrentUser, null);
_tagsContentItemsRepository.Create(new TagsContentItems { ContentItemId = contentItemId, TagId = newTagForContentItem });
_tagsContentItemsRepository.Create(new TagsContentItems { ContentItem = contentItem.Record, Tag = newTagForContentItem });
}
}
}

View File

@@ -158,7 +158,7 @@ namespace Orchard.Tags.Services {
return;
_orchardServices.WorkContext.CurrentUser = user;
_tagService.UpdateTagsForContentItem(id, tags);
_tagService.UpdateTagsForContentItem(contentItem, tags);
});
if (contentItemId > 0)

View File

@@ -8,7 +8,7 @@ namespace Orchard.Tags.ViewModels {
}
public class TagEntry {
public Tag Tag { get; set; }
public TagRecord TagRecord { get; set; }
public bool IsChecked { get; set; }
}

View File

@@ -3,6 +3,6 @@ using Orchard.Tags.Models;
namespace Orchard.Tags.ViewModels {
public class TagsIndexViewModel {
public IList<Tag> Tags { get; set; }
public IList<TagRecord> Tags { get; set; }
}
}