begin implementing episodes
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using server.Models;
|
||||
using server.Persistence.Repositories;
|
||||
|
||||
namespace server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("/api/episodes")]
|
||||
[Produces("application/json")]
|
||||
public class EpisodesController : ControllerBase
|
||||
{
|
||||
private readonly IEpisodeRepository _episodeRepository;
|
||||
|
||||
public EpisodesController(IEpisodeRepository episodeRepository)
|
||||
{
|
||||
_episodeRepository = episodeRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<Episode>), 200)]
|
||||
[ProducesResponseType(500)]
|
||||
public async Task<ActionResult<List<Episode>>> GetEpisodesAsync([FromQuery]EpisodeFilter? filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
var seasons = await _episodeRepository.GetEpisodesAsync(filter);
|
||||
#pragma warning restore CS8604 // Possible null reference argument.
|
||||
return Ok(seasons);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get episodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using server.Persistence.Repositories;
|
||||
namespace server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/seasons")]
|
||||
[Route("/api/seasons")]
|
||||
[Produces("application/json")]
|
||||
public class SeasonsController : ControllerBase
|
||||
{
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Models
|
||||
{
|
||||
public class EpisodeFilter
|
||||
{
|
||||
public int? Season { get; set; } = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public class EpisodeRepository : IEpisodeRepository
|
||||
{
|
||||
private readonly IDbContext _context;
|
||||
|
||||
public EpisodeRepository(IDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<Episode>> GetEpisodesAsync(EpisodeFilter? filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _context.Episodes.AsQueryable();
|
||||
|
||||
if (filter?.Season != null)
|
||||
{
|
||||
query = query.Where(episode => episode.Season == filter.Season);
|
||||
}
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public interface IEpisodeRepository
|
||||
{
|
||||
Task<List<Episode>> GetEpisodesAsync(EpisodeFilter filter);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ builder.Services.AddSingleton<IDbContext, DbContext>();
|
||||
|
||||
builder.Services.AddScoped<ISeasonRepository, SeasonRepository>();
|
||||
|
||||
builder.Services.AddScoped<IEpisodeRepository, EpisodeRepository>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.16.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||
|
||||
Reference in New Issue
Block a user