2019-01-13 16:36:26 +01:00
|
|
|
|
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;
|
2019-01-22 17:35:51 +01:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2019-01-13 16:36:26 +01:00
|
|
|
|
|
2019-01-22 21:02:49 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Magazyn.Windows;
|
2019-01-23 23:10:44 +01:00
|
|
|
|
using Magazyn.Tools;
|
2019-01-27 17:28:29 +01:00
|
|
|
|
using System.Net.NetworkInformation;
|
2019-01-27 21:17:23 +01:00
|
|
|
|
using System.Configuration;
|
2019-01-22 21:02:49 +01:00
|
|
|
|
|
2019-01-13 16:36:26 +01:00
|
|
|
|
namespace Magazyn
|
|
|
|
|
{
|
2019-01-23 22:39:54 +01:00
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public int size = 4;
|
|
|
|
|
public int page = 0;
|
|
|
|
|
|
|
|
|
|
bool firstPage;
|
|
|
|
|
bool lastPage;
|
2019-01-27 17:28:29 +01:00
|
|
|
|
bool netConn;
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
|
|
|
|
HttpClient client;
|
|
|
|
|
|
|
|
|
|
Size windowSize;
|
|
|
|
|
|
2019-01-27 21:17:23 +01:00
|
|
|
|
string serverUrl;
|
|
|
|
|
|
2019-01-23 22:39:54 +01:00
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-01-27 21:17:23 +01:00
|
|
|
|
|
|
|
|
|
serverUrl = ConfigurationManager.AppSettings.Get("serverUrl");
|
|
|
|
|
|
2019-01-23 22:39:54 +01:00
|
|
|
|
client = new HttpClient();
|
|
|
|
|
this.Loaded += MainWindowLoaded;
|
2019-01-27 17:28:29 +01:00
|
|
|
|
netConn = checkInternetConnection();
|
|
|
|
|
if (netConn)
|
|
|
|
|
netStatus.Text = "Połączony";
|
|
|
|
|
else
|
|
|
|
|
netStatus.Text = "Rozłączony";
|
2019-01-23 22:39:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MainWindowLoaded(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
RefreshListOfFruits();
|
|
|
|
|
windowSize = new Size(this.Width, this.Height);
|
|
|
|
|
this.SizeChanged += Window_SizeChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FruitViewSaveChangingAmountOfFruits(int amountChanged, Fruit fruit)
|
|
|
|
|
{
|
|
|
|
|
string json = "{\"id\": " + fruit.Id.ToString() + ", \"change\": " + amountChanged.ToString() + " }";
|
|
|
|
|
|
2019-01-27 21:17:23 +01:00
|
|
|
|
Task<HttpResponseMessage> response = client.PostAsync(serverUrl+"product/change-quantity", new StringContent(json, Encoding.UTF8, "application/json"));
|
2019-01-23 22:39:54 +01:00
|
|
|
|
while (response.IsCompleted != true) ;
|
|
|
|
|
|
|
|
|
|
if (response.Result.StatusCode == HttpStatusCode.BadRequest)
|
|
|
|
|
{
|
|
|
|
|
JObject data = JObject.Parse(response.Result.Content.ReadAsStringAsync().Result.ToString());
|
|
|
|
|
|
|
|
|
|
ErrorWindow window;
|
|
|
|
|
|
|
|
|
|
if (data["message"].ToString().Contains("Too low"))
|
|
|
|
|
{
|
|
|
|
|
window = new ErrorWindow("Wystąpił błąd niedomiaru. Próbujesz usunąć więcej owoców niż masz w magazynie.");
|
|
|
|
|
}
|
|
|
|
|
else if (data["message"].ToString().Contains("Over max"))
|
|
|
|
|
{
|
|
|
|
|
window = new ErrorWindow("Wystąpił błąd nadmiaru. Próbujesz dodać więcej owoców niż pomieści magazyn.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
window = new ErrorWindow("Nieznany błąd.");
|
|
|
|
|
}
|
|
|
|
|
window.Owner = this;
|
|
|
|
|
window.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefreshListOfFruits();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshListOfFruits()
|
|
|
|
|
{
|
2019-01-27 17:28:29 +01:00
|
|
|
|
if (netConn)
|
2019-01-23 22:39:54 +01:00
|
|
|
|
{
|
2019-01-27 19:31:58 +01:00
|
|
|
|
pageBlock.Text = (page+1).ToString();
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 21:17:23 +01:00
|
|
|
|
Task<HttpResponseMessage> response = client.GetAsync(serverUrl+"get-all?page=" + page.ToString() + "&size=" + size.ToString());
|
2019-01-27 17:28:29 +01:00
|
|
|
|
while (response.IsCompleted != true) ;
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
if (response.Result.StatusCode != HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
ErrorWindow window = new ErrorWindow("Nastąpił błąd połączenia z serwerem.");
|
|
|
|
|
window.Owner = this;
|
|
|
|
|
window.ShowDialog();
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
page = 0;
|
|
|
|
|
size = 0;
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
string responseString = response.Result.Content.ReadAsStringAsync().Result;
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
JObject replay = JObject.Parse(responseString);
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
firstPage = bool.Parse(replay["first"].ToString());
|
|
|
|
|
lastPage = bool.Parse(replay["last"].ToString());
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
Fruit[] fruits = JsonConvert.DeserializeObject<Fruit[]>(replay["content"].ToString());
|
2019-01-23 22:39:54 +01:00
|
|
|
|
|
2019-01-27 17:28:29 +01:00
|
|
|
|
UpdateListOfFruits(fruits);
|
|
|
|
|
}
|
2019-01-23 22:39:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2019-01-27 21:17:23 +01:00
|
|
|
|
Task<HttpResponseMessage> response = client.GetAsync(serverUrl+"get-price-of-all");
|
2019-01-23 22:39:54 +01:00
|
|
|
|
while (response.IsCompleted != true) ;
|
|
|
|
|
|
|
|
|
|
WarehousePrice price = JsonConvert.DeserializeObject<WarehousePrice>(response.Result.Content.ReadAsStringAsync().Result.ToString());
|
|
|
|
|
|
|
|
|
|
return price;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Value_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ValueWindow window = new ValueWindow(GetWarehousePrice().Price);
|
|
|
|
|
window.Owner = this;
|
|
|
|
|
window.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void prevButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (firstPage != true) page--;
|
|
|
|
|
RefreshListOfFruits();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void nextButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (lastPage != true) page++;
|
|
|
|
|
RefreshListOfFruits();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Size size = e.NewSize;
|
|
|
|
|
|
2019-01-27 21:17:23 +01:00
|
|
|
|
if( Math.Abs(windowSize.Height - size.Height) >= 64 )
|
2019-01-23 22:39:54 +01:00
|
|
|
|
{
|
|
|
|
|
//size -> number of elementy by page
|
|
|
|
|
this.size = (int)((size.Height - 124) / 80);
|
|
|
|
|
page = 0;
|
|
|
|
|
windowSize = size;
|
|
|
|
|
RefreshListOfFruits();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fruitListMouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (fruitList.SelectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
InfoWindow window = new InfoWindow((fruitList.SelectedItem as FruitView).fruit);
|
|
|
|
|
window.Owner = this;
|
|
|
|
|
window.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-27 17:28:29 +01:00
|
|
|
|
|
|
|
|
|
private Boolean checkInternetConnection()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return new Ping().Send("www.google.com").Status == IPStatus.Success;
|
|
|
|
|
} catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-27 21:17:23 +01:00
|
|
|
|
|
|
|
|
|
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
//RefreshListOfFruits();
|
|
|
|
|
}
|
2019-01-23 22:39:54 +01:00
|
|
|
|
}
|
2019-01-13 16:36:26 +01:00
|
|
|
|
}
|
|
|
|
|
|