#20959: Refactoring the Taxonomy service

Work Item: 20959
This commit is contained in:
Sebastien Ros
2014-09-25 11:34:19 -07:00
parent ae30267df0
commit 9f84c29e39
4 changed files with 75 additions and 67 deletions

View File

@@ -148,11 +148,8 @@ namespace Orchard.Taxonomies.Controllers {
var taxonomy = _taxonomyService.GetTaxonomy(taxonomyId);
var parentTerm = _taxonomyService.GetTerm(parentTermId);
var term = _taxonomyService.NewTerm(taxonomy);
// assign a container to show the full route while editing
term.Container = parentTerm == null ? taxonomy : (IContent)parentTerm;
var term = _taxonomyService.NewTerm(taxonomy, parentTerm);
var model = Services.ContentManager.BuildEditor(term);
return View(model);
}
@@ -164,8 +161,7 @@ namespace Orchard.Taxonomies.Controllers {
var taxonomy = _taxonomyService.GetTaxonomy(taxonomyId);
var parentTerm = _taxonomyService.GetTerm(parentTermId);
var term = _taxonomyService.NewTerm(taxonomy);
term.Container = parentTerm == null ? taxonomy.ContentItem : parentTerm.ContentItem;
var term = _taxonomyService.NewTerm(taxonomy, parentTerm);
// Create content item before updating so attached fields save correctly
Services.ContentManager.Create(term, VersionOptions.Draft);
@@ -177,7 +173,6 @@ namespace Orchard.Taxonomies.Controllers {
return View(model);
}
_taxonomyService.ProcessPath(term);
Services.ContentManager.Publish(term.ContentItem);
Services.Notifier.Information(T("The {0} term has been created.", term.Name));

View File

@@ -140,11 +140,9 @@ namespace Orchard.Taxonomies.Drivers {
var taxonomy = _taxonomyService.GetTaxonomy(taxonomyId);
term = _taxonomyService.NewTerm(taxonomy);
term.Container = taxonomy.ContentItem;
term.Name = entry.Name.Trim();
term.Selectable = true;
_taxonomyService.ProcessPath(term);
Services.ContentManager.Create(term, VersionOptions.Published);
Services.Notifier.Information(T("The {0} term has been created.", term.Name));
}

View File

@@ -5,25 +5,65 @@ using Orchard.Taxonomies.Models;
namespace Orchard.Taxonomies.Services {
public interface ITaxonomyService : IDependency {
/// <summary>
/// Returns all the <see cref="TaxonomyPart" /> content items.
/// </summary>
/// <returns>The <see cref="TaxonomyPart"/> content items.</returns>
IEnumerable<TaxonomyPart> GetTaxonomies();
/// <summary>
/// Loads the published version of <see cref="TaxonomyPart"/> content item by its id.
/// </summary>
/// <param name="id">The id of the <see cref="TaxonomyPart"/> to load.</param>
/// <returns>The <see cref="TaxonomyPart"/> with the specified id or <value>null</value> if no published version of this id exists.</returns>
TaxonomyPart GetTaxonomy(int id);
/// <summary>
/// Loads the published version of <see cref="TaxonomyPart"/> content item by its name.
/// </summary>
/// <param name="name">The name of the <see cref="TaxonomyPart"/> to load.</param>
/// <returns>The <see cref="TaxonomyPart"/> with the specified id or <value>null</value> if no published version of this name exists.</returns>
TaxonomyPart GetTaxonomyByName(string name);
TaxonomyPart GetTaxonomyBySlug(string slug);
/// <summary>
/// Creates a new Content Type for the terms of a <see cref="TaxonomyPart"/>
/// </summary>
/// <param name="taxonomy">The taxonomy to create a term content type for.</param>
void CreateTermContentType(TaxonomyPart taxonomy);
/// <summary>
/// Deletes a <see cref="TaxonomyPart"/> content item from the database.
/// </summary>
/// <param name="taxonomy">The taxonomy to delete.</param>
/// <remarks>It will also remove all its terms and delete their content type.</remarks>
void DeleteTaxonomy(TaxonomyPart taxonomy);
IEnumerable<TermPart> GetAllTerms();
IEnumerable<TermPart> GetTerms(int taxonomyId);
TermPart GetTerm(int id);
TermPart GetTermByPath(string path);
TermPart GetTermByName(int taxonomyId, string name);
void DeleteTerm(TermPart termPart);
void MoveTerm(TaxonomyPart taxonomy, TermPart term, TermPart parentTerm);
void ProcessPath(TermPart term);
IEnumerable<string> GetTermPaths();
string GenerateTermTypeName(string taxonomyName);
/// <summary>
/// Creates a new <see cref="TermPart"/> content item in memory.
/// </summary>
/// <remarks>You still need to assign the Name propery and create the content item in the database.</remarks>
/// <param name="taxonomy">The <see cref="TaxonomyPart"/> content item the new term is associated to.</param>
/// <returns>A new instance of <see cref="TermPart"/></returns>
TermPart NewTerm(TaxonomyPart taxonomy);
/// <summary>
/// Creates a new <see cref="TermPart"/> content item in memory.
/// </summary>
/// <remarks>You still need to assign the Name propery and create the content item in the database.</remarks>
/// <param name="taxonomy">The <see cref="TaxonomyPart"/> content item the new term is associated to.</param>
/// <param name="parent">The <see cref="IContent"/> instance this term is a child of. This can be a <see cref="TermPart"/> of the same taxonomy or the taxonomy itself.</param>
/// <returns>A new instance of <see cref="TermPart"/></returns>
TermPart NewTerm(TaxonomyPart taxonomy, IContent parent);
IEnumerable<TermPart> GetTermsForContentItem(int contentItemId, string field = null, VersionOptions versionOptions = null);
void UpdateTerms(ContentItem contentItem, IEnumerable<TermPart> terms, string field);
IEnumerable<TermPart> GetParents(TermPart term);
@@ -33,11 +73,6 @@ namespace Orchard.Taxonomies.Services {
long GetContentItemsCount(TermPart term, string fieldName = null);
IContentQuery<TermsPart, TermsPartRecord> GetContentItemsQuery(TermPart term, string fieldName = null);
/// <summary>
/// Returns all the slugs which can reach a taxonomy
/// </summary>
IEnumerable<string> GetSlugs();
/// <summary>
/// Organizes a list of <see cref="TermPart"/> objects into a hierarchy.
/// </summary>

View File

@@ -50,7 +50,7 @@ namespace Orchard.Taxonomies.Services {
}
public TaxonomyPart GetTaxonomy(int id) {
return _contentManager.Get(id, VersionOptions.Published, new QueryHints().ExpandParts<TaxonomyPart>()).As<TaxonomyPart>();
return _contentManager.Get(id, VersionOptions.Published).As<TaxonomyPart>();
}
public TaxonomyPart GetTaxonomyByName(string name) {
@@ -58,25 +58,12 @@ namespace Orchard.Taxonomies.Services {
throw new ArgumentNullException("name");
}
return _contentManager
.Query<TaxonomyPart>()
.Join<TitlePartRecord>()
.Where(r => r.Title == name)
.List()
.FirstOrDefault();
}
public TaxonomyPart GetTaxonomyBySlug(string slug) {
if (String.IsNullOrWhiteSpace(slug)) {
throw new ArgumentNullException("slug");
}
// include the record in the query to optimize the query plan
return _contentManager
.Query<TaxonomyPart, TaxonomyPartRecord>()
.Join<TitlePartRecord>()
.Join<AutoroutePartRecord>()
.Where(r => r.DisplayAlias == slug)
.List()
.Where(r => r.Title == name)
.Slice(1)
.FirstOrDefault();
}
@@ -122,8 +109,30 @@ namespace Orchard.Taxonomies.Services {
}
public TermPart NewTerm(TaxonomyPart taxonomy) {
return NewTerm(taxonomy, null);
}
public TermPart NewTerm(TaxonomyPart taxonomy, IContent parent) {
if (taxonomy == null) {
throw new ArgumentNullException("taxonomy");
}
if (parent != null) {
var parentAsTaxonomy = parent.As<TaxonomyPart>();
if (parentAsTaxonomy != null && parentAsTaxonomy != taxonomy) {
throw new ArgumentException("The parent of a term can't be a different taxonomy", "parent");
}
var parentAsTerm = parent.As<TermPart>();
if (parentAsTerm != null && parentAsTerm.TaxonomyId != taxonomy.Id) {
throw new ArgumentException("The parent of a term can't be a from a different taxonomy", "parent");
}
}
var term = _contentManager.New<TermPart>(taxonomy.TermTypeName);
term.Container = parent == null ? taxonomy : parent;
term.TaxonomyId = taxonomy.Id;
ProcessPath(term);
return term;
}
@@ -136,25 +145,10 @@ namespace Orchard.Taxonomies.Services {
return TermPart.Sort(result);
}
public TermPart GetTermByPath(string path) {
return _contentManager.Query<TermPart, TermPartRecord>()
.Join<AutoroutePartRecord>()
.Where(rr => rr.DisplayAlias == path)
.List()
.FirstOrDefault();
}
public IEnumerable<TermPart> GetAllTerms() {
var result = _contentManager
.Query<TermPart, TermPartRecord>()
.List();
return TermPart.Sort(result);
}
public TermPart GetTerm(int id) {
return _contentManager
.Query<TermPart, TermPartRecord>()
.Where(x => x.Id == id).List().FirstOrDefault();
.Where(x => x.Id == id).Slice(1).FirstOrDefault();
}
public IEnumerable<TermPart> GetTermsForContentItem(int contentItemId, string field = null, VersionOptions versionOptions = null) {
@@ -171,7 +165,7 @@ namespace Orchard.Taxonomies.Services {
.Where(t => t.TaxonomyId == taxonomyId)
.Join<TitlePartRecord>()
.Where(r => r.Title == name)
.List()
.Slice(1)
.FirstOrDefault();
}
@@ -282,20 +276,6 @@ namespace Orchard.Taxonomies.Services {
return term.Path.Split(new [] {'/'}, StringSplitOptions.RemoveEmptyEntries).Select(id => GetTerm(int.Parse(id)));
}
public IEnumerable<string> GetSlugs() {
return _contentManager
.Query<TaxonomyPart, TaxonomyPartRecord>()
.List()
.Select(t => t.Slug);
}
public IEnumerable<string> GetTermPaths() {
return _contentManager
.Query<TermPart, TermPartRecord>()
.List()
.Select(t => t.Slug);
}
public void MoveTerm(TaxonomyPart taxonomy, TermPart term, TermPart parentTerm) {
var children = GetChildren(term);
term.Container = parentTerm == null ? taxonomy.ContentItem : parentTerm.ContentItem;