diff --git a/server/Controllers/SeasonsController.cs b/server/Controllers/SeasonsController.cs new file mode 100644 index 0000000..6977fe5 --- /dev/null +++ b/server/Controllers/SeasonsController.cs @@ -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), 200)] + [ProducesResponseType(500)] + public async Task>> 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> 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"); + } + } + } +} diff --git a/server/Models/Character.cs b/server/Models/Character.cs new file mode 100644 index 0000000..69c0868 --- /dev/null +++ b/server/Models/Character.cs @@ -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; } + } +} diff --git a/server/Models/Episode.cs b/server/Models/Episode.cs new file mode 100644 index 0000000..d9fb88b --- /dev/null +++ b/server/Models/Episode.cs @@ -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; } + + } +} diff --git a/server/Models/ErrorResponse.cs b/server/Models/ErrorResponse.cs new file mode 100644 index 0000000..84cf43a --- /dev/null +++ b/server/Models/ErrorResponse.cs @@ -0,0 +1,12 @@ +namespace server.Models +{ + public class ErrorResponse + { + public string Error { get; set; } + + public ErrorResponse(string error) + { + Error = error; + } + } +} diff --git a/server/Models/Quote.cs b/server/Models/Quote.cs new file mode 100644 index 0000000..8e39049 --- /dev/null +++ b/server/Models/Quote.cs @@ -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; } + } +} diff --git a/server/Models/Season.cs b/server/Models/Season.cs new file mode 100644 index 0000000..8f7d068 --- /dev/null +++ b/server/Models/Season.cs @@ -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; } + } +} diff --git a/server/Persistence/DatabaseSettings.cs b/server/Persistence/DatabaseSettings.cs new file mode 100644 index 0000000..f869158 --- /dev/null +++ b/server/Persistence/DatabaseSettings.cs @@ -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; } + } +} diff --git a/server/Persistence/DbContext.cs b/server/Persistence/DbContext.cs new file mode 100644 index 0000000..8f2f9fa --- /dev/null +++ b/server/Persistence/DbContext.cs @@ -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 Seasons { get; set; } + + public IMongoCollection Episodes { get; set; } + + public IMongoCollection Characters { get; set; } + + public IMongoCollection Quotes { get; set; } + + + public DbContext(IDatabaseSettings settings) + { + IMongoClient client = new MongoClient(settings.ConnectionString); + IMongoDatabase database = client.GetDatabase(settings.DatabaseName); + + Seasons = database.GetCollection(settings.SeasonsCollection); + Episodes = database.GetCollection(settings.EpisodesCollection); + Characters = database.GetCollection(settings.CharactersCollection); + Quotes = database.GetCollection(settings.QuotesCollection); + } + + + } +} diff --git a/server/Persistence/IDatabaseSettings.cs b/server/Persistence/IDatabaseSettings.cs new file mode 100644 index 0000000..56e0be6 --- /dev/null +++ b/server/Persistence/IDatabaseSettings.cs @@ -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; } + } +} diff --git a/server/Persistence/IDbContext.cs b/server/Persistence/IDbContext.cs new file mode 100644 index 0000000..64a05f1 --- /dev/null +++ b/server/Persistence/IDbContext.cs @@ -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 Seasons { get; set; } + + public IMongoCollection Episodes { get; set; } + + public IMongoCollection Characters { get; set; } + + public IMongoCollection Quotes { get; set; } + } +} diff --git a/server/Persistence/Repositories/ISeasonRepository.cs b/server/Persistence/Repositories/ISeasonRepository.cs new file mode 100644 index 0000000..7ab720d --- /dev/null +++ b/server/Persistence/Repositories/ISeasonRepository.cs @@ -0,0 +1,10 @@ +using server.Models; + +namespace server.Persistence.Repositories +{ + public interface ISeasonRepository + { + Task> GetSeasonsAsync(); + Task GetSeasonByNumberAsync(int number); + } +} diff --git a/server/Persistence/Repositories/SeasonRepository.cs b/server/Persistence/Repositories/SeasonRepository.cs new file mode 100644 index 0000000..1ac44ef --- /dev/null +++ b/server/Persistence/Repositories/SeasonRepository.cs @@ -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> GetSeasonsAsync() + { + try + { + return await _context.Seasons.Find(season => true).ToListAsync(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } + + public async Task GetSeasonByNumberAsync(int number) + { + try + { + return await _context.Seasons.Find(season => season.SeasonNumber == number).SingleOrDefaultAsync(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } + } +}