diff --git a/EstusShots.Client/EstusShots.Client.csproj b/EstusShots.Client/EstusShots.Client.csproj
new file mode 100644
index 0000000..548a249
--- /dev/null
+++ b/EstusShots.Client/EstusShots.Client.csproj
@@ -0,0 +1,11 @@
+
+
+
+ netcoreapp3.1
+
+
+
+
+
+
+
diff --git a/EstusShots.Client/EstusShotsClient.cs b/EstusShots.Client/EstusShotsClient.cs
new file mode 100644
index 0000000..dba29d2
--- /dev/null
+++ b/EstusShots.Client/EstusShotsClient.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using System.Text.Json;
+using System.Threading.Tasks;
+using EstusShots.Shared.Models;
+
+namespace EstusShots.Client
+{
+ public class EstusShotsClient
+ {
+ private readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
+ {
+ PropertyNameCaseInsensitive = true,
+ };
+
+ private string ApiUrl { get; }
+ private HttpClient HttpClient { get; }
+
+ public EstusShotsClient(string apiUrl)
+ {
+ ApiUrl = apiUrl;
+ HttpClient = new HttpClient {Timeout = TimeSpan.FromSeconds(10)};
+ }
+
+ public async Task> GetSeasons()
+ {
+ var response = HttpClient.GetAsync(ApiUrl + "seasons").Result;
+ var jsonData = await response.Content.ReadAsStringAsync();
+ var data = JsonSerializer.Deserialize>(jsonData, _serializerOptions);
+ return data;
+ }
+ }
+}
\ No newline at end of file
diff --git a/EstusShots.Gtk/DataStores/SeasonsDataStore.cs b/EstusShots.Gtk/DataStores/SeasonsDataStore.cs
new file mode 100644
index 0000000..5961da9
--- /dev/null
+++ b/EstusShots.Gtk/DataStores/SeasonsDataStore.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using EstusShots.Shared.Models;
+using GLib;
+using Gtk;
+
+namespace EstusShots.Gtk.DataStores
+{
+ public class SeasonsDataStore
+ {
+ public ListStore ListStore { get; private set; }
+
+ public TreeView View { get; set; }
+
+ public List Data { get; set; }
+
+ public SeasonsDataStore(TreeView view)
+ {
+ ListStore = new ListStore(GType.Int, GType.String, GType.String);
+ Data = new List();
+ View = view;
+ var columns = BuildColumns();
+ columns.ForEach(column => View.AppendColumn(column));
+ View.Model = ListStore;
+ }
+
+ public void DataBind()
+ {
+ ListStore.Clear();
+ foreach (var datum in Data)
+ {
+ var row = new object[] {datum.Number, datum.Game, datum.Start.ToString(CultureInfo.InvariantCulture)};
+ try
+ {
+ ListStore.AppendValues(row);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e);
+ }
+ }
+ }
+ private List BuildColumns()
+ {
+ var columns = new List
+ {
+ new TreeViewColumn("Number", new CellRendererText(), "text", 0),
+ new TreeViewColumn("Game", new CellRendererText(), "text", 1),
+ new TreeViewColumn("Start", new CellRendererText(), "text", 2)
+ };
+ return columns;
+ }
+ }
+}
\ No newline at end of file
diff --git a/EstusShots.Gtk/EstusShots.Gtk.csproj b/EstusShots.Gtk/EstusShots.Gtk.csproj
index 82d6bad..4ddfaa2 100644
--- a/EstusShots.Gtk/EstusShots.Gtk.csproj
+++ b/EstusShots.Gtk/EstusShots.Gtk.csproj
@@ -17,6 +17,7 @@
+
diff --git a/EstusShots.Gtk/MainWindow.cs b/EstusShots.Gtk/MainWindow.cs
index 78df622..4a7c5da 100644
--- a/EstusShots.Gtk/MainWindow.cs
+++ b/EstusShots.Gtk/MainWindow.cs
@@ -1,9 +1,14 @@
using System;
+using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
+using EstusShots.Client;
+using EstusShots.Gtk.DataStores;
using EstusShots.Shared.Models;
using Gtk;
+using Application = Gtk.Application;
+using DateTime = System.DateTime;
using UI = Gtk.Builder.ObjectAttribute;
namespace EstusShots.Gtk
@@ -11,60 +16,86 @@ namespace EstusShots.Gtk
class MainWindow : Window
{
private const string ApiUrl = "http://localhost:5000/api/";
+
+ private EstusShotsClient Client { get; }
- [UI] private TreeView _seasonsView = null;
- [UI] private Button _loadButton = null;
- [UI] private Label _infoLabel = null;
+ [UI] private readonly TreeView _seasonsView = null;
+ [UI] private readonly Button _loadButton = null;
+ [UI] private readonly Button _newSeasonButton = null;
+ [UI] private readonly Label _infoLabel = null;
+
+ private SeasonsDataStore SeasonsView { get; set; }
public MainWindow() : this(new Builder("MainWindow.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("MainWindow").Handle)
{
builder.Autoconnect(this);
+ Client = new EstusShotsClient("http://localhost:5000/api/");
DeleteEvent += Window_DeleteEvent;
- _loadButton.Clicked += _loadButton_Clicked;
+ _loadButton.Clicked += LoadButtonClicked;
+ _newSeasonButton.Clicked += NewSeasonButtonOnClicked;
+
+ SeasonsView = new SeasonsDataStore(_seasonsView);
+
+ Info("Application Started");
}
- private void Window_DeleteEvent(object sender, DeleteEventArgs a)
+ private void NewSeasonButtonOnClicked(object sender, EventArgs e)
{
- Application.Quit();
- }
-
- private void _loadButton_Clicked(object sender, EventArgs a)
- {
- var season = new Season()
+ var season = new Season
{
Game = "Test Game",
+ Number = 1,
Start = DateTime.Now
- };
-
+ };
var content = new StringContent(JsonSerializer.Serialize(season), Encoding.UTF8, "application/json");
var client = new HttpClient();
try
{
- var response = client.PostAsync(ApiUrl + "seasons", content).Result;
+ var response = client.PostAsync(ApiUrl + "season", content).Result;
- if (response.Headers.Location == null)
+ if (!response.IsSuccessStatusCode)
{
_infoLabel.Text = $"Error while creating Season: {response.ReasonPhrase}";
return;
}
- var data = client.GetAsync(response.Headers.Location).Result;
- var jsonData = data.Content.ReadAsStringAsync().Result;
- var options = new JsonSerializerOptions
- {
- PropertyNameCaseInsensitive = true,
- };
- var s = JsonSerializer.Deserialize(jsonData, options);
- _infoLabel.Text = $"Created new Season: {s.Game} ({s.SeasonId})";
+ Info($"Created new Season");
}
- catch (Exception e)
+ catch (Exception ex)
{
- _infoLabel.Text = $"Exception Occured: {e.Message}";
- Console.WriteLine(e.Message);
+ _infoLabel.Text = $"Exception Occured: {ex.Message}";
+ Console.WriteLine(ex.Message);
}
}
+
+ private void SeasonsViewOnShown(object sender, EventArgs e)
+ {
+ Info("Loading Data");
+ // var seasons = Client.GetSeasons().Result;
+ // SeasonsView.Data = seasons;
+ // SeasonsView.DataBind();
+ // Info("Data Loaded");
+ }
+
+ private void LoadButtonClicked(object sender, EventArgs a)
+ {
+ var seasons = Client.GetSeasons().Result;
+ SeasonsView.Data = seasons;
+ SeasonsView.DataBind();
+ Info("List Refreshed");
+ }
+
+ private void Window_DeleteEvent(object sender, DeleteEventArgs a)
+ {
+ Application.Quit();
+ }
+
+ private void Info(string message)
+ {
+ _infoLabel.Text = message;
+ }
}
}
\ No newline at end of file
diff --git a/EstusShots.Gtk/MainWindow.glade b/EstusShots.Gtk/MainWindow.glade
index b7ec637..a3fec1e 100644
--- a/EstusShots.Gtk/MainWindow.glade
+++ b/EstusShots.Gtk/MainWindow.glade
@@ -4,9 +4,9 @@