/// <summary> /// Adds the child. /// </summary> /// <param name="item">The item.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> /// <exception cref="System.InvalidOperationException">Unable to add + item.Name</exception> public async Task AddChild(BaseItem item, CancellationToken cancellationToken) { item.Parent = this; if (item.Id == Guid.Empty) { item.Id = LibraryManager.GetNewItemId(item.Path, item.GetType()); } if (ActualChildren.Any(i => i.Id == item.Id)) { throw new ArgumentException(string.Format("A child with the Id {0} already exists.", item.Id)); } if (item.DateCreated == DateTime.MinValue) { item.DateCreated = DateTime.UtcNow; } if (item.DateModified == DateTime.MinValue) { item.DateModified = DateTime.UtcNow; } AddChildInternal(item); await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false); await ItemRepository.SaveChildren(Id, ActualChildren.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false); }
/// <summary> /// Adds the child. /// </summary> /// <param name="item">The item.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task.</returns> /// <exception cref="System.InvalidOperationException">Unable to add + item.Name</exception> public async Task AddChild(BaseItem item, CancellationToken cancellationToken) { item.Parent = this; if (item.Id == Guid.Empty) { item.Id = item.Path.GetMBId(item.GetType()); } if (item.DateCreated == DateTime.MinValue) { item.DateCreated = DateTime.UtcNow; } if (item.DateModified == DateTime.MinValue) { item.DateModified = DateTime.UtcNow; } if (!_children.TryAdd(item.Id, item)) { throw new InvalidOperationException("Unable to add " + item.Name); } await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false); await ItemRepository.SaveChildren(Id, _children.Values.ToList().Select(i => i.Id), cancellationToken).ConfigureAwait(false); }