Implementing the Media part of U4-1568

This commit is contained in:
Morten Christensen
2013-01-29 12:58:02 -01:00
parent 14d5c9bee5
commit f3bfe0177d
3 changed files with 79 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ namespace Umbraco.Core.Events
ParentId = parentId;
}
public NewEventArgs(TEntity eventObject, bool canCancel, string @alias, IContent parent)
public NewEventArgs(TEntity eventObject, bool canCancel, string @alias, TEntity parent)
: base(eventObject, canCancel)
{
Alias = alias;
@@ -23,7 +23,7 @@ namespace Umbraco.Core.Events
ParentId = parentId;
}
public NewEventArgs(TEntity eventObject, string @alias, IContent parent)
public NewEventArgs(TEntity eventObject, string @alias, TEntity parent)
: base(eventObject)
{
Alias = alias;
@@ -51,6 +51,6 @@ namespace Umbraco.Core.Events
/// <summary>
/// Gets or Sets the parent IContent object.
/// </summary>
public IContent Parent { get; private set; }
public TEntity Parent { get; private set; }
}
}

View File

@@ -20,6 +20,17 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IMedia"/></returns>
IMedia CreateMedia(string name, int parentId, string mediaTypeAlias, int userId = 0);
/// <summary>
/// Creates an <see cref="IMedia"/> object using the alias of the <see cref="IMediaType"/>
/// that this Media is based on.
/// </summary>
/// <param name="name">Name of the Media object</param>
/// <param name="parent">Parent <see cref="IMedia"/> for the new Media item</param>
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns>
IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0);
/// <summary>
/// Gets an <see cref="IMedia"/> object by Id
/// </summary>
@@ -104,14 +115,16 @@ namespace Umbraco.Core.Services
/// </summary>
/// <param name="media">The <see cref="IMedia"/> to save</param>
/// <param name="userId">Id of the User saving the Media</param>
void Save(IMedia media, int userId = 0);
/// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param>
void Save(IMedia media, int userId = 0, bool raiseEvents = true);
/// <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 Media</param>
void Save(IEnumerable<IMedia> medias, int userId = 0);
/// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param>
void Save(IEnumerable<IMedia> medias, int userId = 0, bool raiseEvents = true);
/// <summary>
/// Gets an <see cref="IMedia"/> object by its 'UniqueId'

View File

@@ -50,13 +50,13 @@ namespace Umbraco.Core.Services
var mediaTypes = repository.GetByQuery(query);
if (!mediaTypes.Any())
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found",
throw new Exception(string.Format("No MediaType matching the passed in Alias: '{0}' was found",
mediaTypeAlias));
mediaType = mediaTypes.First();
if (mediaType == null)
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null",
throw new Exception(string.Format("MediaType matching the passed in Alias: '{0}' was null",
mediaTypeAlias));
}
@@ -74,6 +74,49 @@ namespace Umbraco.Core.Services
return media;
}
/// <summary>
/// Creates an <see cref="IMedia"/> object using the alias of the <see cref="IMediaType"/>
/// that this Media is based on.
/// </summary>
/// <param name="name">Name of the Media object</param>
/// <param name="parent">Parent <see cref="IMedia"/> for the new Media item</param>
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns>
public IMedia CreateMedia(string name, IMedia parent, string mediaTypeAlias, int userId = 0)
{
IMediaType mediaType = null;
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaTypeRepository(uow))
{
var query = Query<IMediaType>.Builder.Where(x => x.Alias == mediaTypeAlias);
var mediaTypes = repository.GetByQuery(query);
if (!mediaTypes.Any())
throw new Exception(string.Format("No MediaType matching the passed in Alias: '{0}' was found",
mediaTypeAlias));
mediaType = mediaTypes.First();
if (mediaType == null)
throw new Exception(string.Format("MediaType matching the passed in Alias: '{0}' was null",
mediaTypeAlias));
}
var media = new Models.Media(name, parent, mediaType);
if (Creating.IsRaisedEventCancelled(new NewEventArgs<IMedia>(media, mediaTypeAlias, parent), this))
return media;
media.CreatorId = userId;
Created.RaiseEvent(new NewEventArgs<IMedia>(media, false, mediaTypeAlias, parent), this);
Audit.Add(AuditTypes.New, "", media.CreatorId, media.Id);
return media;
}
/// <summary>
/// Gets an <see cref="IMedia"/> object by Id
/// </summary>
@@ -471,10 +514,14 @@ namespace Umbraco.Core.Services
/// </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 = 0)
/// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param>
public void Save(IMedia media, int userId = 0, bool raiseEvents = true)
{
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IMedia>(media), this))
if(raiseEvents)
{
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IMedia>(media), this))
return;
}
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
@@ -491,7 +538,8 @@ namespace Umbraco.Core.Services
: Convert.ToInt32(uow.Database.Insert(poco));
}
Saved.RaiseEvent(new SaveEventArgs<IMedia>(media, false), this);
if(raiseEvents)
Saved.RaiseEvent(new SaveEventArgs<IMedia>(media, false), this);
Audit.Add(AuditTypes.Save, "Save Media performed by user", media.CreatorId, media.Id);
}
@@ -501,10 +549,14 @@ namespace Umbraco.Core.Services
/// </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 = 0)
/// <param name="raiseEvents">Optional boolean indicating whether or not to raise events.</param>
public void Save(IEnumerable<IMedia> medias, int userId = 0, bool raiseEvents = true)
{
if (SavingCollection.IsRaisedEventCancelled(new SaveEventArgs<IEnumerable<IMedia>>(medias), this))
if(raiseEvents)
{
if (SavingCollection.IsRaisedEventCancelled(new SaveEventArgs<IEnumerable<IMedia>>(medias), this))
return;
}
var uow = _uowProvider.GetUnitOfWork();
using (var repository = _repositoryFactory.CreateMediaRepository(uow))
@@ -519,7 +571,8 @@ namespace Umbraco.Core.Services
uow.Commit();
}
SavedCollection.RaiseEvent(new SaveEventArgs<IEnumerable<IMedia>>(medias, false), this);
if(raiseEvents)
SavedCollection.RaiseEvent(new SaveEventArgs<IEnumerable<IMedia>>(medias, false), this);
Audit.Add(AuditTypes.Save, "Save Media items performed by user", userId, -1);
}