private void MovedMediaToRecycleBin(MediaMovedToRecycleBinNotification notification)
 {
     foreach (var item in notification.MoveInfoCollection)
     {
         // if this item doesn't exist then Fail!
         var exists = MediaService.GetById(item.Entity.Id);
         if (exists == null)
         {
             Assert.Fail("The item doesn't exist");
         }
     }
 }
    public void Handle(MediaMovedToRecycleBinNotification notification)
    {
        using (IScope scope = _scopeProvider.CreateScope())
        {
            const string  relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
            IRelationType?relationType      = _relationService.GetRelationTypeByAlias(relationTypeAlias);

            // check that the relation-type exists, if not, then recreate it
            if (relationType == null)
            {
                Guid         documentObjectType = Constants.ObjectTypes.Document;
                const string relationTypeName   = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName;
                relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, documentObjectType, false);
                _relationService.Save(relationType);
            }

            foreach (MoveEventInfo <IMedia> item in notification.MoveInfoCollection)
            {
                IList <string> originalPath     = item.OriginalPath.ToDelimitedList();
                var            originalParentId = originalPath.Count > 2
                    ? int.Parse(originalPath[originalPath.Count - 2], CultureInfo.InvariantCulture)
                    : Constants.System.Root;

                // before we can create this relation, we need to ensure that the original parent still exists which
                // may not be the case if the encompassing transaction also deleted it when this item was moved to the bin
                if (_entityService.Exists(originalParentId))
                {
                    // Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
                    IRelation relation =
                        _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ??
                        new Relation(originalParentId, item.Entity.Id, relationType);
                    _relationService.Save(relation);
                    _auditService.Add(
                        AuditType.Delete,
                        item.Entity.CreatorId,
                        item.Entity.Id,
                        UmbracoObjectTypes.Media.GetName(),
                        string.Format(_textService.Localize("recycleBin", "mediaTrashed"), item.Entity.Id, originalParentId));
                }
            }

            scope.Complete();
        }
    }