From fd046f97eb5c1f925deaf7230dae4d45afad845c Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Thu, 20 Oct 2016 01:34:45 +0200 Subject: [PATCH] implements an update call --- BookAStar/BookAStar/Data/RemoteEntity.cs | 32 +++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/BookAStar/BookAStar/Data/RemoteEntity.cs b/BookAStar/BookAStar/Data/RemoteEntity.cs index ce70e52b..3b91aa8d 100644 --- a/BookAStar/BookAStar/Data/RemoteEntity.cs +++ b/BookAStar/BookAStar/Data/RemoteEntity.cs @@ -9,6 +9,7 @@ using System.Net; namespace BookAStar.Data { using Helpers; + using System.Text; public class RemoteEntity : LocalEntity, ICommand where K : IEquatable { @@ -110,8 +111,8 @@ namespace BookAStar.Data } } - AfterExecuting(); CurrentItem = item; + AfterExecuting(); return item; } @@ -121,22 +122,45 @@ namespace BookAStar.Data using (HttpClient client = UserHelpers.CreateClient()) { + var stringContent = JsonConvert.SerializeObject(item); HttpContent content = new StringContent( - JsonConvert.SerializeObject(item) + stringContent, Encoding.UTF8, "application/json" ); using (var response = await client.PostAsync(controllerUri, content)) { if (!response.IsSuccessStatusCode) + { + var errcontent = await response.Content.ReadAsStringAsync(); // TODO throw custom exception, and catch to inform user - throw new Exception($"Create failed posting {item} @ {controllerUri.AbsolutePath}"); + throw new Exception($"Create failed posting {stringContent} @ {controllerUri.AbsoluteUri}"); + } var recontent = await response.Content.ReadAsStringAsync(); JsonConvert.PopulateObject(recontent, item); - } } + CurrentItem = item; AfterExecuting(); + } + public async void Update (V item) + { + BeforeExecute(); + + using (HttpClient client = UserHelpers.CreateClient()) + { + HttpContent content = new StringContent( + JsonConvert.SerializeObject(item) + ); + using (var response = await client.PutAsync(controllerUri, content)) + { + if (!response.IsSuccessStatusCode) + // TODO throw custom exception, and catch to inform user + throw new Exception($"Update failed puting {item} @ {controllerUri.AbsolutePath}"); + } + } + CurrentItem = item; + AfterExecuting(); } }