From 4a25d022e1f5514a093de73b720b1a234f8e47a9 Mon Sep 17 00:00:00 2001 From: Hayden Carson Date: Sat, 15 Aug 2020 14:44:51 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9C=F0=9F=92=BE=20Created=20Core=20Pro?= =?UTF-8?q?ject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Controllers/CharactersController.cs | 2 +- Api/Controllers/ComboVotesController.cs | 2 +- Api/Controllers/CombosController.cs | 16 +++++---- Api/Controllers/CommentVotesController.cs | 2 +- Api/Controllers/CommentsController.cs | 2 +- Api/Controllers/SessionsController.cs | 2 +- Api/Controllers/UsersController.cs | 2 +- Api/Program.cs | 2 +- Api/Smash_Combos.csproj | 4 +++ Api/Startup.cs | 7 +++- Smash_Combos.Core/AssemblyUtility.cs | 9 +++++ .../Cqrs/Combos/GetCombo/GetComboProfile.cs | 13 ++++++++ .../Cqrs/Combos/GetCombo/GetComboRequest.cs | 9 +++++ .../Combos/GetCombo/GetComboRequestHandler.cs | 33 +++++++++++++++++++ .../Cqrs/Combos/GetCombo/GetComboResponse.cs | 26 +++++++++++++++ .../Services/IDbContext.cs | 2 +- Smash_Combos.Core/Smash_Combos.Core.csproj | 17 ++++++++++ .../PostgreSqlDatabaseContext.cs | 2 +- .../Smash_Combos.Persistence.csproj | 1 + Smash_Combos.sln | 6 ++++ 20 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 Smash_Combos.Core/AssemblyUtility.cs create mode 100644 Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboProfile.cs create mode 100644 Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequest.cs create mode 100644 Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequestHandler.cs create mode 100644 Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboResponse.cs rename {Smash_Combos.Domain => Smash_Combos.Core}/Services/IDbContext.cs (96%) create mode 100644 Smash_Combos.Core/Smash_Combos.Core.csproj diff --git a/Api/Controllers/CharactersController.cs b/Api/Controllers/CharactersController.cs index edf0e040..4b07c6c5 100644 --- a/Api/Controllers/CharactersController.cs +++ b/Api/Controllers/CharactersController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Collections.Generic; using System.Linq; using System.Threading; diff --git a/Api/Controllers/ComboVotesController.cs b/Api/Controllers/ComboVotesController.cs index 0b2f2249..c6e938ac 100644 --- a/Api/Controllers/ComboVotesController.cs +++ b/Api/Controllers/ComboVotesController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Linq; using System.Threading; using System.Threading.Tasks; diff --git a/Api/Controllers/CombosController.cs b/Api/Controllers/CombosController.cs index 0fd8dd47..b1c73d34 100644 --- a/Api/Controllers/CombosController.cs +++ b/Api/Controllers/CombosController.cs @@ -1,9 +1,11 @@ +using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Smash_Combos.Core.Cqrs.Combos.GetCombo; +using Smash_Combos.Core.Services; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -20,12 +22,14 @@ public class CombosController : ControllerBase { // This is the variable you use to have access to your database private readonly IDbContext _context; + private readonly IMediator _mediator; // Constructor that recives a reference to your database context // and stores it in _context for you to use in your API methods - public CombosController(IDbContext context) + public CombosController(IDbContext context, IMediator mediator) { _context = context; + _mediator = mediator; } // GET: api/Combos @@ -45,18 +49,18 @@ public async Task>> GetCombos() // to grab the id from the URL. It is then made available to us as the `id` argument to the method. // [HttpGet("{id}")] - public async Task> GetCombo(int id) + public async Task> GetCombo(int id) { - var combo = await _context.Combos.Where(combo => combo.Id == id).Include(combo => combo.User).Include(combo => combo.Comments).ThenInclude(comment => comment.User).FirstOrDefaultAsync(); + var response = await _mediator.Send(new GetComboRequest { ComboID = id }); - if (combo == null) + if (response == null) { // Return a `404` response to the client indicating we could not find a combo with this id return NotFound(); } // Return the combo as a JSON object. - return combo; + return response; } // PUT: api/Combos/5 diff --git a/Api/Controllers/CommentVotesController.cs b/Api/Controllers/CommentVotesController.cs index 095f5d35..a7cff96f 100644 --- a/Api/Controllers/CommentVotesController.cs +++ b/Api/Controllers/CommentVotesController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Linq; using System.Threading; using System.Threading.Tasks; diff --git a/Api/Controllers/CommentsController.cs b/Api/Controllers/CommentsController.cs index 9616aa42..7fec5e0e 100644 --- a/Api/Controllers/CommentsController.cs +++ b/Api/Controllers/CommentsController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Collections.Generic; using System.Linq; using System.Threading; diff --git a/Api/Controllers/SessionsController.cs b/Api/Controllers/SessionsController.cs index 8f260013..1f108a26 100644 --- a/Api/Controllers/SessionsController.cs +++ b/Api/Controllers/SessionsController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Api/Controllers/UsersController.cs b/Api/Controllers/UsersController.cs index 0b7fa91a..c0d97315 100644 --- a/Api/Controllers/UsersController.cs +++ b/Api/Controllers/UsersController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System.Collections.Generic; using System.Linq; using System.Threading; diff --git a/Api/Program.cs b/Api/Program.cs index 4b01bed4..088506c1 100644 --- a/Api/Program.cs +++ b/Api/Program.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System; using System.Data.Common; using System.Linq; diff --git a/Api/Smash_Combos.csproj b/Api/Smash_Combos.csproj index eeb85699..ad52ffb5 100644 --- a/Api/Smash_Combos.csproj +++ b/Api/Smash_Combos.csproj @@ -12,6 +12,9 @@ Smash_Combos + + + @@ -29,6 +32,7 @@ + diff --git a/Api/Startup.cs b/Api/Startup.cs index 7e2ccc7d..cffe79b3 100644 --- a/Api/Startup.cs +++ b/Api/Startup.cs @@ -1,3 +1,5 @@ +using AutoMapper; +using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -7,8 +9,9 @@ using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using Smash_Combos.Persistence; +using System.Reflection; using System.Text; namespace Smash_Combos @@ -38,6 +41,8 @@ public void ConfigureServices(IServiceCollection services) c.SwaggerDoc("v1", new OpenApiInfo { Title = "Smash_Combos", Version = "v1" }); }); services.AddDbContext(); + services.AddMediatR(Core.AssemblyUtility.GetAssembly()); + services.AddAutoMapper(Core.AssemblyUtility.GetAssembly()); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { diff --git a/Smash_Combos.Core/AssemblyUtility.cs b/Smash_Combos.Core/AssemblyUtility.cs new file mode 100644 index 00000000..91e1eca4 --- /dev/null +++ b/Smash_Combos.Core/AssemblyUtility.cs @@ -0,0 +1,9 @@ +using System.Reflection; + +namespace Smash_Combos.Core +{ + public static class AssemblyUtility + { + public static Assembly GetAssembly() => Assembly.GetExecutingAssembly(); + } +} diff --git a/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboProfile.cs b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboProfile.cs new file mode 100644 index 00000000..2bc5d8c3 --- /dev/null +++ b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboProfile.cs @@ -0,0 +1,13 @@ +using AutoMapper; +using Smash_Combos.Domain.Models; + +namespace Smash_Combos.Core.Cqrs.Combos.GetCombo +{ + public class GetComboProfile : Profile + { + public GetComboProfile() + { + CreateMap(); + } + } +} diff --git a/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequest.cs b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequest.cs new file mode 100644 index 00000000..98b082e9 --- /dev/null +++ b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequest.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Smash_Combos.Core.Cqrs.Combos.GetCombo +{ + public class GetComboRequest : IRequest + { + public int ComboID { get; set; } + } +} diff --git a/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequestHandler.cs b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequestHandler.cs new file mode 100644 index 00000000..aa6b3dfb --- /dev/null +++ b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequestHandler.cs @@ -0,0 +1,33 @@ +using AutoMapper; +using MediatR; +using Microsoft.EntityFrameworkCore; +using Smash_Combos.Core.Services; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Smash_Combos.Core.Cqrs.Combos.GetCombo +{ + public class GetComboRequestHandler : IRequestHandler + { + private readonly IDbContext _dbContext; + private readonly IMapper _mapper; + + public GetComboRequestHandler(IDbContext context, IMapper mapper) + { + _dbContext = context ?? throw new ArgumentNullException(nameof(context)); + _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); + } + + public async Task Handle(GetComboRequest request, CancellationToken cancellationToken) + { + var combo = await _dbContext.Combos.Where(combo => combo.Id == request.ComboID).Include(combo => combo.User).Include(combo => combo.Comments).ThenInclude(comment => comment.User).FirstOrDefaultAsync(); + + if (combo == null) + return null; + + return _mapper.Map(combo); + } + } +} diff --git a/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboResponse.cs b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboResponse.cs new file mode 100644 index 00000000..5b6d5f2f --- /dev/null +++ b/Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboResponse.cs @@ -0,0 +1,26 @@ +using Smash_Combos.Domain.Models; +using System; +using System.Collections.Generic; + +namespace Smash_Combos.Core.Cqrs.Combos.GetCombo +{ + public class GetComboResponse + { + public int Id { get; set; } + public int UserId { get; set; } + public int CharacterId { get; set; } + public User User { get; set; } + public DateTime DatePosted { get; set; } + public string Title { get; set; } + public string VideoId { get; set; } + public int VideoStartTime { get; set; } + public int VideoEndTime { get; set; } + public string ComboInput { get; set; } + public bool TrueCombo { get; set; } + public string Difficulty { get; set; } + public int Damage { get; set; } + public string Notes { get; set; } + public List Comments { get; set; } + public int NetVote { get; set; } + } +} diff --git a/Smash_Combos.Domain/Services/IDbContext.cs b/Smash_Combos.Core/Services/IDbContext.cs similarity index 96% rename from Smash_Combos.Domain/Services/IDbContext.cs rename to Smash_Combos.Core/Services/IDbContext.cs index e5f00138..077f5dbe 100644 --- a/Smash_Combos.Domain/Services/IDbContext.cs +++ b/Smash_Combos.Core/Services/IDbContext.cs @@ -6,7 +6,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Smash_Combos.Domain.Services +namespace Smash_Combos.Core.Services { public interface IDbContext { diff --git a/Smash_Combos.Core/Smash_Combos.Core.csproj b/Smash_Combos.Core/Smash_Combos.Core.csproj new file mode 100644 index 00000000..fbcfadc4 --- /dev/null +++ b/Smash_Combos.Core/Smash_Combos.Core.csproj @@ -0,0 +1,17 @@ + + + + netstandard2.1 + + + + + + + + + + + + + diff --git a/Smash_Combos.Persistence/PostgreSqlDatabaseContext.cs b/Smash_Combos.Persistence/PostgreSqlDatabaseContext.cs index 4ebdfc50..f6709be7 100644 --- a/Smash_Combos.Persistence/PostgreSqlDatabaseContext.cs +++ b/Smash_Combos.Persistence/PostgreSqlDatabaseContext.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Smash_Combos.Domain.Models; -using Smash_Combos.Domain.Services; +using Smash_Combos.Core.Services; using System; using System.Text.RegularExpressions; diff --git a/Smash_Combos.Persistence/Smash_Combos.Persistence.csproj b/Smash_Combos.Persistence/Smash_Combos.Persistence.csproj index 2413cbb4..f51a26cb 100644 --- a/Smash_Combos.Persistence/Smash_Combos.Persistence.csproj +++ b/Smash_Combos.Persistence/Smash_Combos.Persistence.csproj @@ -17,6 +17,7 @@ + diff --git a/Smash_Combos.sln b/Smash_Combos.sln index 58e26f4a..1b1a7873 100644 --- a/Smash_Combos.sln +++ b/Smash_Combos.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Smash_Combos.Domain", "Smas EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Smash_Combos.Persistence", "Smash_Combos.Persistence\Smash_Combos.Persistence.csproj", "{3A464018-BB21-4E49-8DBF-4651E76E7A0D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smash_Combos.Core", "Smash_Combos.Core\Smash_Combos.Core.csproj", "{98EF87DF-2810-4E5B-899E-E9726AE96D6C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {3A464018-BB21-4E49-8DBF-4651E76E7A0D}.Debug|Any CPU.Build.0 = Debug|Any CPU {3A464018-BB21-4E49-8DBF-4651E76E7A0D}.Release|Any CPU.ActiveCfg = Release|Any CPU {3A464018-BB21-4E49-8DBF-4651E76E7A0D}.Release|Any CPU.Build.0 = Release|Any CPU + {98EF87DF-2810-4E5B-899E-E9726AE96D6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98EF87DF-2810-4E5B-899E-E9726AE96D6C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98EF87DF-2810-4E5B-899E-E9726AE96D6C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98EF87DF-2810-4E5B-899E-E9726AE96D6C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE