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

Add a PostDocumentAsync with extra generic for new doc type. #305

Merged
merged 1 commit into from
Nov 13, 2020
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
20 changes: 20 additions & 0 deletions arangodb-net-standard.Test/DocumentApi/DocumentApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ public async Task PostDocument_ShouldSucceed_WhenNewDocIsReturned()
Assert.Equal(123, (int)response.New.test);
}

[Fact]
public async Task PostDocument_ShouldSucceed_WhenNewDocIsReturnedWithDifferentType()
{
var doc = new PostDocumentMockModelNew
{
Message = "Hello"
};
var response = await _docClient.PostDocumentAsync<PostDocumentMockModelNew, PostDocumentMockModel>(
_testCollection,
doc,
new PostDocumentsQuery
{
ReturnNew = true
});
Assert.Null(response.Old);
Assert.NotNull(response.New);
Assert.Equal(doc.Message, response.New.Message);
Assert.Equal(response._id, response.New._id);
}

[Fact]
public async Task PostDocument_ShouldFail_WhenDocumentIsInvalid()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ArangoDBNetStandard.DocumentApi.Models;

namespace ArangoDBNetStandardTest.DocumentApi.Models
{
public class PostDocumentMockModel : DocumentBase
{
public string Message { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace ArangoDBNetStandardTest.DocumentApi.Models
{
public class PostDocumentMockModelNew
{
public string Message { get; set; }
}
}
31 changes: 29 additions & 2 deletions arangodb-net-standard/DocumentApi/DocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,34 @@ public DocumentApiClient(IApiClientTransport client, IApiClientSerialization ser
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
public virtual Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
string collectionName,
T document,
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null)
{
return PostDocumentAsync<T, T>(
collectionName,
document,
query,
serializationOptions);
}

/// <summary>
/// Post a single document with the possibility to specify a different type
/// for the new document object returned in the response.
/// </summary>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <typeparam name="U">Type of the returned document, only applies when
/// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/>
/// are used.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
public virtual async Task<PostDocumentResponse<U>> PostDocumentAsync<T, U>(
string collectionName,
T document,
PostDocumentsQuery query = null,
Expand All @@ -72,7 +99,7 @@ public virtual async Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync();
return DeserializeJsonFromStream<PostDocumentResponse<T>>(stream);
return DeserializeJsonFromStream<PostDocumentResponse<U>>(stream);
}
throw await GetApiErrorException(response);
}
Expand Down
22 changes: 21 additions & 1 deletion arangodb-net-standard/DocumentApi/IDocumentApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IDocumentApiClient
/// <summary>
/// Post a single document.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
Expand All @@ -27,6 +27,26 @@ Task<PostDocumentResponse<T>> PostDocumentAsync<T>(
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null);

/// <summary>
/// Post a single document with the possibility to specify a different type
/// for the new document object returned in the response.
/// </summary>
/// <typeparam name="T">The type of the post object used to record a new document.</typeparam>
/// <typeparam name="U">Type of the returned document, only applies when
/// <see cref="PostDocumentsQuery.ReturnNew"/> or <see cref="PostDocumentsQuery.ReturnOld"/>
/// are used.</typeparam>
/// <param name="collectionName"></param>
/// <param name="document"></param>
/// <param name="query"></param>
/// <param name="serializationOptions">The serialization options. When the value is null the
/// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
/// <returns></returns>
Task<PostDocumentResponse<U>> PostDocumentAsync<T, U>(
string collectionName,
T document,
PostDocumentsQuery query = null,
ApiClientSerializationOptions serializationOptions = null);

/// <summary>
/// Post multiple documents in a single request.
/// </summary>
Expand Down