chore: moves to ExpandoObject for updating entities because not all properties need to be part of the payload

This commit is contained in:
Matthias Langhard
2021-06-15 11:48:04 +02:00
parent e63d357d36
commit 51f464bad0
10 changed files with 30 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.ClientContacts.Models;
@@ -41,7 +42,7 @@ namespace Novaloop.PaymoApi.ClientContacts
}
/// <inheritdoc />
public async Task UpdateClientContact(ClientContact clientContact, int clientContactId)
public async Task UpdateClientContact(ExpandoObject clientContact, int clientContactId)
{
await _baseApi.Update(clientContact, clientContactId);
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.ClientContacts.Models;
@@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.ClientContacts
/// <param name="clientContact"></param>
/// <param name="clientContactId"></param>
/// <returns></returns>
Task UpdateClientContact(ClientContact clientContact, int clientContactId);
Task UpdateClientContact(ExpandoObject clientContact, int clientContactId);
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
@@ -41,7 +42,7 @@ namespace Novaloop.PaymoApi.Clients
}
/// <inheritdoc />
public async Task UpdateClient(Client client, int clientId)
public async Task UpdateClient(ExpandoObject client, int clientId)
{
await _baseApi.Update(client, clientId);
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Clients.Models;
@@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.Clients
/// <param name="client"></param>
/// <param name="clientId"></param>
/// <returns></returns>
Task UpdateClient(Client client, int clientId);
Task UpdateClient(ExpandoObject client, int clientId);
}
}

View File

@@ -1,6 +1,10 @@
using System.Dynamic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Novaloop.PaymoApi.Extensions;
namespace Novaloop.PaymoApi.Shared
@@ -70,10 +74,13 @@ namespace Novaloop.PaymoApi.Shared
/// <param name="entity">entity information to update the entity with</param>
/// <param name="id">id of the entity to update</param>
/// <returns></returns>
public async Task Update(TCreatType entity, int id)
public async Task Update(ExpandoObject entity, int id)
{
_client.SetApiKeyHeader(_options.ApiToken);
var response = await _client.PutAsJsonAsync($"api/{ResourceUri}/{id}", entity);
var serializerSettings = new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()};
var body = JsonConvert.SerializeObject(entity, serializerSettings);
var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
var response = await _client.PutAsync($"api/{ResourceUri}/{id}", stringContent);
await response.ThrowExceptionWithDetailsIfUnsuccessful();
}
}

View File

@@ -1,3 +1,4 @@
using System.Dynamic;
using System.Threading.Tasks;
namespace Novaloop.PaymoApi.Shared
@@ -36,6 +37,6 @@ namespace Novaloop.PaymoApi.Shared
/// <param name="entity">entity information to update the entity with</param>
/// <param name="id">id of the entity to update</param>
/// <returns></returns>
Task Update( TCreatType entity, int id);
Task Update(ExpandoObject entity, int id);
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
namespace Novaloop.PaymoApi.Tasks
@@ -35,6 +36,6 @@ namespace Novaloop.PaymoApi.Tasks
/// <param name="task"></param>
/// <param name="taskId"></param>
/// <returns></returns>
Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId);
Task UpdateTask(ExpandoObject task, int taskId);
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Novaloop.PaymoApi.Shared;
@@ -45,7 +46,7 @@ namespace Novaloop.PaymoApi.Tasks
/// <inheritdoc />
public async Task UpdateTask(Novaloop.PaymoApi.Tasks.Models.Task task, int taskId)
public async Task UpdateTask(ExpandoObject task, int taskId)
{
await _baseApi.Update(task, taskId);
}