Cleanup tags modules

Move code to remove content item in ITagService

Admin page for a tag shows all content items (even the ones
that are not published)

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-11-25 18:25:18 -08:00
parent 51c03b08f5
commit be9646daa1
5 changed files with 56 additions and 26 deletions

View File

@@ -96,6 +96,27 @@ namespace Orchard.Tests.Modules.Tags.Services {
Assert.That(thing2.As<TagsPart>().CurrentTags.Any(tagRecord => tagRecord.TagName == "tag3"), Is.True);
}
[Test]
public void TagsShouldDeletedAferRemovingContentItem() {
var thing = _contentManager.New("thing");
_contentManager.Create(thing, VersionOptions.Published);
_tagService.UpdateTagsForContentItem(thing, new string[] { "tag1", "tag2", "tag3" });
ClearSession();
Assert.That(_tagService.GetTagByName("tag1"), Is.Not.Null);
Assert.That(_tagService.GetTagByName("tag2"), Is.Not.Null);
Assert.That(_tagService.GetTagByName("tag3"), Is.Not.Null);
_contentManager.Remove(_contentManager.Get(thing.Id));
ClearSession();
Assert.That(_tagService.GetTagByName("tag1"), Is.Null);
Assert.That(_tagService.GetTagByName("tag2"), Is.Null);
Assert.That(_tagService.GetTagByName("tag3"), Is.Null);
}
[Test]
public void ContentItemsShouldBeReturnedFromTagService() {
var thing1 = _contentManager.New("thing");

View File

@@ -91,7 +91,7 @@ namespace Orchard.Tags.Controllers {
TagName = tagRecord.TagName,
};
ViewData["ContentItems"] = _tagService.GetTaggedContentItems(id).ToList();
ViewData["ContentItems"] = _tagService.GetTaggedContentItems(id, VersionOptions.Latest).ToList();
return View(viewModel);
}

View File

@@ -1,38 +1,20 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
using Orchard.Tags.Models;
using Orchard.Tags.Services;
namespace Orchard.Tags.Handlers {
[UsedImplicitly]
public class TagsPartHandler : ContentHandler {
public TagsPartHandler(IRepository<TagsPartRecord> repository, IRepository<TagRecord> tagsRepository, IRepository<ContentTagRecord> tagsContentItemsRepository) {
public TagsPartHandler(IRepository<TagsPartRecord> repository, ITagService tagService) {
Filters.Add(StorageFilter.For(repository));
OnRemoved<TagsPart>((context, tags) => {
tagsContentItemsRepository.Flush();
OnRemoved<TagsPart>((context, tags) =>
tagService.RemoveTagsForContentItem(context.ContentItem));
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.TagsPartRecord.Id != context.ContentItem.Id).Count() == 0 ) {
tagsRepository.Delete(tag);
}
}
// delete tag links with this contentItem (tagsContentItems)
foreach ( var tagsContentItem in tagsContentItemsRepository.Fetch(x => x.TagsPartRecord.Id == context.ContentItem.Id) ) {
tagsContentItemsRepository.Delete(tagsContentItem);
}
});
OnIndexing<TagsPart>((context, tagsPart) => context.DocumentIndex
.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
OnIndexing<TagsPart>((context, tagsPart) =>
context.DocumentIndex.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze());
}
}
}

View File

@@ -8,6 +8,7 @@ namespace Orchard.Tags.Services {
TagRecord GetTag(int tagId);
TagRecord GetTagByName(string tagName);
IEnumerable<IContent> GetTaggedContentItems(int tagId);
IEnumerable<IContent> GetTaggedContentItems(int tagId, VersionOptions options);
TagRecord CreateTag(string tagName);
@@ -16,5 +17,6 @@ namespace Orchard.Tags.Services {
void UpdateTag(int tagId, string tagName);
void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem);
void RemoveTagsForContentItem(ContentItem contentItem);
}
}

View File

@@ -109,9 +109,13 @@ namespace Orchard.Tags.Services {
}
public IEnumerable<IContent> GetTaggedContentItems(int tagId) {
return GetTaggedContentItems(tagId, VersionOptions.Published);
}
public IEnumerable<IContent> GetTaggedContentItems(int tagId, VersionOptions options) {
return _contentTagRepository
.Fetch(x => x.TagRecord.Id == tagId)
.Select(t => _orchardServices.ContentManager.Get(t.TagsPartRecord.Id))
.Select(t => _orchardServices.ContentManager.Get(t.TagsPartRecord.Id, options))
.Where(c => c != null);
}
@@ -121,6 +125,27 @@ namespace Orchard.Tags.Services {
_contentTagRepository.Create(tagsContentItems);
}
public void RemoveTagsForContentItem(ContentItem contentItem) {
if (contentItem.Id == 0)
throw new OrchardException(T("Error removing tag to content item: the content item has not been created yet."));
_contentTagRepository.Flush();
var tagsPart = 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 (_contentTagRepository.Count(x => x.TagsPartRecord.Id != contentItem.Id) == 0) {
_tagRepository.Delete(tag);
}
}
// delete tag links with this contentItem (ContentTagRecords)
foreach (var record in _contentTagRepository.Fetch(x => x.TagsPartRecord.Id == contentItem.Id)) {
_contentTagRepository.Delete(record);
}
}
public void UpdateTagsForContentItem(ContentItem contentItem, IEnumerable<string> tagNamesForContentItem) {
if (contentItem.Id == 0)
throw new OrchardException(T("Error adding tag to content item: the content item has not been created yet."));