Implements MediaService for U4-941
Minor update to the ContentType/MediaType repositories to insure that CT references are removed when deleting. Updates Media- and ContentMappers to map ContentTypeId. Updates Media to have internal Trash-method.
This commit is contained in:
@@ -77,6 +77,26 @@ namespace Umbraco.Core.Models
|
||||
ChangeContentType(contentType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the Trashed state of the content object
|
||||
/// </summary>
|
||||
/// <param name="isTrashed">Boolean indicating whether content is trashed (true) or not trashed (false)</param>
|
||||
/// <param name="parentId"> </param>
|
||||
internal void ChangeTrashedState(bool isTrashed, int parentId = -1)
|
||||
{
|
||||
Trashed = isTrashed;
|
||||
|
||||
//If Content is trashed the parent id should be set to that of the RecycleBin
|
||||
if (isTrashed)
|
||||
{
|
||||
ParentId = -20;
|
||||
}
|
||||
else//otherwise set the parent id to the optional parameter, -1 being the fallback
|
||||
{
|
||||
ParentId = parentId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method to call when Entity is being saved
|
||||
/// </summary>
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Umbraco.Core.Models.Rdbms
|
||||
public int NodeId { get; set; }
|
||||
|
||||
[Column("contentType")]
|
||||
public int ContentType { get; set; }
|
||||
public int ContentTypeId { get; set; }
|
||||
|
||||
[ResultColumn]
|
||||
public NodeDto NodeDto { get; set; }
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Umbraco.Core.Models.Rdbms
|
||||
internal class ContentType2ContentTypeDto
|
||||
{
|
||||
[Column("parentContentTypeId")]
|
||||
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentType2ContentType", OnColumns = "[parentContentTypeId], [childContentTypeId]")]
|
||||
[PrimaryKeyColumn(AutoIncrement = false, Clustered = true, Name = "PK_cmsContentType2ContentType", OnColumns = "parentContentTypeId, childContentTypeId")]
|
||||
public int ParentId { get; set; }
|
||||
|
||||
[Column("childContentTypeId")]
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
var contentDto = new ContentDto
|
||||
{
|
||||
NodeId = entity.Id,
|
||||
ContentType = entity.ContentTypeId,
|
||||
ContentTypeId = entity.ContentTypeId,
|
||||
NodeDto = BuildNodeDto(entity)
|
||||
};
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
var contentDto = new ContentDto
|
||||
{
|
||||
NodeId = entity.Id,
|
||||
ContentType = entity.ContentTypeId,
|
||||
ContentTypeId = entity.ContentTypeId,
|
||||
NodeDto = BuildNodeDto(entity)
|
||||
};
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace Umbraco.Core.Persistence.Mappers
|
||||
CacheMap<Content, NodeDto>(src => src.Trashed, dto => dto.Trashed);
|
||||
CacheMap<Content, NodeDto>(src => src.Key, dto => dto.UniqueId);
|
||||
CacheMap<Content, NodeDto>(src => src.UserId, dto => dto.UserId);
|
||||
CacheMap<Content, ContentDto>(src => src.ContentTypeId, dto => dto.ContentTypeId);
|
||||
CacheMap<Content, ContentVersionDto>(src => src.UpdateDate, dto => dto.VersionDate);
|
||||
CacheMap<Content, ContentVersionDto>(src => src.Version, dto => dto.VersionId);
|
||||
CacheMap<Content, DocumentDto>(src => src.Name, dto => dto.Text);
|
||||
|
||||
@@ -36,6 +36,7 @@ namespace Umbraco.Core.Persistence.Mappers
|
||||
CacheMap<Models.Media, NodeDto>(src => src.Trashed, dto => dto.Trashed);
|
||||
CacheMap<Models.Media, NodeDto>(src => src.Key, dto => dto.UniqueId);
|
||||
CacheMap<Models.Media, NodeDto>(src => src.UserId, dto => dto.UserId);
|
||||
CacheMap<Models.Media, ContentDto>(src => src.ContentTypeId, dto => dto.ContentTypeId);
|
||||
CacheMap<Models.Media, ContentVersionDto>(src => src.UpdateDate, dto => dto.VersionDate);
|
||||
CacheMap<Models.Media, ContentVersionDto>(src => src.Version, dto => dto.VersionId);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
if (dto == null)
|
||||
return null;
|
||||
|
||||
var contentType = _contentTypeRepository.Get(dto.ContentVersionDto.ContentDto.ContentType);
|
||||
var contentType = _contentTypeRepository.Get(dto.ContentVersionDto.ContentDto.ContentTypeId);
|
||||
|
||||
var factory = new ContentFactory(contentType, NodeObjectTypeId, id);
|
||||
var content = factory.BuildEntity(dto);
|
||||
@@ -211,7 +211,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var o = Database.Update(nodeDto);
|
||||
|
||||
//Only update this DTO if the contentType has actually changed
|
||||
if (contentDto.ContentType != entity.ContentTypeId)
|
||||
if (contentDto.ContentTypeId != entity.ContentTypeId)
|
||||
{
|
||||
//Create the Content specific data - cmsContent
|
||||
var newContentDto = dto.ContentVersionDto.ContentDto;
|
||||
@@ -277,7 +277,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
if (dto == null)
|
||||
return null;
|
||||
|
||||
var contentType = _contentTypeRepository.Get(dto.ContentVersionDto.ContentDto.ContentType);
|
||||
var contentType = _contentTypeRepository.Get(dto.ContentVersionDto.ContentDto.ContentTypeId);
|
||||
|
||||
var factory = new ContentFactory(contentType, NodeObjectTypeId, id);
|
||||
var content = factory.BuildEntity(dto);
|
||||
|
||||
@@ -120,6 +120,8 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
string.Format("DELETE FROM umbracoUser2NodePermission WHERE nodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsTagRelationship WHERE nodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsContentTypeAllowedContentType WHERE Id = @Id"),
|
||||
string.Format("DELETE FROM cmsContentType2ContentType WHERE parentContentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsContentType2ContentType WHERE childContentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsPropertyType WHERE contentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsPropertyTypeGroup WHERE contenttypeNodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsDocumentType WHERE contentTypeNodeId = @Id"),
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
if (dto == null)
|
||||
return null;
|
||||
|
||||
var contentType = _mediaTypeRepository.Get(dto.ContentDto.ContentType);
|
||||
var contentType = _mediaTypeRepository.Get(dto.ContentDto.ContentTypeId);
|
||||
|
||||
var factory = new MediaFactory(contentType, NodeObjectTypeId, id);
|
||||
var content = factory.BuildEntity(dto);
|
||||
@@ -201,7 +201,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var o = Database.Update(nodeDto);
|
||||
|
||||
//Only update this DTO if the contentType has actually changed
|
||||
if (contentDto.ContentType != entity.ContentTypeId)
|
||||
if (contentDto.ContentTypeId != entity.ContentTypeId)
|
||||
{
|
||||
//Create the Content specific data - cmsContent
|
||||
var newContentDto = dto.ContentDto;
|
||||
|
||||
@@ -115,6 +115,8 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
string.Format("DELETE FROM umbracoUser2NodePermission WHERE nodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsTagRelationship WHERE nodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsContentTypeAllowedContentType WHERE Id = @Id"),
|
||||
string.Format("DELETE FROM cmsContentType2ContentType WHERE parentContentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsContentType2ContentType WHERE childContentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsPropertyType WHERE contentTypeId = @Id"),
|
||||
string.Format("DELETE FROM cmsPropertyTypeGroup WHERE contenttypeNodeId = @Id"),
|
||||
string.Format("DELETE FROM cmsContentType WHERE NodeId = @Id"),
|
||||
|
||||
@@ -11,6 +11,9 @@ using Content = Umbraco.Core.Models.Content;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Content Service, which is an easy access to operations involving <see cref="IContent"/>
|
||||
/// </summary>
|
||||
public class ContentService : IContentService
|
||||
{
|
||||
private readonly IUnitOfWorkProvider _provider;
|
||||
@@ -68,6 +71,22 @@ namespace Umbraco.Web.Services
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IContentType"/></param>
|
||||
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
||||
public IEnumerable<IContent> GetContentOfContentType(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
|
||||
var query = Query<IContent>.Builder.Where(x => x.ContentTypeId == id);
|
||||
var contents = repository.GetByQuery(query);
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by Level
|
||||
/// </summary>
|
||||
@@ -369,7 +388,9 @@ namespace Umbraco.Web.Services
|
||||
/// <param name="userId">Id of the User deleting the Content</param>
|
||||
public void Delete(IContent content, int userId)
|
||||
{
|
||||
//TODO Ensure that content is unpublished when deleted
|
||||
//TODO This method should handle/react to errors when there is a constraint issue with the content being deleted
|
||||
//TODO Children should either be deleted or moved to the recycle bin
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
repository.Delete(content);
|
||||
@@ -384,6 +405,8 @@ namespace Umbraco.Web.Services
|
||||
/// <param name="userId">Id of the User deleting the Content</param>
|
||||
public void MoveToRecycleBin(IContent content, int userId)
|
||||
{
|
||||
//TODO If content item has children those should also be moved to the recycle bin
|
||||
//TODO Unpublish deleted content + children
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
((Content)content).ChangeTrashedState(true);
|
||||
@@ -403,6 +426,24 @@ namespace Umbraco.Web.Services
|
||||
SaveAndPublish(content, userId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Empties the Recycle Bin by deleting all <see cref="IContent"/> that resides in the bin
|
||||
/// </summary>
|
||||
public void EmptyRecycleBin()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
|
||||
|
||||
var query = Query<IContent>.Builder.Where(x => x.ParentId == -20);
|
||||
var contents = repository.GetByQuery(query);
|
||||
|
||||
foreach (var content in contents)
|
||||
{
|
||||
repository.Delete(content);
|
||||
}
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies an <see cref="IContent"/> object by creating a new Content object of the same type and copies all data from the current
|
||||
/// to the new copy which is returned.
|
||||
|
||||
@@ -8,17 +8,23 @@ using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the ContentType Service, which is an easy access to operations involving <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
public class ContentTypeService : IContentTypeService
|
||||
{
|
||||
private readonly IUnitOfWorkProvider _provider;
|
||||
private readonly IContentService _contentService;
|
||||
private readonly IMediaService _mediaService;
|
||||
|
||||
public ContentTypeService(IContentService contentService) : this(contentService, new PetaPocoUnitOfWorkProvider())
|
||||
public ContentTypeService(IContentService contentService, IMediaService mediaService)
|
||||
: this(contentService, mediaService, new PetaPocoUnitOfWorkProvider())
|
||||
{}
|
||||
|
||||
public ContentTypeService(IContentService contentService, IUnitOfWorkProvider provider)
|
||||
public ContentTypeService(IContentService contentService, IMediaService mediaService, IUnitOfWorkProvider provider)
|
||||
{
|
||||
_contentService = contentService;
|
||||
_mediaService = mediaService;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
@@ -236,8 +242,7 @@ namespace Umbraco.Web.Services
|
||||
/// <remarks>Deleting a <see cref="IMediaType"/> will delete all the <see cref="IMedia"/> objects based on this <see cref="IMediaType"/></remarks>
|
||||
public void Delete(IMediaType mediaType)
|
||||
{
|
||||
//TODO
|
||||
//_mediaService.DeleteMediaOfType(mediaType.Id);
|
||||
_mediaService.DeleteMediaOfType(mediaType.Id);
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaTypeRepository, IMediaType, int>(unitOfWork);
|
||||
|
||||
@@ -252,19 +257,16 @@ namespace Umbraco.Web.Services
|
||||
/// <remarks>Deleting a <see cref="IMediaType"/> will delete all the <see cref="IMedia"/> objects based on this <see cref="IMediaType"/></remarks>
|
||||
public void Delete(IEnumerable<IMediaType> mediaTypes)
|
||||
{
|
||||
//TODO
|
||||
/*
|
||||
var mediaTypeList = mediaTypes.ToList();
|
||||
foreach (var mediaType in mediaTypeList)
|
||||
{
|
||||
_mediaService.DeleteMediaOfType(mediaType.Id);
|
||||
}
|
||||
*/
|
||||
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaTypeRepository, IMediaType, int>(unitOfWork);
|
||||
|
||||
foreach (var mediaType in mediaTypes)
|
||||
foreach (var mediaType in mediaTypeList)
|
||||
{
|
||||
repository.Delete(mediaType);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,13 @@ namespace Umbraco.Web.Services
|
||||
/// <returns><see cref="IContent"/></returns>
|
||||
IContent GetById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IContentType"/></param>
|
||||
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
||||
IEnumerable<IContent> GetContentOfContentType(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IContent"/> objects by Level
|
||||
/// </summary>
|
||||
@@ -158,6 +165,11 @@ namespace Umbraco.Web.Services
|
||||
/// <param name="userId">Id of the User moving the Content</param>
|
||||
void Move(IContent content, int parentId, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Empties the Recycle Bin by deleting all <see cref="IContent"/> that resides in the bin
|
||||
/// </summary>
|
||||
void EmptyRecycleBin();
|
||||
|
||||
/// <summary>
|
||||
/// Copies an <see cref="IContent"/> object by creating a new Content object of the same type and copies all data from the current
|
||||
/// to the new copy which is returned.
|
||||
|
||||
103
src/Umbraco.Web/Services/IMediaService.cs
Normal file
103
src/Umbraco.Web/Services/IMediaService.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the MediaService, which is an easy access to operations involving <see cref="IMedia"/>
|
||||
/// </summary>
|
||||
public interface IMediaService : IService
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IMedia"/> object by Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Content to retrieve</param>
|
||||
/// <returns><see cref="IMedia"/></returns>
|
||||
IMedia GetById(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects by Parent Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Parent to retrieve Children from</param>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
IEnumerable<IMedia> GetChildren(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets descendants of a <see cref="IMedia"/> object by its Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Parent to retrieve descendants from</param>
|
||||
/// <returns>An Enumerable flat list of <see cref="IMedia"/> objects</returns>
|
||||
IEnumerable<IMedia> GetDescendants(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IMediaType"/></param>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
IEnumerable<IMedia> GetMediaOfMediaType(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects, which reside at the first level / root
|
||||
/// </summary>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
IEnumerable<IMedia> GetRootMedia();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of an <see cref="IMedia"/> objects, which resides in the Recycle Bin
|
||||
/// </summary>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
IEnumerable<IMedia> GetMediaInRecycleBin();
|
||||
|
||||
/// <summary>
|
||||
/// Moves an <see cref="IMedia"/> object to a new location
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to move</param>
|
||||
/// <param name="parentId">Id of the Media's new Parent</param>
|
||||
/// <param name="userId">Id of the User moving the Media</param>
|
||||
void Move(IMedia media, int parentId, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an <see cref="IMedia"/> object by moving it to the Recycle Bin
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to delete</param>
|
||||
/// <param name="userId">Id of the User deleting the Media</param>
|
||||
void MoveToRecycleBin(IMedia media, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Empties the Recycle Bin by deleting all <see cref="IMedia"/> that resides in the bin
|
||||
/// </summary>
|
||||
void EmptyRecycleBin();
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin.
|
||||
/// </summary>
|
||||
/// <remarks>This needs extra care and attention as its potentially a dangerous and extensive operation</remarks>
|
||||
/// <param name="mediaTypeId">Id of the <see cref="IMediaType"/></param>
|
||||
void DeleteMediaOfType(int mediaTypeId);
|
||||
|
||||
/// <summary>
|
||||
/// Permanently deletes an <see cref="IMedia"/> object
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Please note that this method will completely remove the Media from the database,
|
||||
/// but current not from the file system.
|
||||
/// </remarks>
|
||||
/// <param name="media">The <see cref="IMedia"/> to delete</param>
|
||||
/// <param name="userId">Id of the User deleting the Media</param>
|
||||
void Delete(IMedia media, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a single <see cref="IMedia"/> object
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to save</param>
|
||||
/// <param name="userId">Id of the User saving the Content</param>
|
||||
void Save(IMedia media, int userId);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a collection of <see cref="IMedia"/> objects
|
||||
/// </summary>
|
||||
/// <param name="medias">Collection of <see cref="IMedia"/> to save</param>
|
||||
/// <param name="userId">Id of the User saving the Content</param>
|
||||
void Save(IEnumerable<IMedia> medias, int userId);
|
||||
}
|
||||
}
|
||||
234
src/Umbraco.Web/Services/MediaService.cs
Normal file
234
src/Umbraco.Web/Services/MediaService.cs
Normal file
@@ -0,0 +1,234 @@
|
||||
using System.Collections.Generic;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Querying;
|
||||
using Umbraco.Core.Persistence.Repositories;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Web.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the Media Service, which is an easy access to operations involving <see cref="IMedia"/>
|
||||
/// </summary>
|
||||
public class MediaService : IMediaService
|
||||
{
|
||||
private readonly IUnitOfWorkProvider _provider;
|
||||
|
||||
public MediaService() : this(new PetaPocoUnitOfWorkProvider())
|
||||
{
|
||||
}
|
||||
|
||||
public MediaService(IUnitOfWorkProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IMedia"/> object by Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Content to retrieve</param>
|
||||
/// <returns><see cref="IMedia"/></returns>
|
||||
public IMedia GetById(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
return repository.Get(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects by Parent Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Parent to retrieve Children from</param>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
public IEnumerable<IMedia> GetChildren(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ParentId == id);
|
||||
var medias = repository.GetByQuery(query);
|
||||
|
||||
return medias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets descendants of a <see cref="IMedia"/> object by its Id
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the Parent to retrieve descendants from</param>
|
||||
/// <returns>An Enumerable flat list of <see cref="IMedia"/> objects</returns>
|
||||
public IEnumerable<IMedia> GetDescendants(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var media = repository.Get(id);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.Path.StartsWith(media.Path));
|
||||
var medias = repository.GetByQuery(query);
|
||||
|
||||
return medias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects by the Id of the <see cref="IContentType"/>
|
||||
/// </summary>
|
||||
/// <param name="id">Id of the <see cref="IMediaType"/></param>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
public IEnumerable<IMedia> GetMediaOfMediaType(int id)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ContentTypeId == id);
|
||||
var medias = repository.GetByQuery(query);
|
||||
|
||||
return medias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of <see cref="IMedia"/> objects, which reside at the first level / root
|
||||
/// </summary>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
public IEnumerable<IMedia> GetRootMedia()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -1);
|
||||
var medias = repository.GetByQuery(query);
|
||||
|
||||
return medias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of an <see cref="IMedia"/> objects, which resides in the Recycle Bin
|
||||
/// </summary>
|
||||
/// <returns>An Enumerable list of <see cref="IMedia"/> objects</returns>
|
||||
public IEnumerable<IMedia> GetMediaInRecycleBin()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -20);
|
||||
var medias = repository.GetByQuery(query);
|
||||
|
||||
return medias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves an <see cref="IMedia"/> object to a new location
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to move</param>
|
||||
/// <param name="parentId">Id of the Media's new Parent</param>
|
||||
/// <param name="userId">Id of the User moving the Media</param>
|
||||
public void Move(IMedia media, int parentId, int userId)
|
||||
{
|
||||
media.ParentId = parentId;
|
||||
Save(media, userId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an <see cref="IMedia"/> object by moving it to the Recycle Bin
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to delete</param>
|
||||
/// <param name="userId">Id of the User deleting the Media</param>
|
||||
public void MoveToRecycleBin(IMedia media, int userId)
|
||||
{
|
||||
//TODO If media item has children those should also be moved to the recycle bin as well
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
((Core.Models.Media)media).ChangeTrashedState(true);
|
||||
repository.AddOrUpdate(media);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Empties the Recycle Bin by deleting all <see cref="IMedia"/> that resides in the bin
|
||||
/// </summary>
|
||||
public void EmptyRecycleBin()
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ParentId == -20);
|
||||
var contents = repository.GetByQuery(query);
|
||||
|
||||
foreach (var content in contents)
|
||||
{
|
||||
repository.Delete(content);
|
||||
}
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin.
|
||||
/// </summary>
|
||||
/// <remarks>This needs extra care and attention as its potentially a dangerous and extensive operation</remarks>
|
||||
/// <param name="mediaTypeId">Id of the <see cref="IMediaType"/></param>
|
||||
public void DeleteMediaOfType(int mediaTypeId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
|
||||
//NOTE What about media that has the contenttype as part of its composition?
|
||||
//The ContentType has to be removed from the composition somehow as it would otherwise break
|
||||
//Dbl.check+test that the ContentType's Id is removed from the ContentType2ContentType table
|
||||
var query = Query<IMedia>.Builder.Where(x => x.ContentTypeId == mediaTypeId);
|
||||
var contents = repository.GetByQuery(query);
|
||||
|
||||
foreach (var content in contents)
|
||||
{
|
||||
((Core.Models.Media)content).ChangeTrashedState(true);
|
||||
repository.AddOrUpdate(content);
|
||||
}
|
||||
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Permanently deletes an <see cref="IMedia"/> object
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Please note that this method will completely remove the Media from the database,
|
||||
/// but current not from the file system.
|
||||
/// </remarks>
|
||||
/// <param name="media">The <see cref="IMedia"/> to delete</param>
|
||||
/// <param name="userId">Id of the User deleting the Media</param>
|
||||
public void Delete(IMedia media, int userId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
repository.Delete(media);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a single <see cref="IMedia"/> object
|
||||
/// </summary>
|
||||
/// <param name="media">The <see cref="IMedia"/> to save</param>
|
||||
/// <param name="userId">Id of the User saving the Content</param>
|
||||
public void Save(IMedia media, int userId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
repository.AddOrUpdate(media);
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a collection of <see cref="IMedia"/> objects
|
||||
/// </summary>
|
||||
/// <param name="medias">Collection of <see cref="IMedia"/> to save</param>
|
||||
/// <param name="userId">Id of the User saving the Content</param>
|
||||
public void Save(IEnumerable<IMedia> medias, int userId)
|
||||
{
|
||||
var unitOfWork = _provider.GetUnitOfWork();
|
||||
var repository = RepositoryResolver.ResolveByType<IMediaRepository, IMedia, int>(unitOfWork);
|
||||
foreach (var media in medias)
|
||||
{
|
||||
repository.AddOrUpdate(media);
|
||||
}
|
||||
unitOfWork.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,10 +18,16 @@ namespace Umbraco.Web
|
||||
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();
|
||||
return new ContentTypeService(contentService, new PetaPocoUnitOfWorkProvider());
|
||||
var mediaService = umbracoContext.MediaService();
|
||||
return new ContentTypeService(contentService, mediaService, new PetaPocoUnitOfWorkProvider());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,7 +317,9 @@
|
||||
<Compile Include="Services\ContentTypeService.cs" />
|
||||
<Compile Include="Services\IContentService.cs" />
|
||||
<Compile Include="Services\IContentTypeService.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">
|
||||
|
||||
Reference in New Issue
Block a user