Implement unified API controller pattern
This commit is contained in:
@@ -8,9 +8,9 @@ namespace EstusShots.Shared.Dto
|
||||
|
||||
public int Number { get; set; }
|
||||
|
||||
public string Game { get; set; } = default!;
|
||||
public string Game { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
public DateTime Start { get; set; }
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
|
||||
12
EstusShots.Shared/Interfaces/IApiParameter.cs
Normal file
12
EstusShots.Shared/Interfaces/IApiParameter.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace EstusShots.Shared.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks a class that can be used as a parameter for API requests
|
||||
/// </summary>
|
||||
public interface IApiParameter { }
|
||||
|
||||
/// <summary>
|
||||
/// Marks a class as response type that is returned from API requests
|
||||
/// </summary>
|
||||
public interface IApiResponse { }
|
||||
}
|
||||
23
EstusShots.Shared/Interfaces/ISeasonsController.cs
Normal file
23
EstusShots.Shared/Interfaces/ISeasonsController.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Threading.Tasks;
|
||||
using EstusShots.Shared.Models;
|
||||
using EstusShots.Shared.Models.Parameters;
|
||||
|
||||
namespace EstusShots.Shared.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Access many seasons with one request
|
||||
/// </summary>
|
||||
public interface ISeasonsController
|
||||
{
|
||||
/// <summary>
|
||||
/// Get a list of all seasons in the system
|
||||
/// </summary>
|
||||
/// <param name="parameter"></param>
|
||||
/// <returns></returns>
|
||||
Task<ApiResponse<GetSeasonsResponse>> GetSeasons(GetSeasonsParameter parameter);
|
||||
|
||||
Task<ApiResponse<GetSeasonResponse>> GetSeason(GetSeasonParameter parameter);
|
||||
|
||||
Task<ApiResponse<SaveSeasonResponse>> SaveSeason(SaveSeasonParameter parameter);
|
||||
}
|
||||
}
|
||||
@@ -29,4 +29,35 @@ namespace EstusShots.Shared.Models
|
||||
StackTrace = e.StackTrace;
|
||||
}
|
||||
}
|
||||
|
||||
public class ApiResponse<T> where T : class, new()
|
||||
{
|
||||
public OperationResult OperationResult { get; set; }
|
||||
|
||||
public T Data { get; set; }
|
||||
|
||||
public ApiResponse()
|
||||
{
|
||||
OperationResult = new OperationResult();
|
||||
Data = new T();
|
||||
}
|
||||
|
||||
public ApiResponse(OperationResult operationResult)
|
||||
{
|
||||
OperationResult = operationResult;
|
||||
Data = new T();
|
||||
}
|
||||
|
||||
public ApiResponse(T data)
|
||||
{
|
||||
OperationResult = new OperationResult();
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public ApiResponse(OperationResult operationResult, T data)
|
||||
{
|
||||
OperationResult = operationResult;
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
EstusShots.Shared/Models/Parameters/SeasonParameters.cs
Normal file
124
EstusShots.Shared/Models/Parameters/SeasonParameters.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using EstusShots.Shared.Dto;
|
||||
using EstusShots.Shared.Interfaces;
|
||||
|
||||
namespace EstusShots.Shared.Models.Parameters
|
||||
{
|
||||
// GetSeasons
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class for loading a list of all seasons from the database
|
||||
/// </summary>
|
||||
public class GetSeasonsParameter : IApiParameter { }
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class returned from the API with all loaded seasons
|
||||
/// </summary>
|
||||
public class GetSeasonsResponse : IApiResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// All existing seasons in the database
|
||||
/// </summary>
|
||||
public List<Season> Seasons { get; set; }
|
||||
|
||||
public GetSeasonsResponse(List<Season> seasons)
|
||||
{
|
||||
Seasons = seasons;
|
||||
}
|
||||
|
||||
public GetSeasonsResponse()
|
||||
{
|
||||
Seasons = new List<Season>();
|
||||
}
|
||||
}
|
||||
|
||||
// GetSeason
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class for loading a single season object
|
||||
/// </summary>
|
||||
public class GetSeasonParameter : IApiParameter
|
||||
{
|
||||
/// <summary>
|
||||
/// ID of the season that should be loaded
|
||||
/// </summary>
|
||||
public Guid SeasonId { get; set; }
|
||||
|
||||
public GetSeasonParameter()
|
||||
{
|
||||
SeasonId = Guid.Empty;
|
||||
}
|
||||
|
||||
public GetSeasonParameter(Guid seasonId)
|
||||
{
|
||||
SeasonId = seasonId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class returned from the API after loading a single season object
|
||||
/// </summary>
|
||||
public class GetSeasonResponse : IApiResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// The loaded season
|
||||
/// </summary>
|
||||
public Season Season { get; set; }
|
||||
|
||||
public GetSeasonResponse()
|
||||
{
|
||||
Season = new Season();
|
||||
}
|
||||
|
||||
public GetSeasonResponse(Season season)
|
||||
{
|
||||
Season = season;
|
||||
}
|
||||
}
|
||||
|
||||
// SaveSeason
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class for saving season objects
|
||||
/// </summary>
|
||||
public class SaveSeasonParameter : IApiParameter
|
||||
{
|
||||
/// <summary>
|
||||
/// The season object that should be saved
|
||||
/// </summary>
|
||||
public Season Season { get; set; }
|
||||
|
||||
public SaveSeasonParameter()
|
||||
{
|
||||
Season = new Season();
|
||||
}
|
||||
|
||||
public SaveSeasonParameter(Season season)
|
||||
{
|
||||
Season = season;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameter class returned from the API after saving a season object
|
||||
/// </summary>
|
||||
public class SaveSeasonResponse : IApiResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// ID of the season that was updated or created
|
||||
/// </summary>
|
||||
public Guid SeasonId { get; set; }
|
||||
|
||||
public SaveSeasonResponse()
|
||||
{
|
||||
SeasonId = Guid.Empty;
|
||||
}
|
||||
|
||||
public SaveSeasonResponse(Guid seasonId)
|
||||
{
|
||||
SeasonId = seasonId;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user