Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(teams): more options for creating channel #1126

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 96 additions & 24 deletions src/sdk/PnP.Core/Model/Teams/Internal/TeamChannelCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public TeamChannelCollection(PnPContext context, IDataModelParent parent, string

#region Add methods

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public async Task<ITeamChannel> AddAsync(string name, TeamChannelOptions options)
{
var newChannel = CreateNewAndAdd(name, options);
return await newChannel.AddAsync().ConfigureAwait(false) as TeamChannel;
}

/// <summary>
/// Adds a new channel
/// </summary>
Expand All @@ -25,20 +37,18 @@ public TeamChannelCollection(PnPContext context, IDataModelParent parent, string
/// <returns>Newly added channel</returns>
public async Task<ITeamChannel> AddAsync(string name, string description = null)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

// TODO: validate name restrictions

var newChannel = CreateNewAndAdd() as TeamChannel;

// Assign field values
newChannel.DisplayName = name;
newChannel.Description = description;
return await AddAsync(name, new TeamChannelOptions(description)).ConfigureAwait(false);
}

return await newChannel.AddAsync().ConfigureAwait(false) as TeamChannel;
/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel Add(string name, TeamChannelOptions options)
{
return AddAsync(name, options).GetAwaiter().GetResult();
}

/// <summary>
Expand All @@ -52,6 +62,19 @@ public ITeamChannel Add(string name, string description = null)
return AddAsync(name, description).GetAwaiter().GetResult();
}

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="batch">Batch to use</param>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public async Task<ITeamChannel> AddBatchAsync(Batch batch, string name, TeamChannelOptions options)
{
var newChannel = CreateNewAndAdd(name, options);
return await newChannel.AddBatchAsync(batch).ConfigureAwait(false) as TeamChannel;
}

/// <summary>
/// Adds a new channel
/// </summary>
Expand All @@ -61,18 +84,19 @@ public ITeamChannel Add(string name, string description = null)
/// <returns>Newly added channel</returns>
public async Task<ITeamChannel> AddBatchAsync(Batch batch, string name, string description = null)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

var newChannel = CreateNewAndAdd() as TeamChannel;

// Assign field values
newChannel.DisplayName = name;
newChannel.Description = description;
return await AddBatchAsync(name, new TeamChannelOptions(description)).ConfigureAwait(false);
}

return await newChannel.AddBatchAsync(batch).ConfigureAwait(false) as TeamChannel;
/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="batch">Batch to use</param>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel AddBatch(Batch batch, string name, TeamChannelOptions options)
{
return AddBatchAsync(batch, name, options).GetAwaiter().GetResult();
}

/// <summary>
Expand All @@ -87,6 +111,17 @@ public ITeamChannel AddBatch(Batch batch, string name, string description = null
return AddBatchAsync(batch, name, description).GetAwaiter().GetResult();
}

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public async Task<ITeamChannel> AddBatchAsync(string name, TeamChannelOptions options)
{
return await AddBatchAsync(PnPContext.CurrentBatch, name, options).ConfigureAwait(false);
}

/// <summary>
/// Adds a new channel
/// </summary>
Expand All @@ -98,6 +133,17 @@ public async Task<ITeamChannel> AddBatchAsync(string name, string description =
return await AddBatchAsync(PnPContext.CurrentBatch, name, description).ConfigureAwait(false);
}

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel AddBatch(string name, TeamChannelOptions options)
{
return AddBatchAsync(name, options).GetAwaiter().GetResult();
}

/// <summary>
/// Adds a new channel
/// </summary>
Expand All @@ -109,6 +155,32 @@ public ITeamChannel AddBatch(string name, string description = null)
return AddBatchAsync(name, description).GetAwaiter().GetResult();
}

/// <summary>
/// Creates a new `TeamChannel` instance,
/// adds it to the collection and configures it.
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">Missing name argument</exception>
private TeamChannel CreateNewAndAdd(string name, TeamChannelOptions options)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

// TODO: validate name restrictions

var newChannel = CreateNewAndAdd() as TeamChannel;

newChannel.DisplayName = name;
newChannel.Description = options.Description;
if(options.MembershipType.HasValue) newChannel.MembershipType = options.MembershipType.Value;

return newChannel;
}

#endregion

#region GetByDisplayName methods
Expand Down
50 changes: 50 additions & 0 deletions src/sdk/PnP.Core/Model/Teams/Public/ITeamChannelCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ namespace PnP.Core.Model.Teams
public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumerable<ITeamChannel>, IDataModelCollection<ITeamChannel>, IDataModelCollectionLoad<ITeamChannel>, IDataModelCollectionDeleteByStringId, ISupportModules<ITeamChannelCollection>
{
#region Add methods

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddAsync(string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
Expand All @@ -22,6 +30,14 @@ public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumer
/// <param name="description">Optional description of the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddAsync(string name, string description = null);

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel Add(string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
Expand All @@ -31,6 +47,15 @@ public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumer
/// <returns>Newly added channel</returns>
public ITeamChannel Add(string name, string description = null);

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="batch">Batch to use</param>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddBatchAsync(Batch batch, string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
/// </summary>
Expand All @@ -39,6 +64,15 @@ public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumer
/// <param name="description">Optional description of the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddBatchAsync(Batch batch, string name, string description = null);

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="batch">Batch to use</param>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel AddBatch(Batch batch, string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
Expand All @@ -48,6 +82,14 @@ public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumer
/// <param name="description">Optional description of the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel AddBatch(Batch batch, string name, string description = null);

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddBatchAsync(string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
Expand All @@ -56,6 +98,14 @@ public interface ITeamChannelCollection : IQueryable<ITeamChannel>, IAsyncEnumer
/// <param name="description">Optional description of the channel</param>
/// <returns>Newly added channel</returns>
public Task<ITeamChannel> AddBatchAsync(string name, string description = null);

/// <summary>
/// Adds a new channel
/// </summary>
/// <param name="name">Display name of the channel</param>
/// <param name="options">Options for creating the channel</param>
/// <returns>Newly added channel</returns>
public ITeamChannel AddBatch(string name, TeamChannelOptions options);

/// <summary>
/// Adds a new channel
Expand Down
30 changes: 30 additions & 0 deletions src/sdk/PnP.Core/Model/Teams/Public/Options/TeamChannelOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace PnP.Core.Model.Teams
{
/// <summary>
/// Available options for Teams channel
/// </summary>
public class TeamChannelOptions
{
/// <summary>
/// Gets or sets the channel description.
/// </summary>
public string Description { get; set; }

/// <summary>
/// Gets or sets the channel membership type.
///
/// The membership type cannot be changed for existing channels.
/// </summary>
public TeamChannelMembershipType? MembershipType { get; set; }

/// <summary>
/// Creates a new `TeamChannelOptions` instance
/// with the provided description.
/// </summary>
/// <param name="description">The channel description.</param>
public TeamChannelOptions(string description = null)
{
Description = description;
}
}
}