DINO_SCRUM/Magazyn_Client/Magazyn/MainWindow.xaml.cs
2019-01-21 19:48:10 +01:00

104 lines
2.8 KiB
C#

using Magazyn.DataModels;
using Magazyn.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
namespace Magazyn
{
public partial class MainWindow : Window
{
HttpClient client;
public MainWindow()
{
InitializeComponent();
loadingContent.IsIndeterminate = true;
client = new HttpClient();
RefreshListOfFruits();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RefreshListOfFruits();
}
private void FruitViewSaveChangingAmountOfFruits(int amountChanged, Fruit fruit)
{
string json = "{\"id\": " + fruit.Id.ToString() + ", \"change\": " + amountChanged.ToString() + " }";
Task<HttpResponseMessage> response = client.PostAsync("https://sysmag.herokuapp.com/api/product/change-quantity", new StringContent(json, Encoding.UTF8, "application/json"));
while (response.IsCompleted != true)
{
RefreshListOfFruits();
}
}
private void RefreshListOfFruits()
{
loadingContent.Visibility = Visibility.Visible;
loadingContent.IsIndeterminate = true;
Task<HttpResponseMessage> response = client.GetAsync("https://sysmag.herokuapp.com/api/get-all");
while (response.IsCompleted != true)
{
loadingContent.IsIndeterminate = false;
loadingContent.Visibility = Visibility.Hidden;
}
Fruit[] fruits = JsonConvert.DeserializeObject<Fruit[]>(response.Result.Content.ReadAsStringAsync().Result.ToString());
UpdateListOfFruits(fruits);
}
private void UpdateListOfFruits(Fruit[] list)
{
fruitList.Items.Clear();
foreach (Fruit item in list)
{
FruitView fruitView = new FruitView(item);
fruitView.SaveChangingAmountOfFruits += FruitViewSaveChangingAmountOfFruits;
fruitList.Items.Add(fruitView);
}
}
private WarehousePrice GetWarehousePrice()
{
Task<HttpResponseMessage> response = client.GetAsync("https://sysmag.herokuapp.com/api/get-price-of-all");
while (response.IsCompleted != true) ;
WarehousePrice price = JsonConvert.DeserializeObject<WarehousePrice>(response.Result.Content.ReadAsStringAsync().Result.ToString());
return price;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MessageBox.Show("Wartość magazynu to: " + GetWarehousePrice().Price.ToString() + "zł", "Wartość magazynu" );
}
}
}