private async void BookAdd_Click(object sender, RoutedEventArgs e)
{
Notice.Foreground = new SolidColorBrush(Windows.UI.Colors.Black);
Notice.Text = "Attempting to add book...";
Dictionary<string, string> newBook = new Dictionary<string, string>();
newBook["isbn"] = ISBN.Text;
newBook["librarian_barcode"] = Librarian.Password;
string json = JsonConvert.SerializeObject(newBook);
try
{
HttpClient client = new HttpClient();
StringContent theContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage aResponse = await client.PostAsync(new Uri("http://alexandria.ad.sofse.org:8080/books.json"), theContent);
string content = await aResponse.Content.ReadAsStringAsync();
ISBN.Text = "";
Librarian.Password = "";
if ((int)aResponse.StatusCode == 201)
{
Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
Notice.Foreground = new SolidColorBrush(Windows.UI.Colors.ForestGreen);
Notice.Text = "You succesfully added " + dictionary["title"] + "!";
}
else
{
Dictionary<string, List<string>> dictionary = JsonConvert.DeserializeObject <Dictionary<string, List<string>>>(content);
string notice = "";
foreach (var item in dictionary)
{
foreach (var error in item.Value)
{
notice += error + "\n";
}
}
Notice.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
Notice.Text = notice;
}
}
catch (HttpRequestException)
{
Notice.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
Notice.Text = "You might want to consider connecting to the internet...";
return;
}
}
}