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

@@ -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));
}
}
}