Add episodes controller

This commit is contained in:
2020-02-29 16:01:22 +01:00
parent 0cef2e3df2
commit 54c0fb2fd2
10 changed files with 190 additions and 23 deletions

View File

@@ -1,38 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Server.Services;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Dto = EstusShots.Shared.Dto;
namespace EstusShots.Server.Controllers
{
[ApiController]
[Route("/api/[controller]")]
public class EpisodesController : ControllerBase
{
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
[Route("/api/[controller]/[action]")]
public class EpisodesController : ControllerBase, IEpisodesController
{
private readonly ILogger _logger;
private readonly EpisodesService _episodesService;
public EpisodesController(ILogger<EpisodesController> logger, IMapper mapper, EstusShotsContext context)
public EpisodesController(ILogger<EpisodesController> logger, EpisodesService episodesService)
{
_logger = logger;
_mapper = mapper;
_context = context;
_episodesService = episodesService;
}
[HttpGet("seasonId")]
public async Task<ActionResult<List<Dto.Episode>>> GetEpisodes(Guid seasonId)
{
_logger.LogDebug($"All");
var episodes = await _context.Episodes.Where(x => x.SeasonId == seasonId).ToListAsync();
var dtos = _mapper.Map<List<Dto.Episode>>(episodes);
return dtos;
public async Task<ApiResponse<GetEpisodesResponse>> GetEpisodes(GetEpisodesParameter parameter)
{
try
{
_logger.LogInformation($"Request received from client '{Request.HttpContext.Connection.RemoteIpAddress}'");
return await _episodesService.GetEpisodes(parameter);
}
catch (Exception e)
{
_logger.LogError(e, "Exception Occured");
return new ApiResponse<GetEpisodesResponse>(new OperationResult(e));
}
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Net;
using EstusShots.Shared.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace EstusShots.Server.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
var code = HttpStatusCode.InternalServerError;
context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.StatusCode = (int)code;
var response = new ApiResponse<CriticalErrorResponse>(new OperationResult(false, "Critical Server Error", context.Exception.Message));
context.Result = new JsonResult(response);
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using EstusShots.Shared.Interfaces;
using EstusShots.Shared.Models;
using EstusShots.Shared.Models.Parameters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Dto = EstusShots.Shared.Dto;
namespace EstusShots.Server.Services
{
public class EpisodesService : IEpisodesController
{
private readonly ILogger _logger;
private readonly EstusShotsContext _context;
private readonly IMapper _mapper;
public EpisodesService(ILogger<EpisodesService> logger, EstusShotsContext context, IMapper mapper)
{
_logger = logger;
_context = context;
_mapper = mapper;
}
public async Task<ApiResponse<GetEpisodesResponse>> GetEpisodes(GetEpisodesParameter parameter)
{
var episodes = await _context.Episodes
.Where(x => x.SeasonId == parameter.SeasonId)
.ToListAsync();
var dtos = _mapper.Map<List<Dto.Episode>>(episodes);
_logger.LogInformation($"{dtos.Count} episodes loaded for season '{parameter.SeasonId}'");
return new ApiResponse<GetEpisodesResponse>(new GetEpisodesResponse(dtos));
}
}
}

View File

@@ -29,6 +29,7 @@ namespace EstusShots.Server
services.AddAutoMapper(typeof(Startup));
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
if (IsDevelopment)
{
options.JsonSerializerOptions.WriteIndented = true;
@@ -38,7 +39,10 @@ namespace EstusShots.Server
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Estus Shots API", Version = "v1" });
});
// Register business logic services
services.AddScoped<SeasonsService>();
services.AddScoped<EpisodesService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -65,6 +69,7 @@ namespace EstusShots.Server
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}