Finishes macro repository + tests.

This commit is contained in:
Shannon
2013-09-17 17:54:09 +10:00
parent f2f8004599
commit 89f2c0e38f
9 changed files with 661 additions and 265 deletions

View File

@@ -152,6 +152,17 @@ namespace Umbraco.Core.Models
_removedProperties.Add(alias);
}
}
internal override void ResetDirtyProperties(bool rememberPreviouslyChangedProperties)
{
_addedProperties.Clear();
_removedProperties.Clear();
base.ResetDirtyProperties(rememberPreviouslyChangedProperties);
foreach (var prop in Properties)
{
((TracksChangesEntityBase)prop).ResetDirtyProperties(rememberPreviouslyChangedProperties);
}
}
/// <summary>
/// Used internally to check if we need to add a section in the repository to the db

View File

@@ -12,6 +12,19 @@ namespace Umbraco.Core.Models
[DataContract(IsReference = true)]
internal class MacroProperty : TracksChangesEntityBase, IMacroProperty, IRememberBeingDirty
{
public MacroProperty()
{
}
public MacroProperty(string @alias, string name, int sortOrder, IMacroPropertyType propertyType)
{
_alias = alias;
_name = name;
_sortOrder = sortOrder;
_propertyType = propertyType;
}
private string _alias;
private string _name;
private int _sortOrder;

View File

@@ -12,6 +12,7 @@ namespace Umbraco.Core.Models.Rdbms
[PrimaryKeyColumn]
public int Id { get; set; }
//NOTE: This column is not used, we always show the properties
[Column("macroPropertyHidden")]
[Constraint(Default = "0")]
public bool Hidden { get; set; }

View File

@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;
@@ -11,6 +12,11 @@ namespace Umbraco.Core.Persistence.Factories
public IMacro BuildEntity(MacroDto dto)
{
var model = new Macro(dto.Id, dto.UseInEditor, dto.RefreshRate, dto.Alias, dto.Name, dto.ScriptType, dto.ScriptAssembly, dto.Xslt, dto.CacheByPage, dto.CachePersonalized, dto.DontRender, dto.Python);
foreach (var p in dto.MacroPropertyDtos)
{
model.Properties.Add(new MacroProperty(p.Alias, p.Name, p.SortOrder, null));
}
//on initial construction we don't want to have dirty properties tracked
// http://issues.umbraco.org/issue/U4-1946
model.ResetDirtyProperties(false);
@@ -31,8 +37,10 @@ namespace Umbraco.Core.Persistence.Factories
ScriptAssembly = entity.ControlAssembly,
ScriptType = entity.ControlType,
UseInEditor = entity.UseInEditor,
Xslt = entity.XsltPath
Xslt = entity.XsltPath,
MacroPropertyDtos = BuildPropertyDtos(entity)
};
if (entity.HasIdentity)
dto.Id = short.Parse(entity.Id.ToString(CultureInfo.InvariantCulture));
@@ -40,5 +48,25 @@ namespace Umbraco.Core.Persistence.Factories
}
#endregion
private List<MacroPropertyDto> BuildPropertyDtos(IMacro entity)
{
var list = new List<MacroPropertyDto>();
foreach (var p in entity.Properties)
{
var text = new MacroPropertyDto
{
Alias = p.Alias,
Name = p.Name,
Macro = entity.Id,
SortOrder = (byte)p.SortOrder,
//TODO: WE need to change this column to store an alias!!!! for now I'm just hard coding this until we go to that part.
Type = 16
};
list.Add(text);
}
return list;
}
}
}

View File

@@ -0,0 +1,54 @@
using System.Collections.Generic;
using Umbraco.Core.Models;
namespace Umbraco.Core.Services
{
/// <summary>
/// Defines the ContentService, which is an easy access to operations involving <see cref="IMacro"/>
/// </summary>
internal interface IMacroService : IService
{
//TODO Possibly create import macro method and ToXml?
/// <summary>
/// Gets an <see cref="IMacro"/> object by its alias
/// </summary>
/// <param name="alias">Alias to retrieve an <see cref="IMacro"/> for</param>
/// <returns>An <see cref="IMacro"/> object</returns>
IMacro GetByAlias(string alias);
/// <summary>
/// Gets a list all available <see cref="IMacro"/> objects
/// </summary>
/// <param name="aliases">Optional array of aliases to limit the results</param>
/// <returns>An enumerable list of <see cref="IMacro"/> objects</returns>
IEnumerable<IMacro> GetAll(params string[] aliases);
/// <summary>
/// Deletes an <see cref="IMacro"/>
/// </summary>
/// <param name="macro"><see cref="IMacro"/> to delete</param>
/// <param name="userId">Optional id of the user deleting the macro</param>
void Delete(IMacro macro, int userId = 0);
/// <summary>
/// Saves an <see cref="IMacro"/>
/// </summary>
/// <param name="macro"><see cref="IMacro"/> to save</param>
/// <param name="userId">Optional id of the user saving the macro</param>
void Save(IMacro macro, int userId = 0);
/// <summary>
/// Gets a list all available <see cref="IMacroPropertyType"/> plugins
/// </summary>
/// <returns>An enumerable list of <see cref="IMacroPropertyType"/> objects</returns>
IEnumerable<IMacroPropertyType> GetMacroPropertyTypes();
/// <summary>
/// Gets an <see cref="IMacroPropertyType"/> by its alias
/// </summary>
/// <param name="alias">Alias to retrieve an <see cref="IMacroPropertyType"/> for</param>
/// <returns>An <see cref="IMacroPropertyType"/> object</returns>
IMacroPropertyType GetMacroPropertyTypeByAlias(string alias);
}
}

View File

@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Auditing;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
{
/// <summary>
/// Represents the Macro Service, which is an easy access to operations involving <see cref="IMacro"/>
/// </summary>
internal class MacroService : IMacroService
{
private readonly RepositoryFactory _repositoryFactory;
private readonly IUnitOfWorkProvider _uowProvider;
public MacroService()
: this(new RepositoryFactory())
{
}
public MacroService(RepositoryFactory repositoryFactory)
: this(new FileUnitOfWorkProvider(), repositoryFactory)
{
}
public MacroService(IUnitOfWorkProvider provider)
: this(provider, new RepositoryFactory())
{
}
public MacroService(IUnitOfWorkProvider provider, RepositoryFactory repositoryFactory) : this(provider, repositoryFactory, false)
{
}
public MacroService(IUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, bool ensureCachedMacros)
{
_uowProvider = provider;
_repositoryFactory = repositoryFactory;
if(ensureCachedMacros)
EnsureMacroCache();
}
/// <summary>
/// Ensures the macro cache by getting all macros
/// from the repository and thus caching them.
/// </summary>
private void EnsureMacroCache()
{
IEnumerable<IMacro> macros = GetAll();
}
/// <summary>
/// Gets an <see cref="IMacro"/> object by its alias
/// </summary>
/// <param name="alias">Alias to retrieve an <see cref="IMacro"/> for</param>
/// <returns>An <see cref="IMacro"/> object</returns>
public IMacro GetByAlias(string alias)
{
using (var repository = _repositoryFactory.CreateMacroRepository(_uowProvider.GetUnitOfWork()))
{
return repository.Get(alias);
}
}
/// <summary>
/// Gets a list all available <see cref="IMacro"/> objects
/// </summary>
/// <param name="aliases">Optional array of aliases to limit the results</param>
/// <returns>An enumerable list of <see cref="IMacro"/> objects</returns>
public IEnumerable<IMacro> GetAll(params string[] aliases)
{
using (var repository = _repositoryFactory.CreateMacroRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetAll(aliases);
}
}
/// <summary>
/// Deletes an <see cref="IMacro"/>
/// </summary>
/// <param name="macro"><see cref="IMacro"/> to delete</param>
/// <param name="userId">Optional id of the user deleting the macro</param>
public void Delete(IMacro macro, int userId = 0)
{
if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs<IMacro>(macro), this))
return;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMacroRepository(uow))
{
repository.Delete(macro);
uow.Commit();
Deleted.RaiseEvent(new DeleteEventArgs<IMacro>(macro, false), this);
}
Audit.Add(AuditTypes.Delete, "Delete Macro performed by user", userId, -1);
}
/// <summary>
/// Saves an <see cref="IMacro"/>
/// </summary>
/// <param name="macro"><see cref="IMacro"/> to save</param>
/// <param name="userId">Optional Id of the user deleting the macro</param>
public void Save(IMacro macro, int userId = 0)
{
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IMacro>(macro), this))
return;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMacroRepository(uow))
{
repository.AddOrUpdate(macro);
uow.Commit();
Saved.RaiseEvent(new SaveEventArgs<IMacro>(macro, false), this);
}
Audit.Add(AuditTypes.Save, "Save Macro performed by user", userId, -1);
}
/// <summary>
/// Gets a list all available <see cref="IMacroPropertyType"/> plugins
/// </summary>
/// <returns>An enumerable list of <see cref="IMacroPropertyType"/> objects</returns>
public IEnumerable<IMacroPropertyType> GetMacroPropertyTypes()
{
return MacroPropertyTypeResolver.Current.MacroPropertyTypes;
}
/// <summary>
/// Gets an <see cref="IMacroPropertyType"/> by its alias
/// </summary>
/// <param name="alias">Alias to retrieve an <see cref="IMacroPropertyType"/> for</param>
/// <returns>An <see cref="IMacroPropertyType"/> object</returns>
public IMacroPropertyType GetMacroPropertyTypeByAlias(string alias)
{
return MacroPropertyTypeResolver.Current.MacroPropertyTypes.FirstOrDefault(x => x.Alias == alias);
}
#region Event Handlers
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<IMacroService, DeleteEventArgs<IMacro>> Deleting;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<IMacroService, DeleteEventArgs<IMacro>> Deleted;
/// <summary>
/// Occurs before Save
/// </summary>
public static event TypedEventHandler<IMacroService, SaveEventArgs<IMacro>> Saving;
/// <summary>
/// Occurs after Save
/// </summary>
public static event TypedEventHandler<IMacroService, SaveEventArgs<IMacro>> Saved;
#endregion
}
}