add changes
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using server.Models;
|
||||
using server.Persistence.Repositories;
|
||||
|
||||
namespace server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/seasons")]
|
||||
[Produces("application/json")]
|
||||
public class SeasonsController : ControllerBase
|
||||
{
|
||||
private readonly ISeasonRepository _seasonRepository;
|
||||
|
||||
public SeasonsController(ISeasonRepository seasonRepository)
|
||||
{
|
||||
_seasonRepository = seasonRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<Season>), 200)]
|
||||
[ProducesResponseType(500)]
|
||||
public async Task<ActionResult<List<Season>>> GetSeasonsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var seasons = await _seasonRepository.GetSeasonsAsync();
|
||||
return Ok(seasons);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get seasons");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{number:int}")]
|
||||
[ProducesResponseType(typeof(Season), 200)]
|
||||
[ProducesResponseType(typeof(ErrorResponse), 404)]
|
||||
[ProducesResponseType(500)]
|
||||
public async Task<ActionResult<Season>> GetSeasonByNumberAsync(int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
var season = await _seasonRepository.GetSeasonByNumberAsync(number);
|
||||
|
||||
if (season == null) return NotFound(new ErrorResponse($"Could not find season number {number}"));
|
||||
|
||||
return Ok(season);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get season");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace server.Models
|
||||
{
|
||||
public class Character
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[BsonElement("firstName")]
|
||||
public string? FirstName { get; set; }
|
||||
|
||||
[BsonElement("lastName")]
|
||||
public string? LastName { get; set; }
|
||||
|
||||
[BsonElement("actorFirstName")]
|
||||
public string? ActorFirstName { get; set; }
|
||||
|
||||
[BsonElement("actorLastName")]
|
||||
public string? ActorLastName { get; set; }
|
||||
|
||||
[BsonElement("seasons")]
|
||||
public int[]? Seasons { get; set; }
|
||||
|
||||
[BsonElement("firstEpisode")]
|
||||
public string? FirstEpisode { get; set; }
|
||||
|
||||
[BsonElement("lastEpisode")]
|
||||
public string? LastEpisode { get; set; }
|
||||
|
||||
[BsonElement("image")]
|
||||
public string? Image { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace server.Models
|
||||
{
|
||||
public class Episode
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[BsonElement("season")]
|
||||
public int? Season { get; set; }
|
||||
|
||||
[BsonElement("numberInSeries")]
|
||||
public int? NumberInSeries { get; set; }
|
||||
|
||||
[BsonElement("numberInSeason")]
|
||||
public int? NumberInSeason { get; set; }
|
||||
|
||||
[BsonElement("title")]
|
||||
public string? Title { get; set; }
|
||||
|
||||
[BsonElement("summary")]
|
||||
public string? Summary { get; set; }
|
||||
|
||||
[BsonElement("directedBy")]
|
||||
public string? DirectedBy { get; set; }
|
||||
|
||||
[BsonElement("writtenBy")]
|
||||
public string[]? WrittenBy { get; set; }
|
||||
|
||||
[BsonElement("airDate")]
|
||||
public DateTime? AirDate { get; set; }
|
||||
|
||||
[BsonElement("usViewersInMillions")]
|
||||
public double? UsViewersInMillions { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace server.Models
|
||||
{
|
||||
public class ErrorResponse
|
||||
{
|
||||
public string Error { get; set; }
|
||||
|
||||
public ErrorResponse(string error)
|
||||
{
|
||||
Error = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace server.Models
|
||||
{
|
||||
public class Quote
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[BsonElement("season")]
|
||||
public int? Season { get; set; }
|
||||
|
||||
[BsonElement("episode")]
|
||||
public int Episode { get; set; }
|
||||
|
||||
[BsonElement("text")]
|
||||
public string? Text { get; set; }
|
||||
|
||||
[BsonElement("source")]
|
||||
public string? Source { get; set; }
|
||||
|
||||
[BsonElement("narrator")]
|
||||
public string? Narrator { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace server.Models
|
||||
{
|
||||
public class Season
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
[BsonElement("seasonNumber")]
|
||||
public int? SeasonNumber { get; set; }
|
||||
|
||||
[BsonElement("numberOfEpisodes")]
|
||||
public int? NumberOfEpisodes { get; set; }
|
||||
|
||||
[BsonElement("dateFirstAired")]
|
||||
public DateTime? DateFirstAired { get; set; }
|
||||
|
||||
[BsonElement("dateLastAired")]
|
||||
public DateTime? DateLastAired { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public class DatabaseSettings : IDatabaseSettings
|
||||
{
|
||||
public string? ConnectionString { get; set; }
|
||||
public string? DatabaseName { get; set; }
|
||||
public string? SeasonsCollection { get; set; }
|
||||
public string? EpisodesCollection { get; set; }
|
||||
public string? CharactersCollection { get; set; }
|
||||
public string? QuotesCollection { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.OpenApi.Writers;
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public class DbContext : IDbContext
|
||||
{
|
||||
public IMongoCollection<Season> Seasons { get; set; }
|
||||
|
||||
public IMongoCollection<Episode> Episodes { get; set; }
|
||||
|
||||
public IMongoCollection<Character> Characters { get; set; }
|
||||
|
||||
public IMongoCollection<Quote> Quotes { get; set; }
|
||||
|
||||
|
||||
public DbContext(IDatabaseSettings settings)
|
||||
{
|
||||
IMongoClient client = new MongoClient(settings.ConnectionString);
|
||||
IMongoDatabase database = client.GetDatabase(settings.DatabaseName);
|
||||
|
||||
Seasons = database.GetCollection<Season>(settings.SeasonsCollection);
|
||||
Episodes = database.GetCollection<Episode>(settings.EpisodesCollection);
|
||||
Characters = database.GetCollection<Character>(settings.CharactersCollection);
|
||||
Quotes = database.GetCollection<Quote>(settings.QuotesCollection);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public interface IDatabaseSettings
|
||||
{
|
||||
string? ConnectionString { get; set; }
|
||||
string? DatabaseName { get; set; }
|
||||
string? SeasonsCollection { get; set; }
|
||||
string? EpisodesCollection { get; set; }
|
||||
string? CharactersCollection { get; set; }
|
||||
string? QuotesCollection { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public interface IDbContext
|
||||
{
|
||||
public IMongoCollection<Season> Seasons { get; set; }
|
||||
|
||||
public IMongoCollection<Episode> Episodes { get; set; }
|
||||
|
||||
public IMongoCollection<Character> Characters { get; set; }
|
||||
|
||||
public IMongoCollection<Quote> Quotes { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public interface ISeasonRepository
|
||||
{
|
||||
Task<List<Season>> GetSeasonsAsync();
|
||||
Task<Season> GetSeasonByNumberAsync(int number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public class SeasonRepository : ISeasonRepository
|
||||
{
|
||||
private readonly IDbContext _context;
|
||||
|
||||
public SeasonRepository(IDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<Season>> GetSeasonsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.Seasons.Find(season => true).ToListAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Season> GetSeasonByNumberAsync(int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.Seasons.Find(season => season.SeasonNumber == number).SingleOrDefaultAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user