From 58f3c0e93061fd05e60e52ddab5190ec4b396e16 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Sun, 19 Jun 2022 16:42:02 -0500 Subject: [PATCH 1/2] include changes --- client/public/index.html | 31 +---------------- client/src/App.js | 9 ++++- client/src/setupProxy.js | 2 +- .../Controllers/WeatherForecastController.cs | 33 ------------------- server/Program.cs | 23 ++++++++++--- server/WeatherForecast.cs | 13 -------- server/server.csproj | 5 +-- 7 files changed, 31 insertions(+), 85 deletions(-) delete mode 100644 server/Controllers/WeatherForecastController.cs delete mode 100644 server/WeatherForecast.cs diff --git a/client/public/index.html b/client/public/index.html index aa069f2..fbdddf3 100644 --- a/client/public/index.html +++ b/client/public/index.html @@ -5,39 +5,10 @@ - - - - - React App - -
- +
diff --git a/client/src/App.js b/client/src/App.js index a0f61d0..4084a85 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -51,7 +51,14 @@ export default class App extends Component { } async populateWeatherData() { - const response = await fetch('https://criminalminds-server.azurewebsites.net/weatherforecast'); + + const url = '/api/seasons'; + + if (process.env.NODE_ENV == 'production') { + url = 'https://criminalminds-server.azurewebsites.net' + url; + } + + const response = await fetch(url); const data = await response.json(); this.setState({ forecasts: data, loading: false }); } diff --git a/client/src/setupProxy.js b/client/src/setupProxy.js index 72f4c46..8a899ab 100644 --- a/client/src/setupProxy.js +++ b/client/src/setupProxy.js @@ -1,7 +1,7 @@ const { createProxyMiddleware } = require('http-proxy-middleware'); const context = [ - "/weatherforecast", + '/api/seasons' ]; module.exports = function (app) { diff --git a/server/Controllers/WeatherForecastController.cs b/server/Controllers/WeatherForecastController.cs deleted file mode 100644 index c3a25d4..0000000 --- a/server/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace server.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/server/Program.cs b/server/Program.cs index b83fd9b..67e2101 100644 --- a/server/Program.cs +++ b/server/Program.cs @@ -1,5 +1,20 @@ +using Microsoft.Extensions.Options; +using MongoDB.Driver; +using server.Persistence; +using server.Persistence.Repositories; + var builder = WebApplication.CreateBuilder(args); +builder.Services.Configure( + builder.Configuration.GetSection("MongoDBSettings")); + +builder.Services.AddSingleton(sp => + sp.GetRequiredService>().Value); + +builder.Services.AddSingleton(); + +builder.Services.AddScoped(); + builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); @@ -7,11 +22,9 @@ builder.Services.AddSwaggerGen(); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} +app.UseSwagger(); + +app.UseSwaggerUI(); app.UseHttpsRedirection(); diff --git a/server/WeatherForecast.cs b/server/WeatherForecast.cs deleted file mode 100644 index 1b7802b..0000000 --- a/server/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace server -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/server/server.csproj b/server/server.csproj index 60bf9ea..675eba3 100644 --- a/server/server.csproj +++ b/server/server.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,7 +7,8 @@ - + + From 11619d36386352b367395a2344e41c75283dea0f Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Sun, 19 Jun 2022 16:42:35 -0500 Subject: [PATCH 2/2] add changes --- server/Controllers/SeasonsController.cs | 57 +++++++++++++++++++ server/Models/Character.cs | 36 ++++++++++++ server/Models/Episode.cs | 40 +++++++++++++ server/Models/ErrorResponse.cs | 12 ++++ server/Models/Quote.cs | 27 +++++++++ server/Models/Season.cs | 24 ++++++++ server/Persistence/DatabaseSettings.cs | 18 ++++++ server/Persistence/DbContext.cs | 36 ++++++++++++ server/Persistence/IDatabaseSettings.cs | 18 ++++++ server/Persistence/IDbContext.cs | 21 +++++++ .../Repositories/ISeasonRepository.cs | 10 ++++ .../Repositories/SeasonRepository.cs | 41 +++++++++++++ 12 files changed, 340 insertions(+) create mode 100644 server/Controllers/SeasonsController.cs create mode 100644 server/Models/Character.cs create mode 100644 server/Models/Episode.cs create mode 100644 server/Models/ErrorResponse.cs create mode 100644 server/Models/Quote.cs create mode 100644 server/Models/Season.cs create mode 100644 server/Persistence/DatabaseSettings.cs create mode 100644 server/Persistence/DbContext.cs create mode 100644 server/Persistence/IDatabaseSettings.cs create mode 100644 server/Persistence/IDbContext.cs create mode 100644 server/Persistence/Repositories/ISeasonRepository.cs create mode 100644 server/Persistence/Repositories/SeasonRepository.cs 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; + } + } + } +}