Skip to content

Commit

Permalink
Added merge command method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrebires committed Dec 13, 2017
1 parent 689e8db commit 7abef2b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Lime.Protocol/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public enum CommandMethod
/// </summary>
[EnumMember(Value = "observe")]
Observe,

/// <summary>
/// Merges the resource document with an existing one. If the resource doesn't exists, it is created.
/// </summary>
[EnumMember(Value = "merge")]
Merge,
}

/// <summary>
Expand Down
66 changes: 64 additions & 2 deletions src/Lime.Protocol/Network/ChannelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static async Task<TResource> GetResourceAsync<TResource>(this ICommandCha
}

/// <summary>
/// Sets the resource value asynchronous.
/// Sets the resource value.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="channel">The channel.</param>
Expand All @@ -129,7 +129,7 @@ public static Task SetResourceAsync<TResource>(this ICommandChannel channel, Lim
}

/// <summary>
/// Sets the resource value asynchronous.
/// Sets the resource value.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="channel">The channel.</param>
Expand Down Expand Up @@ -223,5 +223,67 @@ public static async Task DeleteResourceAsync(this ICommandChannel channel, LimeU
throw new InvalidOperationException("An invalid command response was received");
}
}

/// <summary>
/// Merges the resource value.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="channel">The channel.</param>
/// <param name="uri">The resource uri.</param>
/// <param name="resource">The resource to be merge.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">channel</exception>
public static Task MergeResourceAsync<TResource>(this ICommandChannel channel, LimeUri uri, TResource resource, CancellationToken cancellationToken) where TResource : Document
{
return MergeResourceAsync(channel, uri, resource, null, cancellationToken);
}

/// <summary>
/// Merges the resource value.
/// </summary>
/// <typeparam name="TResource">The type of the resource.</typeparam>
/// <param name="channel">The channel.</param>
/// <param name="uri">The resource uri.</param>
/// <param name="resource">The resource to be merge.</param>
/// <param name="from">The originator to be used in the command.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">channel</exception>
/// <exception cref="LimeException"></exception>
public static async Task MergeResourceAsync<TResource>(this ICommandChannel channel, LimeUri uri, TResource resource, Node from, CancellationToken cancellationToken) where TResource : Document
{
if (channel == null) throw new ArgumentNullException(nameof(channel));
if (uri == null) throw new ArgumentNullException(nameof(uri));
if (resource == null) throw new ArgumentNullException(nameof(resource));

var requestCommand = new Command
{
From = from,
Method = CommandMethod.Merge,
Uri = uri,
Resource = resource
};

var responseCommand = await channel.ProcessCommandAsync(requestCommand, cancellationToken).ConfigureAwait(false);
if (responseCommand.Status != CommandStatus.Success)
{
if (responseCommand.Reason != null)
{
throw new LimeException(responseCommand.Reason.Code, responseCommand.Reason.Description);
}
else
{
#if DEBUG
if (requestCommand == responseCommand)
{
throw new InvalidOperationException("The request and the response are the same instance");
}
#endif

throw new InvalidOperationException("An invalid command response was received");
}
}
}
}
}

0 comments on commit 7abef2b

Please sign in to comment.