Implements IDataTypeService for U4-938
Refactoring base ContentType Repository and its implementations to allow for queries against PropertyType - still needs testing though. Adding mappers for PropertyGroup and PropertyType models, so they are usable for queries.
This commit is contained in:
142
src/Umbraco.Web/Services/DataTypeService.cs
Normal file
142
src/Umbraco.Web/Services/DataTypeService.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Querying;
|
||||
using Umbraco.Core.Persistence.Repositories;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the DataType Service, which is an easy access to operations involving <see cref="IDataType"/> and <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
public class DataTypeService : IDataTypeService
|
||||
{
|
||||
private readonly IUnitOfWorkProvider _provider;
|
||||
|
||||
public DataTypeService() : this(new PetaPocoUnitOfWorkProvider())
|
||||
{
|
||||
}
|
||||
|
||||
public DataTypeService(IUnitOfWorkProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDataTypeDefinition"/> by its Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDataTypeDefinition"/></param>
|
||||
/// <returns><see cref="IDataTypeDefinition"/></returns>
|
||||
public IDataTypeDefinition GetDataTypeDefinitionById(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDataTypeDefinitionRepository, IDataTypeDefinition, int>(unitOfWork);
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDataTypeDefinition"/> by its control Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the DataType control</param>
|
||||
/// <returns><see cref="IDataTypeDefinition"/></returns>
|
||||
public IDataTypeDefinition GetDataTypeDefinitionById(Guid id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDataTypeDefinitionRepository, IDataTypeDefinition, int>(unitOfWork);
|
||||
|
||||
var query = Query<IDataTypeDefinition>.Builder.Where(x => x.ControlId == id);
|
||||
var definitions = repository.GetByQuery(query);
|
||||
|
||||
return definitions.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all <see cref="IDataTypeDefinition"/> objects or those with the ids passed in
|
||||
/// </summary>
|
||||
/// <param name="ids">Optional array of Ids</param>
|
||||
/// <returns>An enumerable list of <see cref="IDataTypeDefinition"/> objects</returns>
|
||||
public IEnumerable<IDataTypeDefinition> GetAllDataTypeDefinitions(params int[] ids)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDataTypeDefinitionRepository, IDataTypeDefinition, int>(unitOfWork);
|
||||
return repository.GetAll(ids);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves an <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
/// <param name="dataTypeDefinition"><see cref="IDataTypeDefinition"/> to save</param>
|
||||
/// <param name="userId">Id of the user issueing the save</param>
|
||||
public void Save(IDataTypeDefinition dataTypeDefinition, int userId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IDataTypeDefinitionRepository, IDataTypeDefinition, int>(unitOfWork);
|
||||
repository.AddOrUpdate(dataTypeDefinition);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Please note that deleting a <see cref="IDataTypeDefinition"/> will remove
|
||||
/// all the <see cref="PropertyType"/> data that references this <see cref="IDataTypeDefinition"/>.
|
||||
/// </remarks>
|
||||
/// <param name="dataTypeDefinition"><see cref="IDataTypeDefinition"/> to delete</param>
|
||||
/// <param name="userId">Id of the user issueing the deletion</param>
|
||||
public void Delete(IDataTypeDefinition dataTypeDefinition, int userId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
|
||||
//Find ContentTypes using this IDataTypeDefinition on a PropertyType
|
||||
var contentTypeRepository = RepositoryResolver.ResolveByType<IContentTypeRepository, IContentType, int>(unitOfWork);
|
||||
var query = Query<PropertyType>.Builder.Where(x => x.DataTypeId == dataTypeDefinition.Id);
|
||||
var contentTypes = contentTypeRepository.GetByQuery(query);
|
||||
|
||||
//Loop through the list of results and remove the PropertyTypes that references the DataTypeDefinition that is being deleted
|
||||
foreach (var contentType in contentTypes)
|
||||
{
|
||||
if(contentType == null) continue;
|
||||
|
||||
foreach (var group in contentType.PropertyGroups)
|
||||
{
|
||||
var types = group.PropertyTypes.Where(x => x.DataTypeId == dataTypeDefinition.Id);
|
||||
foreach (var propertyType in types)
|
||||
{
|
||||
group.PropertyTypes.Remove(propertyType);
|
||||
}
|
||||
}
|
||||
|
||||
contentTypeRepository.AddOrUpdate(contentType);
|
||||
}
|
||||
|
||||
var repository = RepositoryResolver.ResolveByType<IDataTypeDefinitionRepository, IDataTypeDefinition, int>(unitOfWork);
|
||||
repository.Delete(dataTypeDefinition);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the IDataType specified by it's unique ID
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the DataType, which corresponds to the Guid Id of the control</param>
|
||||
/// <returns><see cref="IDataType"/> object</returns>
|
||||
public IDataType GetDataTypeById(Guid id)
|
||||
{
|
||||
return DataTypesResolver.Current.GetById(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a complete list of all registered IDataType's
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="IDataType"/> objects</returns>
|
||||
public IEnumerable<IDataType> GetAllDataTypes()
|
||||
{
|
||||
return DataTypesResolver.Current.DataTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/Umbraco.Web/Services/IDataTypeService.cs
Normal file
65
src/Umbraco.Web/Services/IDataTypeService.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the DataType Service, which is an easy access to operations involving <see cref="IDataType"/> and <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
public interface IDataTypeService : IService
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDataTypeDefinition"/> by its Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IDataTypeDefinition"/></param>
|
||||
/// <returns><see cref="IDataTypeDefinition"/></returns>
|
||||
IDataTypeDefinition GetDataTypeDefinitionById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="IDataTypeDefinition"/> by its control Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the DataType control</param>
|
||||
/// <returns><see cref="IDataTypeDefinition"/></returns>
|
||||
IDataTypeDefinition GetDataTypeDefinitionById(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all <see cref="IDataTypeDefinition"/> objects or those with the ids passed in
|
||||
/// </summary>
|
||||
/// <param name="ids">Optional array of Ids</param>
|
||||
/// <returns>An enumerable list of <see cref="IDataTypeDefinition"/> objects</returns>
|
||||
IEnumerable<IDataTypeDefinition> GetAllDataTypeDefinitions(params int[] ids);
|
||||
|
||||
/// <summary>
|
||||
/// Saves an <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
/// <param name="dataTypeDefinition"><see cref="IDataTypeDefinition"/> to save</param>
|
||||
/// <param name="userId">Id of the user issueing the save</param>
|
||||
void Save(IDataTypeDefinition dataTypeDefinition, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an <see cref="IDataTypeDefinition"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Please note that deleting a <see cref="IDataTypeDefinition"/> will remove
|
||||
/// all the <see cref="PropertyType"/> data that references this <see cref="IDataTypeDefinition"/>.
|
||||
/// </remarks>
|
||||
/// <param name="dataTypeDefinition"><see cref="IDataTypeDefinition"/> to delete</param>
|
||||
/// <param name="userId">Id of the user issueing the deletion</param>
|
||||
void Delete(IDataTypeDefinition dataTypeDefinition, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the IDataType specified by it's unique ID
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the DataType, which corresponds to the Guid Id of the control</param>
|
||||
/// <returns><see cref="IDataType"/> object</returns>
|
||||
IDataType GetDataTypeById(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a complete list of all registered IDataType's
|
||||
/// </summary>
|
||||
/// <returns>An enumerable list of <see cref="IDataType"/> objects</returns>
|
||||
IEnumerable<IDataType> GetAllDataTypes();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
using Umbraco.Web.Publishing;
|
||||
using Umbraco.Web.Services;
|
||||
|
||||
namespace Umbraco.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Service specific extensions for <see cref="UmbracoContext"/>, which provides access to the various services
|
||||
/// </summary>
|
||||
public static class UmbracoContextExtensions
|
||||
{
|
||||
//TODO Add services to a dictionary, so we don't create a new instance each time a service is needed
|
||||
private static readonly ConcurrentDictionary<string, IService> ServiceCache = new ConcurrentDictionary<string, IService>();
|
||||
|
||||
public static IContentService ContentService(this UmbracoContext umbracoContext)
|
||||
{
|
||||
return new ContentService(new PetaPocoUnitOfWorkProvider(), new PublishingStrategy());
|
||||
}
|
||||
|
||||
public static IMediaService MediaService(this UmbracoContext umbracoContext)
|
||||
{
|
||||
return new MediaService(new PetaPocoUnitOfWorkProvider());
|
||||
}
|
||||
|
||||
public static IContentTypeService ContentTypeService(this UmbracoContext umbracoContext)
|
||||
{
|
||||
var contentService = umbracoContext.ContentService();
|
||||
var mediaService = umbracoContext.MediaService();
|
||||
return new ContentTypeService(contentService, mediaService, new PetaPocoUnitOfWorkProvider());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -315,12 +315,13 @@
|
||||
<Compile Include="Mvc\SurfaceControllerResolver.cs" />
|
||||
<Compile Include="Services\ContentService.cs" />
|
||||
<Compile Include="Services\ContentTypeService.cs" />
|
||||
<Compile Include="Services\DataTypeService.cs" />
|
||||
<Compile Include="Services\IContentService.cs" />
|
||||
<Compile Include="Services\IContentTypeService.cs" />
|
||||
<Compile Include="Services\IDataTypeService.cs" />
|
||||
<Compile Include="Services\IMediaService.cs" />
|
||||
<Compile Include="Services\IService.cs" />
|
||||
<Compile Include="Services\MediaService.cs" />
|
||||
<Compile Include="Services\UmbracoContextExtensions.cs" />
|
||||
<Compile Include="Templates\TemplateUtilities.cs" />
|
||||
<Compile Include="umbraco.presentation\Default.aspx.cs">
|
||||
<SubType>ASPXCodeBehind</SubType>
|
||||
|
||||
Reference in New Issue
Block a user