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; using Umbraco.Core.Services; namespace Umbraco.Web.Services { /// /// Represents the Media Service, which is an easy access to operations involving /// public class MediaService : IMediaService { private readonly IUnitOfWork _unitOfWork; public MediaService() : this(new PetaPocoUnitOfWorkProvider()) { } public MediaService(IUnitOfWorkProvider provider) { _unitOfWork = provider.GetUnitOfWork(); } /// /// Gets an object by Id /// /// Id of the Content to retrieve /// public IMedia GetById(int id) { 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 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 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 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 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 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 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 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 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 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 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 repository = RepositoryResolver.ResolveByType(_unitOfWork); foreach (var media in medias) { repository.AddOrUpdate(media); } _unitOfWork.Commit(); } } }