Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💜💾 Created Core Project #22

Merged
merged 1 commit into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Api/Controllers/CharactersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Api/Controllers/ComboVotesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 10 additions & 6 deletions Api/Controllers/CombosController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -45,18 +49,18 @@ public async Task<ActionResult<IEnumerable<Combo>>> 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<ActionResult<Combo>> GetCombo(int id)
public async Task<ActionResult<GetComboResponse>> 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
Expand Down
2 changes: 1 addition & 1 deletion Api/Controllers/CommentVotesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Api/Controllers/CommentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Api/Controllers/SessionsController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Api/Smash_Combos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<AssemblyName>Smash_Combos</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="MediatR" Version="8.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
Expand All @@ -29,6 +32,7 @@
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Smash_Combos.Core\Smash_Combos.Core.csproj" />
<ProjectReference Include="..\Smash_Combos.Persistence\Smash_Combos.Persistence.csproj" />
<ProjectReference Include="..\Smash_Combos.Domain\Smash_Combos.Domain.csproj" />
<ProjectReference Include="..\Smash_Combos.Persistence\Smash_Combos.Persistence.csproj" />
Expand Down
7 changes: 6 additions & 1 deletion Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -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
Expand Down Expand Up @@ -38,6 +41,8 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Smash_Combos", Version = "v1" });
});
services.AddDbContext<IDbContext, PostgreSqlDatabaseContext>();
services.AddMediatR(Core.AssemblyUtility.GetAssembly());
services.AddAutoMapper(Core.AssemblyUtility.GetAssembly());

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
Expand Down
9 changes: 9 additions & 0 deletions Smash_Combos.Core/AssemblyUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace Smash_Combos.Core
{
public static class AssemblyUtility
{
public static Assembly GetAssembly() => Assembly.GetExecutingAssembly();
}
}
13 changes: 13 additions & 0 deletions Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboProfile.cs
Original file line number Diff line number Diff line change
@@ -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<Combo, GetComboResponse>();
}
}
}
9 changes: 9 additions & 0 deletions Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MediatR;

namespace Smash_Combos.Core.Cqrs.Combos.GetCombo
{
public class GetComboRequest : IRequest<GetComboResponse>
{
public int ComboID { get; set; }
}
}
33 changes: 33 additions & 0 deletions Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -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<GetComboRequest, GetComboResponse>
{
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<GetComboResponse> 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<GetComboResponse>(combo);
}
}
}
26 changes: 26 additions & 0 deletions Smash_Combos.Core/Cqrs/Combos/GetCombo/GetComboResponse.cs
Original file line number Diff line number Diff line change
@@ -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<Comment> Comments { get; set; }
public int NetVote { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Smash_Combos.Domain.Services
namespace Smash_Combos.Core.Services
{
public interface IDbContext
{
Expand Down
17 changes: 17 additions & 0 deletions Smash_Combos.Core/Smash_Combos.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.0.0" />
<PackageReference Include="MediatR" Version="8.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Smash_Combos.Domain\Smash_Combos.Domain.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Smash_Combos.Persistence/PostgreSqlDatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
1 change: 1 addition & 0 deletions Smash_Combos.Persistence/Smash_Combos.Persistence.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Smash_Combos.Core\Smash_Combos.Core.csproj" />
<ProjectReference Include="..\Smash_Combos.Domain\Smash_Combos.Domain.csproj" />
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions Smash_Combos.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down