From be9646daa1d47db2749f6d0bcb9bc3ef5860ca7e Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Thu, 25 Nov 2010 18:25:18 -0800 Subject: [PATCH] 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 --- .../Tags/Services/TagsServiceTests.cs | 21 +++++++++++++ .../Controllers/AdminController.cs | 2 +- .../Orchard.Tags/Handlers/TagsPartHandler.cs | 30 ++++--------------- .../Orchard.Tags/Services/ITagService.cs | 2 ++ .../Orchard.Tags/Services/TagService.cs | 27 ++++++++++++++++- 5 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/Orchard.Tests.Modules/Tags/Services/TagsServiceTests.cs b/src/Orchard.Tests.Modules/Tags/Services/TagsServiceTests.cs index 575ebab14..dcc150a81 100644 --- a/src/Orchard.Tests.Modules/Tags/Services/TagsServiceTests.cs +++ b/src/Orchard.Tests.Modules/Tags/Services/TagsServiceTests.cs @@ -96,6 +96,27 @@ namespace Orchard.Tests.Modules.Tags.Services { Assert.That(thing2.As().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"); diff --git a/src/Orchard.Web/Modules/Orchard.Tags/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Tags/Controllers/AdminController.cs index 5d8aa12bc..7c8a31c11 100644 --- a/src/Orchard.Web/Modules/Orchard.Tags/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Tags/Controllers/AdminController.cs @@ -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); } diff --git a/src/Orchard.Web/Modules/Orchard.Tags/Handlers/TagsPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Tags/Handlers/TagsPartHandler.cs index 39ea8c325..c91ad057d 100644 --- a/src/Orchard.Web/Modules/Orchard.Tags/Handlers/TagsPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Tags/Handlers/TagsPartHandler.cs @@ -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 repository, IRepository tagsRepository, IRepository tagsContentItemsRepository) { + public TagsPartHandler(IRepository repository, ITagService tagService) { Filters.Add(StorageFilter.For(repository)); - OnRemoved((context, tags) => { - tagsContentItemsRepository.Flush(); + OnRemoved((context, tags) => + tagService.RemoveTagsForContentItem(context.ContentItem)); - var tagsPart = context.ContentItem.As(); - - // 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((context, tagsPart) => context.DocumentIndex - .Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze()); + OnIndexing((context, tagsPart) => + context.DocumentIndex.Add("tags", String.Join(", ", tagsPart.CurrentTags.Select(t => t.TagName))).Analyze()); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Tags/Services/ITagService.cs b/src/Orchard.Web/Modules/Orchard.Tags/Services/ITagService.cs index 25b2e8502..5aa2e455d 100644 --- a/src/Orchard.Web/Modules/Orchard.Tags/Services/ITagService.cs +++ b/src/Orchard.Web/Modules/Orchard.Tags/Services/ITagService.cs @@ -8,6 +8,7 @@ namespace Orchard.Tags.Services { TagRecord GetTag(int tagId); TagRecord GetTagByName(string tagName); IEnumerable GetTaggedContentItems(int tagId); + IEnumerable 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 tagNamesForContentItem); + void RemoveTagsForContentItem(ContentItem contentItem); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs b/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs index f1595fb61..b2f2c724c 100644 --- a/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs +++ b/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs @@ -109,9 +109,13 @@ namespace Orchard.Tags.Services { } public IEnumerable GetTaggedContentItems(int tagId) { + return GetTaggedContentItems(tagId, VersionOptions.Published); + } + + public IEnumerable 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(); + + // 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 tagNamesForContentItem) { if (contentItem.Id == 0) throw new OrchardException(T("Error adding tag to content item: the content item has not been created yet."));