forked from s434786/DINO_SCRUM
166 lines
4.5 KiB
C#
166 lines
4.5 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;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using System.Net;
|
|
using Magazyn.Windows;
|
|
|
|
namespace Magazyn
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
int i = 0;
|
|
|
|
public int size = 4;
|
|
public int page = 0;
|
|
|
|
bool firstPage;
|
|
bool lastPage;
|
|
|
|
HttpClient client;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
this.Initialized += MainWindow_Initialized; ;
|
|
|
|
client = new HttpClient();
|
|
}
|
|
|
|
private void MainWindow_Initialized(object sender, EventArgs 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) ;
|
|
|
|
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()
|
|
{
|
|
|
|
Task<HttpResponseMessage> response = client.GetAsync("https://sysmag.herokuapp.com/api/get-all?page=" + page.ToString() + "&size=" + size.ToString());
|
|
while (response.IsCompleted != true) ;
|
|
|
|
if (response.Result.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
ErrorWindow window = new ErrorWindow("Nastąpił błąd połączenia z serwerem.");
|
|
window.Owner = this;
|
|
window.ShowDialog();
|
|
return;
|
|
}
|
|
|
|
string responseString = response.Result.Content.ReadAsStringAsync().Result;
|
|
|
|
JObject replay = JObject.Parse(responseString);
|
|
|
|
firstPage = bool.Parse(replay["first"].ToString());
|
|
lastPage = bool.Parse(replay["last"].ToString());
|
|
|
|
Fruit[] fruits = JsonConvert.DeserializeObject<Fruit[]>(replay["content"].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 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;
|
|
double height = size.Height - 124;
|
|
this.size = (int)(height / 90);
|
|
RefreshListOfFruits();
|
|
}
|
|
|
|
private void themeButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var app = App.Current as App;
|
|
app.ChangeTheme(i++);
|
|
}
|
|
}
|
|
}
|
|
|