Drinks: models and boilerplate
This commit is contained in:
@@ -24,6 +24,7 @@ namespace EstusShots.Client
|
|||||||
public Seasons Seasons { get; }
|
public Seasons Seasons { get; }
|
||||||
public Episodes Episodes { get; }
|
public Episodes Episodes { get; }
|
||||||
public Players Players { get; }
|
public Players Players { get; }
|
||||||
|
public Drinks Drinks { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="EstusShotsClient"/>
|
/// Creates a new instance of <see cref="EstusShotsClient"/>
|
||||||
@@ -37,6 +38,7 @@ namespace EstusShots.Client
|
|||||||
Seasons = new Seasons(this);
|
Seasons = new Seasons(this);
|
||||||
Episodes = new Episodes(this);
|
Episodes = new Episodes(this);
|
||||||
Players = new Players(this);
|
Players = new Players(this);
|
||||||
|
Drinks = new Drinks(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
34
EstusShots.Client/Routes/Drinks.cs
Normal file
34
EstusShots.Client/Routes/Drinks.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
54
EstusShots.Server/Controllers/DrinksController.cs
Normal file
54
EstusShots.Server/Controllers/DrinksController.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using EstusShots.Server.Services;
|
||||||
|
using EstusShots.Shared.Interfaces;
|
||||||
|
using EstusShots.Shared.Models;
|
||||||
|
using EstusShots.Shared.Models.Parameters;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace EstusShots.Server.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("/api/[controller]/[action]")]
|
||||||
|
public class DrinksController : ControllerBase, IDrinksController
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly DrinksService _service;
|
||||||
|
|
||||||
|
public DrinksController(ILogger<DrinksController> logger, DrinksService drinksService)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_service = drinksService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ApiResponse<GetDrinksResponse>> GetDrinks(GetDrinksParameter parameter)
|
||||||
|
=> await ServiceCall(() => _service.GetDrinks(parameter));
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ApiResponse<GetDrinkDetailsResponse>> GetDrinkDetails(GetDrinkDetailsParameter parameter)
|
||||||
|
=> await ServiceCall(() => _service.GetDrinkDetails(parameter));
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ApiResponse<SaveDrinkResponse>> SaveDrink(SaveDrinkParameter parameter)
|
||||||
|
=> await ServiceCall(() => _service.SaveDrink(parameter));
|
||||||
|
|
||||||
|
private async Task<ApiResponse<T>> ServiceCall<T>(Func<Task<ApiResponse<T>>> serviceCall)
|
||||||
|
where T : class, IApiResponse, new()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid) _logger.LogError($"Model invalid");
|
||||||
|
_logger.LogInformation(
|
||||||
|
$"Request received from client '{Request.HttpContext.Connection.RemoteIpAddress}'");
|
||||||
|
return await serviceCall();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Exception Occured");
|
||||||
|
return new ApiResponse<T>(new OperationResult(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,9 @@ namespace EstusShots.Server.Mapping
|
|||||||
|
|
||||||
CreateMap<Player, Shared.Dto.Player>();
|
CreateMap<Player, Shared.Dto.Player>();
|
||||||
CreateMap<Shared.Dto.Player, Player>();
|
CreateMap<Shared.Dto.Player, Player>();
|
||||||
|
|
||||||
|
CreateMap<Drink, Shared.Dto.Drink>();
|
||||||
|
CreateMap<Shared.Dto.Drink, Drink>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
13
EstusShots.Server/Models/Drink.cs
Normal file
13
EstusShots.Server/Models/Drink.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace EstusShots.Server.Models
|
||||||
|
{
|
||||||
|
public class Drink
|
||||||
|
{
|
||||||
|
public Guid DrinkId { get; set; }
|
||||||
|
|
||||||
|
public string Name { get; set; } = default!;
|
||||||
|
|
||||||
|
public double Vol { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
25
EstusShots.Server/Services/DrinksService.cs
Normal file
25
EstusShots.Server/Services/DrinksService.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using EstusShots.Shared.Interfaces;
|
||||||
|
using EstusShots.Shared.Models;
|
||||||
|
using EstusShots.Shared.Models.Parameters;
|
||||||
|
|
||||||
|
namespace EstusShots.Server.Services
|
||||||
|
{
|
||||||
|
public class DrinksService : IDrinksController
|
||||||
|
{
|
||||||
|
public Task<ApiResponse<GetDrinksResponse>> GetDrinks(GetDrinksParameter parameter)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<ApiResponse<GetDrinkDetailsResponse>> GetDrinkDetails(GetDrinkDetailsParameter parameter)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<ApiResponse<SaveDrinkResponse>> SaveDrink(SaveDrinkParameter parameter)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ namespace EstusShots.Server.Services
|
|||||||
public DbSet<Season> Seasons { get; set; } = default!;
|
public DbSet<Season> Seasons { get; set; } = default!;
|
||||||
public DbSet<Episode> Episodes { get; set; } = default!;
|
public DbSet<Episode> Episodes { get; set; } = default!;
|
||||||
public DbSet<Player> Players { get; set; } = default!;
|
public DbSet<Player> Players { get; set; } = default!;
|
||||||
|
public DbSet<Drink> Drinks { get; set; } = default!;
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ namespace EstusShots.Server
|
|||||||
services.AddScoped<SeasonsService>();
|
services.AddScoped<SeasonsService>();
|
||||||
services.AddScoped<EpisodesService>();
|
services.AddScoped<EpisodesService>();
|
||||||
services.AddScoped<PlayersService>();
|
services.AddScoped<PlayersService>();
|
||||||
|
services.AddScoped<DrinksService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
|||||||
13
EstusShots.Shared/Dto/Drink.cs
Normal file
13
EstusShots.Shared/Dto/Drink.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace EstusShots.Shared.Dto
|
||||||
|
{
|
||||||
|
public class Drink
|
||||||
|
{
|
||||||
|
public Guid DrinkId { get; set; }
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public double Vol { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
30
EstusShots.Shared/Interfaces/IDrinksController.cs
Normal file
30
EstusShots.Shared/Interfaces/IDrinksController.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using EstusShots.Shared.Models;
|
||||||
|
using EstusShots.Shared.Models.Parameters;
|
||||||
|
|
||||||
|
namespace EstusShots.Shared.Interfaces
|
||||||
|
{
|
||||||
|
public interface IDrinksController
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Load all drinks from the database
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameter">An <see cref="GetDrinksParameter"/> instance</param>
|
||||||
|
/// <returns>An ApiResponse instance of type <see cref="GetDrinksResponse"/></returns>
|
||||||
|
Task<ApiResponse<GetDrinksResponse>> GetDrinks(GetDrinksParameter parameter);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Load detailed information on a single drink
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameter">An <see cref="GetDrinkDetailsParameter"/> instance</param>
|
||||||
|
/// <returns>An ApiResponse instance of type <see cref="GetDrinkDetailsResponse"/></returns>
|
||||||
|
Task<ApiResponse<GetDrinkDetailsResponse>> GetDrinkDetails(GetDrinkDetailsParameter parameter);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates or Updates a drink object
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parameter">An <see cref="SaveDrinkParameter"/> instance</param>
|
||||||
|
/// <returns>An ApiResponse instance of type <see cref="SaveDrinkResponse"/></returns>
|
||||||
|
Task<ApiResponse<SaveDrinkResponse>> SaveDrink(SaveDrinkParameter parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
129
EstusShots.Shared/Models/Parameters/DrinkParameters.cs
Normal file
129
EstusShots.Shared/Models/Parameters/DrinkParameters.cs
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using EstusShots.Shared.Dto;
|
||||||
|
using EstusShots.Shared.Interfaces;
|
||||||
|
|
||||||
|
namespace EstusShots.Shared.Models.Parameters
|
||||||
|
{
|
||||||
|
# region GetDrinks
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class for the GetDrinks API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class GetDrinksParameter : IApiParameter
|
||||||
|
{
|
||||||
|
public GetDrinksParameter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class returned from the GetDrinks API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class GetDrinksResponse : IApiResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// List of all drinks in the database
|
||||||
|
/// </summary>
|
||||||
|
public List<Drink> Drinks { get; set; }
|
||||||
|
|
||||||
|
public GetDrinksResponse(List<Drink> drinks)
|
||||||
|
{
|
||||||
|
Drinks = drinks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GetDrinksResponse()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# endregion
|
||||||
|
|
||||||
|
# region GetDrinkDetails
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class for the GetDrinkDetails API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class GetDrinkDetailsParameter : IApiParameter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ID of the drink for which to load details
|
||||||
|
/// </summary>
|
||||||
|
public Guid DrinkId { get; set; }
|
||||||
|
|
||||||
|
public GetDrinkDetailsParameter(Guid drinkId)
|
||||||
|
{
|
||||||
|
DrinkId = drinkId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GetDrinkDetailsParameter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class returned from the GetDrinkDetails API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class GetDrinkDetailsResponse : IApiResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Detailed information on a drink
|
||||||
|
/// </summary>
|
||||||
|
public Drink Drink { get; set; }
|
||||||
|
|
||||||
|
public GetDrinkDetailsResponse(Drink drink)
|
||||||
|
{
|
||||||
|
Drink = drink;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GetDrinkDetailsResponse()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# endregion
|
||||||
|
|
||||||
|
# region SaveDrink
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class for the SaveDrink API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class SaveDrinkParameter : IApiParameter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The object to save in the database
|
||||||
|
/// </summary>
|
||||||
|
public Drink Drink { get; set; }
|
||||||
|
|
||||||
|
public SaveDrinkParameter(Drink drink)
|
||||||
|
{
|
||||||
|
Drink = drink;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SaveDrinkParameter()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter class returned from the SaveDrink API controller.
|
||||||
|
/// </summary>
|
||||||
|
public class SaveDrinkResponse : IApiResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ID of the created or updated drink object
|
||||||
|
/// </summary>
|
||||||
|
public Guid DrinkId { get; set; }
|
||||||
|
|
||||||
|
public SaveDrinkResponse(Guid drinkId)
|
||||||
|
{
|
||||||
|
DrinkId = drinkId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SaveDrinkResponse()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# endregion
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user