Add code generation.

Replaces the client and server controller implementations with generated code from the shared project.
This commit is contained in:
2020-03-13 20:16:53 +01:00
parent 7bd44091cf
commit 42bca48742
29 changed files with 542 additions and 499 deletions

View File

@@ -12,4 +12,17 @@
<PackageReference Include="Z.ExtensionMethods" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<None Update="Routes.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Routes.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Compile Update="Routes.cs">
<DependentUpon>Routes.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>

View File

@@ -3,14 +3,12 @@ using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using EstusShots.Client.Routes;
using EstusShots.Shared.Dto;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
namespace EstusShots.Client
{
public class EstusShotsClient
public partial class EstusShotsClient
{
private readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
@@ -20,13 +18,6 @@ namespace EstusShots.Client
public string ApiUrl { get; }
// API Routes
public Seasons Seasons { get; }
public Episodes Episodes { get; }
public Players Players { get; }
public Drinks Drinks { get; }
public Enemies Enemies { get; }
/// <summary>
/// Creates a new instance of <see cref="EstusShotsClient"/>
/// </summary>
@@ -35,12 +26,7 @@ namespace EstusShots.Client
{
ApiUrl = apiUrl;
HttpClient = new HttpClient {Timeout = TimeSpan.FromSeconds(10)};
Seasons = new Seasons(this);
Episodes = new Episodes(this);
Players = new Players(this);
Drinks = new Drinks(this);
Enemies = new Enemies(this);
CreateApiRoutes();
}
/// <summary>

141
EstusShots.Client/Routes.cs Normal file
View File

@@ -0,0 +1,141 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Threading.Tasks;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
using EstusShots.Shared.Interfaces.Controllers;
namespace EstusShots.Client
{
// Generated part of the client class
public partial class EstusShotsClient
{
public Drinks Drinks { get; internal set; }
public Enemies Enemies { get; internal set; }
public Episodes Episodes { get; internal set; }
public Players Players { get; internal set; }
public Seasons Seasons { get; internal set; }
private void CreateApiRoutes()
{
Drinks = new Drinks(this);
Enemies = new Enemies(this);
Episodes = new Episodes(this);
Players = new Players(this);
Seasons = new Seasons(this);
}
}
public class Drinks : IDrinksController
{
private readonly EstusShotsClient _client;
public Drinks(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetDrinksResponse>> GetDrinks(GetDrinksParameter parameter)
=> await _client.PostToApi<GetDrinksResponse, GetDrinksParameter>($"{_client.ApiUrl}Drinks/GetDrinks", parameter);
public async Task<ApiResponse<GetDrinkDetailsResponse>> GetDrinkDetails(GetDrinkDetailsParameter parameter)
=> await _client.PostToApi<GetDrinkDetailsResponse, GetDrinkDetailsParameter>($"{_client.ApiUrl}Drinks/GetDrinkDetails", parameter);
public async Task<ApiResponse<SaveDrinkResponse>> SaveDrink(SaveDrinkParameter parameter)
=> await _client.PostToApi<SaveDrinkResponse, SaveDrinkParameter>($"{_client.ApiUrl}Drinks/SaveDrink", parameter);
}
public class Enemies : IEnemiesController
{
private readonly EstusShotsClient _client;
public Enemies(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetEnemiesResponse>> GetEnemies(GetEnemiesParameter parameter)
=> await _client.PostToApi<GetEnemiesResponse, GetEnemiesParameter>($"{_client.ApiUrl}Enemies/GetEnemies", parameter);
public async Task<ApiResponse<GetEnemyResponse>> GetEnemy(GetEnemyParameter parameter)
=> await _client.PostToApi<GetEnemyResponse, GetEnemyParameter>($"{_client.ApiUrl}Enemies/GetEnemy", parameter);
public async Task<ApiResponse<SaveEnemyResponse>> SaveEnemy(SaveEnemyParameter parameter)
=> await _client.PostToApi<SaveEnemyResponse, SaveEnemyParameter>($"{_client.ApiUrl}Enemies/SaveEnemy", parameter);
public async Task<ApiResponse<DeleteEnemyResponse>> DeleteEnemy(DeleteEnemyParameter parameter)
=> await _client.PostToApi<DeleteEnemyResponse, DeleteEnemyParameter>($"{_client.ApiUrl}Enemies/DeleteEnemy", parameter);
}
public class Episodes : IEpisodesController
{
private readonly EstusShotsClient _client;
public Episodes(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetEpisodesResponse>> GetEpisodes(GetEpisodesParameter parameter)
=> await _client.PostToApi<GetEpisodesResponse, GetEpisodesParameter>($"{_client.ApiUrl}Episodes/GetEpisodes", parameter);
public async Task<ApiResponse<GetEpisodeResponse>> GetEpisode(GetEpisodeParameter parameter)
=> await _client.PostToApi<GetEpisodeResponse, GetEpisodeParameter>($"{_client.ApiUrl}Episodes/GetEpisode", parameter);
public async Task<ApiResponse<SaveEpisodeResponse>> SaveEpisode(SaveEpisodeParameter parameter)
=> await _client.PostToApi<SaveEpisodeResponse, SaveEpisodeParameter>($"{_client.ApiUrl}Episodes/SaveEpisode", parameter);
}
public class Players : IPlayersController
{
private readonly EstusShotsClient _client;
public Players(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetPlayersResponse>> GetPlayers(GetPlayersParameter parameter)
=> await _client.PostToApi<GetPlayersResponse, GetPlayersParameter>($"{_client.ApiUrl}Players/GetPlayers", parameter);
public async Task<ApiResponse<GetPlayerDetailsResponse>> GetPlayerDetails(GetPlayerDetailsParameter parameter)
=> await _client.PostToApi<GetPlayerDetailsResponse, GetPlayerDetailsParameter>($"{_client.ApiUrl}Players/GetPlayerDetails", parameter);
public async Task<ApiResponse<SavePlayerResponse>> SavePlayer(SavePlayerParameter parameter)
=> await _client.PostToApi<SavePlayerResponse, SavePlayerParameter>($"{_client.ApiUrl}Players/SavePlayer", parameter);
public async Task<ApiResponse<DeletePlayerResponse>> DeletePlayer(DeletePlayerParameter parameter)
=> await _client.PostToApi<DeletePlayerResponse, DeletePlayerParameter>($"{_client.ApiUrl}Players/DeletePlayer", parameter);
}
public class Seasons : ISeasonsController
{
private readonly EstusShotsClient _client;
public Seasons(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetSeasonsResponse>> GetSeasons(GetSeasonsParameter parameter)
=> await _client.PostToApi<GetSeasonsResponse, GetSeasonsParameter>($"{_client.ApiUrl}Seasons/GetSeasons", parameter);
public async Task<ApiResponse<GetSeasonResponse>> GetSeason(GetSeasonParameter parameter)
=> await _client.PostToApi<GetSeasonResponse, GetSeasonParameter>($"{_client.ApiUrl}Seasons/GetSeason", parameter);
public async Task<ApiResponse<SaveSeasonResponse>> SaveSeason(SaveSeasonParameter parameter)
=> await _client.PostToApi<SaveSeasonResponse, SaveSeasonParameter>($"{_client.ApiUrl}Seasons/SaveSeason", parameter);
}
}

View File

@@ -0,0 +1,91 @@
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)EstusShots.Shared/bin/Debug/netcoreapp3.1/EstusShots.Shared.dll" #>
<#@ import namespace="Shared.Interfaces" #>
<#@ import namespace="System.Linq" #>
<#
var assembly = typeof(IApiParameter).Assembly;
var types = assembly.GetTypes()
.Where(t => t.Namespace?.StartsWith("EstusShots.Shared") == true)
.ToArray();
var controllers = types
.Where(t => t.Namespace == "EstusShots.Shared.Interfaces.Controllers")
.ToArray();
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Threading.Tasks;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
using EstusShots.Shared.Interfaces.Controllers;
namespace EstusShots.Client
{
// Generated part of the client class
public partial class EstusShotsClient
{
<#
// Create API client route properties
foreach (var iFace in controllers)
{
var className = iFace.Name.Substring(1).Replace("Controller", "");
#>
public <#=className#> <#=className#> { get; internal set; }
<#
}
#>
private void CreateApiRoutes()
{
<#
// Instantiate API client routes
foreach (var iFace in controllers)
{
var className = iFace.Name.Substring(1).Replace("Controller", "");
#>
<#=className#> = new <#=className#>(this);
<#
}
#>
}
}
<#
foreach (var iFace in controllers)
{
var className = iFace.Name.Substring(1).Replace("Controller", "");
#>
public class <#=className#> : <#=iFace.Name #>
{
private readonly EstusShotsClient _client;
public <#=className#>(EstusShotsClient client)
{
_client = client;
}
<#
foreach (var member in iFace.GetMembers())
{
var responseName = $"{member.Name}Response";
var parameterName = $"{member.Name}Parameter";
#>
public async Task<ApiResponse<<#=responseName#>>> <#=member.Name#>(<#=parameterName#> parameter)
=> await _client.PostToApi<<#=responseName#>, <#=parameterName#>>($"{_client.ApiUrl}<#=className#>/<#=member.Name#>", parameter);
<#
}
#>
}
<#
}
#>
}

View File

@@ -1,34 +0,0 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
namespace EstusShots.Client.Routes
{
/// <inheritdoc/>
public class Drinks : IDrinksController
{
private readonly EstusShotsClient _client;
public Drinks(EstusShotsClient client)
{
_client = client;
}
private string ActionUrl([CallerMemberName]string caller = "") =>
$"{_client.ApiUrl}{GetType().Name}/{caller}";
/// <inheritdoc/>
public async Task<ApiResponse<GetDrinksResponse>> GetDrinks(GetDrinksParameter parameter)
=> await _client.PostToApi<GetDrinksResponse, GetDrinksParameter>(ActionUrl(), parameter);
/// <inheritdoc/>
public async Task<ApiResponse<GetDrinkDetailsResponse>> GetDrinkDetails(GetDrinkDetailsParameter parameter)
=> await _client.PostToApi<GetDrinkDetailsResponse, GetDrinkDetailsParameter>(ActionUrl(), parameter);
/// <inheritdoc/>
public async Task<ApiResponse<SaveDrinkResponse>> SaveDrink(SaveDrinkParameter parameter)
=> await _client.PostToApi<SaveDrinkResponse, SaveDrinkParameter>(ActionUrl(), parameter);
}
}

View File

@@ -1,33 +0,0 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
namespace EstusShots.Client.Routes
{
public class Enemies : IEnemiesController
{
private readonly EstusShotsClient _client;
public Enemies(EstusShotsClient client)
{
_client = client;
}
public async Task<ApiResponse<GetEnemiesResponse>> GetEnemies(GetEnemiesParameter parameter)
=> await _client.PostToApi<GetEnemiesResponse, GetEnemiesParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<GetEnemyResponse>> GetEnemy(GetEnemyParameter parameter)
=> await _client.PostToApi<GetEnemyResponse, GetEnemyParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<SaveEnemyResponse>> SaveEnemy(SaveEnemyParameter parameter)
=> await _client.PostToApi<SaveEnemyResponse, SaveEnemyParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<DeleteEnemyResponse>> DeleteEnemy(DeleteEnemyParameter parameter)
=> await _client.PostToApi<DeleteEnemyResponse, DeleteEnemyParameter>(ActionUrl(), parameter);
private string ActionUrl([CallerMemberName]string caller = "") =>
$"{_client.ApiUrl}{GetType().Name}/{caller}";
}
}

View File

@@ -1,30 +0,0 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
namespace EstusShots.Client.Routes
{
public class Episodes : IEpisodesController
{
private readonly EstusShotsClient _client;
public Episodes(EstusShotsClient client)
{
_client = client;
}
private string ActionUrl([CallerMemberName]string caller = "") =>
$"{_client.ApiUrl}{nameof(Episodes)}/{caller}";
public async Task<ApiResponse<GetEpisodesResponse>> GetEpisodes(GetEpisodesParameter parameter) =>
await _client.PostToApi<GetEpisodesResponse, GetEpisodesParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<GetEpisodeResponse>> GetEpisode(GetEpisodeParameter parameter) =>
await _client.PostToApi<GetEpisodeResponse, GetEpisodeParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<SaveEpisodeResponse>> SaveEpisode(SaveEpisodeParameter parameter) =>
await _client.PostToApi<SaveEpisodeResponse, SaveEpisodeParameter>(ActionUrl(), parameter);
}
}

View File

@@ -1,32 +0,0 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
namespace EstusShots.Client.Routes
{
public class Players : IPlayersController
{
private readonly EstusShotsClient _client;
public Players(EstusShotsClient client)
{
_client = client;
}
private string ActionUrl([CallerMemberName]string caller = "") =>
$"{_client.ApiUrl}{GetType().Name}/{caller}";
public async Task<ApiResponse<GetPlayersResponse>> GetPlayers(GetPlayersParameter parameter)
=> await _client.PostToApi<GetPlayersResponse, GetPlayersParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<GetPlayerDetailsResponse>> GetPlayerDetails(GetPlayerDetailsParameter parameter)
=> await _client.PostToApi<GetPlayerDetailsResponse, GetPlayerDetailsParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<SavePlayerResponse>> SavePlayer(SavePlayerParameter parameter)
=> await _client.PostToApi<SavePlayerResponse, SavePlayerParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<DeletePlayerResponse>> DeletePlayers(DeletePlayerParameter parameter)
=> await _client.PostToApi<DeletePlayerResponse, DeletePlayerParameter>(ActionUrl(), parameter);
}
}

View File

@@ -1,30 +0,0 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
namespace EstusShots.Client.Routes
{
public class Seasons : ISeasonsController
{
private readonly EstusShotsClient _client;
public Seasons(EstusShotsClient client)
{
_client = client;
}
private string ActionUrl([CallerMemberName]string caller = "") =>
$"{_client.ApiUrl}{nameof(Seasons)}/{caller}";
public async Task<ApiResponse<GetSeasonsResponse>> GetSeasons(GetSeasonsParameter parameter) =>
await _client.PostToApi<GetSeasonsResponse, GetSeasonsParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<GetSeasonResponse>> GetSeason(GetSeasonParameter parameter) =>
await _client.PostToApi<GetSeasonResponse, GetSeasonParameter>(ActionUrl(), parameter);
public async Task<ApiResponse<SaveSeasonResponse>> SaveSeason(SaveSeasonParameter parameter)=>
await _client.PostToApi<SaveSeasonResponse, SaveSeasonParameter>(ActionUrl(), parameter);
}
}