diff --git a/src/Umbraco.Core/Models/Media.cs b/src/Umbraco.Core/Models/Media.cs index fbad998829..44ffeecc89 100644 --- a/src/Umbraco.Core/Models/Media.cs +++ b/src/Umbraco.Core/Models/Media.cs @@ -77,6 +77,26 @@ namespace Umbraco.Core.Models ChangeContentType(contentType); } + /// + /// Changes the Trashed state of the content object + /// + /// Boolean indicating whether content is trashed (true) or not trashed (false) + /// + 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; + } + } + /// /// Method to call when Entity is being saved /// diff --git a/src/Umbraco.Core/Models/Rdbms/ContentDto.cs b/src/Umbraco.Core/Models/Rdbms/ContentDto.cs index adbfed3ab0..fde47f410e 100644 --- a/src/Umbraco.Core/Models/Rdbms/ContentDto.cs +++ b/src/Umbraco.Core/Models/Rdbms/ContentDto.cs @@ -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; } diff --git a/src/Umbraco.Core/Models/Rdbms/ContentType2ContentTypeDto.cs b/src/Umbraco.Core/Models/Rdbms/ContentType2ContentTypeDto.cs index c156003bfb..132181099a 100644 --- a/src/Umbraco.Core/Models/Rdbms/ContentType2ContentTypeDto.cs +++ b/src/Umbraco.Core/Models/Rdbms/ContentType2ContentTypeDto.cs @@ -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")] diff --git a/src/Umbraco.Core/Persistence/Factories/ContentFactory.cs b/src/Umbraco.Core/Persistence/Factories/ContentFactory.cs index 79d75c44ff..31f5617cce 100644 --- a/src/Umbraco.Core/Persistence/Factories/ContentFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/ContentFactory.cs @@ -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) }; diff --git a/src/Umbraco.Core/Persistence/Factories/MediaFactory.cs b/src/Umbraco.Core/Persistence/Factories/MediaFactory.cs index 901ff2ed30..b78013881c 100644 --- a/src/Umbraco.Core/Persistence/Factories/MediaFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/MediaFactory.cs @@ -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) }; diff --git a/src/Umbraco.Core/Persistence/Mappers/ContentMapper.cs b/src/Umbraco.Core/Persistence/Mappers/ContentMapper.cs index 3ad9f31f98..df0115312c 100644 --- a/src/Umbraco.Core/Persistence/Mappers/ContentMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/ContentMapper.cs @@ -37,6 +37,7 @@ namespace Umbraco.Core.Persistence.Mappers CacheMap(src => src.Trashed, dto => dto.Trashed); CacheMap(src => src.Key, dto => dto.UniqueId); CacheMap(src => src.UserId, dto => dto.UserId); + CacheMap(src => src.ContentTypeId, dto => dto.ContentTypeId); CacheMap(src => src.UpdateDate, dto => dto.VersionDate); CacheMap(src => src.Version, dto => dto.VersionId); CacheMap(src => src.Name, dto => dto.Text); diff --git a/src/Umbraco.Core/Persistence/Mappers/MediaMapper.cs b/src/Umbraco.Core/Persistence/Mappers/MediaMapper.cs index abd59ecab9..d2efd03034 100644 --- a/src/Umbraco.Core/Persistence/Mappers/MediaMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/MediaMapper.cs @@ -36,6 +36,7 @@ namespace Umbraco.Core.Persistence.Mappers CacheMap(src => src.Trashed, dto => dto.Trashed); CacheMap(src => src.Key, dto => dto.UniqueId); CacheMap(src => src.UserId, dto => dto.UserId); + CacheMap(src => src.ContentTypeId, dto => dto.ContentTypeId); CacheMap(src => src.UpdateDate, dto => dto.VersionDate); CacheMap(src => src.Version, dto => dto.VersionId); } diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs index 87eb953737..0957dc2a54 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs @@ -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); diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs index b16df2a4a3..e7afb1cd3a 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeRepository.cs @@ -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"), diff --git a/src/Umbraco.Core/Persistence/Repositories/MediaRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MediaRepository.cs index 4000e83c2d..17042d133a 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MediaRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MediaRepository.cs @@ -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; diff --git a/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs index 1f4de5dc09..ed5a167b26 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MediaTypeRepository.cs @@ -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"), diff --git a/src/Umbraco.Web/Services/ContentService.cs b/src/Umbraco.Web/Services/ContentService.cs index 47c3b30cdd..de725970ba 100644 --- a/src/Umbraco.Web/Services/ContentService.cs +++ b/src/Umbraco.Web/Services/ContentService.cs @@ -11,6 +11,9 @@ using Content = Umbraco.Core.Models.Content; namespace Umbraco.Web.Services { + /// + /// Represents the Content Service, which is an easy access to operations involving + /// public class ContentService : IContentService { private readonly IUnitOfWorkProvider _provider; @@ -68,6 +71,22 @@ namespace Umbraco.Web.Services return repository.Get(id); } + /// + /// Gets a collection of objects by the Id of the + /// + /// Id of the + /// An Enumerable list of objects + public IEnumerable GetContentOfContentType(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ContentTypeId == id); + var contents = repository.GetByQuery(query); + + return contents; + } + /// /// Gets a collection of objects by Level /// @@ -369,7 +388,9 @@ namespace Umbraco.Web.Services /// Id of the User deleting the Content 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(unitOfWork); repository.Delete(content); @@ -384,6 +405,8 @@ namespace Umbraco.Web.Services /// Id of the User deleting the Content 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(unitOfWork); ((Content)content).ChangeTrashedState(true); @@ -403,6 +426,24 @@ namespace Umbraco.Web.Services SaveAndPublish(content, userId); } + /// + /// Empties the Recycle Bin by deleting all that resides in the bin + /// + public void EmptyRecycleBin() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == -20); + var contents = repository.GetByQuery(query); + + foreach (var content in contents) + { + repository.Delete(content); + } + unitOfWork.Commit(); + } + /// /// Copies an 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. diff --git a/src/Umbraco.Web/Services/ContentTypeService.cs b/src/Umbraco.Web/Services/ContentTypeService.cs index 2d146dd1fe..dd05394100 100644 --- a/src/Umbraco.Web/Services/ContentTypeService.cs +++ b/src/Umbraco.Web/Services/ContentTypeService.cs @@ -8,17 +8,23 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Web.Services { + /// + /// Represents the ContentType Service, which is an easy access to operations involving + /// 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 /// Deleting a will delete all the objects based on this public void Delete(IMediaType mediaType) { - //TODO - //_mediaService.DeleteMediaOfType(mediaType.Id); + _mediaService.DeleteMediaOfType(mediaType.Id); var unitOfWork = _provider.GetUnitOfWork(); var repository = RepositoryResolver.ResolveByType(unitOfWork); @@ -252,19 +257,16 @@ namespace Umbraco.Web.Services /// Deleting a will delete all the objects based on this public void Delete(IEnumerable mediaTypes) { - //TODO - /* var mediaTypeList = mediaTypes.ToList(); foreach (var mediaType in mediaTypeList) { _mediaService.DeleteMediaOfType(mediaType.Id); } - */ var unitOfWork = _provider.GetUnitOfWork(); var repository = RepositoryResolver.ResolveByType(unitOfWork); - foreach (var mediaType in mediaTypes) + foreach (var mediaType in mediaTypeList) { repository.Delete(mediaType); } diff --git a/src/Umbraco.Web/Services/IContentService.cs b/src/Umbraco.Web/Services/IContentService.cs index 429af0fbb1..46f9b8b5bd 100644 --- a/src/Umbraco.Web/Services/IContentService.cs +++ b/src/Umbraco.Web/Services/IContentService.cs @@ -29,6 +29,13 @@ namespace Umbraco.Web.Services /// IContent GetById(int id); + /// + /// Gets a collection of objects by the Id of the + /// + /// Id of the + /// An Enumerable list of objects + IEnumerable GetContentOfContentType(int id); + /// /// Gets a collection of objects by Level /// @@ -158,6 +165,11 @@ namespace Umbraco.Web.Services /// Id of the User moving the Content void Move(IContent content, int parentId, int userId); + /// + /// Empties the Recycle Bin by deleting all that resides in the bin + /// + void EmptyRecycleBin(); + /// /// Copies an 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. diff --git a/src/Umbraco.Web/Services/IMediaService.cs b/src/Umbraco.Web/Services/IMediaService.cs new file mode 100644 index 0000000000..812d0bb4d8 --- /dev/null +++ b/src/Umbraco.Web/Services/IMediaService.cs @@ -0,0 +1,103 @@ +using System.Collections.Generic; +using Umbraco.Core.Models; + +namespace Umbraco.Web.Services +{ + /// + /// Defines the MediaService, which is an easy access to operations involving + /// + public interface IMediaService : IService + { + /// + /// Gets an object by Id + /// + /// Id of the Content to retrieve + /// + IMedia GetById(int id); + + /// + /// Gets a collection of objects by Parent Id + /// + /// Id of the Parent to retrieve Children from + /// An Enumerable list of objects + IEnumerable GetChildren(int id); + + /// + /// Gets descendants of a object by its Id + /// + /// Id of the Parent to retrieve descendants from + /// An Enumerable flat list of objects + IEnumerable GetDescendants(int id); + + /// + /// Gets a collection of objects by the Id of the + /// + /// Id of the + /// An Enumerable list of objects + IEnumerable GetMediaOfMediaType(int id); + + /// + /// Gets a collection of objects, which reside at the first level / root + /// + /// An Enumerable list of objects + IEnumerable GetRootMedia(); + + /// + /// Gets a collection of an objects, which resides in the Recycle Bin + /// + /// An Enumerable list of objects + IEnumerable GetMediaInRecycleBin(); + + /// + /// Moves an object to a new location + /// + /// The to move + /// Id of the Media's new Parent + /// Id of the User moving the Media + void Move(IMedia media, int parentId, int userId); + + /// + /// Deletes an object by moving it to the Recycle Bin + /// + /// The to delete + /// Id of the User deleting the Media + void MoveToRecycleBin(IMedia media, int userId); + + /// + /// Empties the Recycle Bin by deleting all that resides in the bin + /// + void EmptyRecycleBin(); + + /// + /// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin. + /// + /// This needs extra care and attention as its potentially a dangerous and extensive operation + /// Id of the + void DeleteMediaOfType(int mediaTypeId); + + /// + /// Permanently deletes an object + /// + /// + /// Please note that this method will completely remove the Media from the database, + /// but current not from the file system. + /// + /// The to delete + /// Id of the User deleting the Media + void Delete(IMedia media, int userId); + + /// + /// Saves a single object + /// + /// The to save + /// Id of the User saving the Content + void Save(IMedia media, int userId); + + /// + /// Saves a collection of objects + /// + /// Collection of to save + /// Id of the User saving the Content + void Save(IEnumerable medias, int userId); + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Services/MediaService.cs b/src/Umbraco.Web/Services/MediaService.cs new file mode 100644 index 0000000000..ce43fddd2a --- /dev/null +++ b/src/Umbraco.Web/Services/MediaService.cs @@ -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 +{ + /// + /// Represents the Media Service, which is an easy access to operations involving + /// + public class MediaService : IMediaService + { + private readonly IUnitOfWorkProvider _provider; + + public MediaService() : this(new PetaPocoUnitOfWorkProvider()) + { + } + + public MediaService(IUnitOfWorkProvider provider) + { + _provider = provider; + } + + /// + /// Gets an object by Id + /// + /// Id of the Content to retrieve + /// + public IMedia GetById(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + return repository.Get(id); + } + + /// + /// Gets a collection of objects by Parent Id + /// + /// Id of the Parent to retrieve Children from + /// An Enumerable list of objects + public IEnumerable GetChildren(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == id); + var medias = repository.GetByQuery(query); + + return medias; + } + + /// + /// Gets descendants of a object by its Id + /// + /// Id of the Parent to retrieve descendants from + /// An Enumerable flat list of objects + public IEnumerable GetDescendants(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var media = repository.Get(id); + + var query = Query.Builder.Where(x => x.Path.StartsWith(media.Path)); + var medias = repository.GetByQuery(query); + + return medias; + } + + /// + /// Gets a collection of objects by the Id of the + /// + /// Id of the + /// An Enumerable list of objects + public IEnumerable GetMediaOfMediaType(int id) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ContentTypeId == id); + var medias = repository.GetByQuery(query); + + return medias; + } + + /// + /// Gets a collection of objects, which reside at the first level / root + /// + /// An Enumerable list of objects + public IEnumerable GetRootMedia() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == -1); + var medias = repository.GetByQuery(query); + + return medias; + } + + /// + /// Gets a collection of an objects, which resides in the Recycle Bin + /// + /// An Enumerable list of objects + public IEnumerable GetMediaInRecycleBin() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == -20); + var medias = repository.GetByQuery(query); + + return medias; + } + + /// + /// Moves an object to a new location + /// + /// The to move + /// Id of the Media's new Parent + /// Id of the User moving the Media + public void Move(IMedia media, int parentId, int userId) + { + media.ParentId = parentId; + Save(media, userId); + } + + /// + /// Deletes an object by moving it to the Recycle Bin + /// + /// The to delete + /// Id of the User deleting the Media + 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(unitOfWork); + ((Core.Models.Media)media).ChangeTrashedState(true); + repository.AddOrUpdate(media); + unitOfWork.Commit(); + } + + /// + /// Empties the Recycle Bin by deleting all that resides in the bin + /// + public void EmptyRecycleBin() + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + + var query = Query.Builder.Where(x => x.ParentId == -20); + var contents = repository.GetByQuery(query); + + foreach (var content in contents) + { + repository.Delete(content); + } + unitOfWork.Commit(); + } + + /// + /// Deletes all media of specified type. All children of deleted media is moved to Recycle Bin. + /// + /// This needs extra care and attention as its potentially a dangerous and extensive operation + /// Id of the + public void DeleteMediaOfType(int mediaTypeId) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(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.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(); + } + + /// + /// Permanently deletes an object + /// + /// + /// Please note that this method will completely remove the Media from the database, + /// but current not from the file system. + /// + /// The to delete + /// Id of the User deleting the Media + public void Delete(IMedia media, int userId) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + repository.Delete(media); + unitOfWork.Commit(); + } + + /// + /// Saves a single object + /// + /// The to save + /// Id of the User saving the Content + public void Save(IMedia media, int userId) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + repository.AddOrUpdate(media); + unitOfWork.Commit(); + } + + /// + /// Saves a collection of objects + /// + /// Collection of to save + /// Id of the User saving the Content + public void Save(IEnumerable medias, int userId) + { + var unitOfWork = _provider.GetUnitOfWork(); + var repository = RepositoryResolver.ResolveByType(unitOfWork); + foreach (var media in medias) + { + repository.AddOrUpdate(media); + } + unitOfWork.Commit(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Services/UmbracoContextExtensions.cs b/src/Umbraco.Web/Services/UmbracoContextExtensions.cs index 429c077fd8..e69f98f762 100644 --- a/src/Umbraco.Web/Services/UmbracoContextExtensions.cs +++ b/src/Umbraco.Web/Services/UmbracoContextExtensions.cs @@ -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()); } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index dcdc9ad038..2306a102b2 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -317,7 +317,9 @@ + +