This commit is contained in:
2020-02-26 21:50:58 +01:00
parent 78c21ade79
commit 64cdaf8e9f
15 changed files with 353 additions and 66 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EstusShots.Shared\EstusShots.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -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<List<Season>> GetSeasons()
{
var response = HttpClient.GetAsync(ApiUrl + "seasons").Result;
var jsonData = await response.Content.ReadAsStringAsync();
var data = JsonSerializer.Deserialize<List<Season>>(jsonData, _serializerOptions);
return data;
}
}
}

View File

@@ -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<Season> Data { get; set; }
public SeasonsDataStore(TreeView view)
{
ListStore = new ListStore(GType.Int, GType.String, GType.String);
Data = new List<Season>();
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<TreeViewColumn> BuildColumns()
{
var columns = new List<TreeViewColumn>
{
new TreeViewColumn("Number", new CellRendererText(), "text", 0),
new TreeViewColumn("Game", new CellRendererText(), "text", 1),
new TreeViewColumn("Start", new CellRendererText(), "text", 2)
};
return columns;
}
}
}

View File

@@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EstusShots.Client\EstusShots.Client.csproj" />
<ProjectReference Include="..\EstusShots.Shared\EstusShots.Shared.csproj" />
</ItemGroup>

View File

@@ -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<Season>(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;
}
}
}

View File

@@ -4,9 +4,9 @@
<requires lib="gtk+" version="3.18"/>
<object class="GtkWindow" id="MainWindow">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Example Window</property>
<property name="default_width">480</property>
<property name="default_height">240</property>
<property name="title" translatable="yes">Estus Shots</property>
<property name="default_width">800</property>
<property name="default_height">600</property>
<child>
<placeholder/>
</child>
@@ -14,38 +14,106 @@
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">2</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="_infoLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkTreeView" id="_seasonsView">
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="_seasonsView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="enable_grid_lines">horizontal</property>
<signal name="show" handler="SeasonsViewOnShown" swapped="no"/>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="_loadButton">
<property name="label" translatable="yes">Reload</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="_newSeasonButton">
<property name="label" translatable="yes">New Season</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">2</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="_loadButton">
<property name="label" translatable="yes">Load Seasons</property>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="can_focus">False</property>
<property name="margin_top">2</property>
<property name="spacing">5</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-connect</property>
<property name="icon_size">2</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="_infoLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>

View File

@@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Server.Models;
using EstusShots.Server.Services;
using Microsoft.AspNetCore.Mvc;
using Dto = EstusShots.Shared.Models;
namespace EstusShots.Server.Controllers
{
[ApiController]
[Route("/api/[controller]")]
public class SeasonController : ControllerBase
{
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
public SeasonController(EstusShotsContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
[HttpGet("{id}")]
public async Task<ActionResult<Dto.Season>> GetSeason(Guid id)
{
var season = await _context.Seasons.FindAsync(id);
if (season == null) return NotFound();
var seasonDto = _mapper.Map<Dto.Season>(season);
return seasonDto;
}
[HttpPost]
public async Task<ActionResult<Dto.Season>> CreateSeason(Dto.Season season)
{
var dbSeason = _mapper.Map<Season>(season);
_context.Seasons.Add(dbSeason);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetSeason), new {id = dbSeason.SeasonId}, dbSeason);
}
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Server.Services;
using EstusShots.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Dto = EstusShots.Shared.Models;
namespace EstusShots.Server.Controllers
{
@@ -11,26 +13,21 @@ namespace EstusShots.Server.Controllers
public class SeasonsController : ControllerBase
{
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
public SeasonsController(EstusShotsContext context)
public SeasonsController(EstusShotsContext context, IMapper mapper)
{
_context = context;
}
[HttpGet("{id}")]
public async Task<ActionResult<Season>> GetSeason(Guid id)
{
var season = await _context.Seasons.FindAsync(id);
if (season == null) return NotFound();
return season;
_mapper = mapper;
}
[HttpPost]
public async Task<ActionResult<Season>> CreateSeason(Season season)
[HttpGet]
public async Task<ActionResult<List<Dto.Season>>> GetSeasons()
{
_context.Seasons.Add(season);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetSeason), new {id = season.SeasonId}, season);
var seasons = await _context.Seasons.ToListAsync();
var dtos = _mapper.Map<List<Dto.Season>>(seasons);
return dtos;
}
}
}

View File

@@ -14,6 +14,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using EstusShots.Server.Models;
namespace EstusShots.Server.Mapping
{
public class Profiles : Profile
{
public Profiles()
{
CreateMap<Season, Shared.Models.Season>();
CreateMap<Shared.Models.Season, Season>();
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace EstusShots.Server.Models
{
public class Season
{
public Guid SeasonId { get; set; }
public int Number { get; set; }
[MaxLength(50)] public string Game { get; set; } = default!;
public string? Description { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
}
}

View File

@@ -1,4 +1,4 @@
using EstusShots.Shared.Models;
using EstusShots.Server.Models;
using Microsoft.EntityFrameworkCore;
namespace EstusShots.Server.Services

View File

@@ -1,3 +1,4 @@
using AutoMapper;
using EstusShots.Server.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@@ -22,6 +23,7 @@ namespace EstusShots.Server
{
services.AddDbContext<EstusShotsContext>(
opt => opt.UseInMemoryDatabase("debug.db"));
services.AddAutoMapper(typeof(Startup));
services.AddControllers();
}

View File

@@ -1,5 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace EstusShots.Shared.Models
{
@@ -7,12 +6,16 @@ namespace EstusShots.Shared.Models
{
public Guid SeasonId { get; set; }
[MaxLength(50)] public string Game { get; set; } = default!;
public int Number { get; set; }
public string Game { get; set; } = default!;
public string? Description { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
public string DisplayName => $"S{Number:D2} {Game}";
}
}

View File

@@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EstusShots.Shared", "EstusS
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EstusShots.Gtk", "EstusShots.Gtk\EstusShots.Gtk.csproj", "{165F3B5C-9802-46C5-BCE2-F40C42819045}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EstusShots.Client", "EstusShots.Client\EstusShots.Client.csproj", "{9D2CCB23-6232-4587-AA95-24ADA96569E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -24,5 +26,9 @@ Global
{165F3B5C-9802-46C5-BCE2-F40C42819045}.Debug|Any CPU.Build.0 = Debug|Any CPU
{165F3B5C-9802-46C5-BCE2-F40C42819045}.Release|Any CPU.ActiveCfg = Release|Any CPU
{165F3B5C-9802-46C5-BCE2-F40C42819045}.Release|Any CPU.Build.0 = Release|Any CPU
{9D2CCB23-6232-4587-AA95-24ADA96569E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D2CCB23-6232-4587-AA95-24ADA96569E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D2CCB23-6232-4587-AA95-24ADA96569E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D2CCB23-6232-4587-AA95-24ADA96569E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal