Skip to content
Permalink
Browse files

Add API-VERSIONING & refactor sum code. Closes #11 (kindof)

  • Loading branch information...
gariasf committed Mar 12, 2019
1 parent 81a820e commit 904023caa1bd960c1d40c98f2145e8145e380cca
Showing with 135 additions and 53 deletions.
  1. +13 −10 ...Exam/VuelingExam.Bussiness.Facade/Controllers/{InsuranceController.cs → InsuranceControllerV1.cs}
  2. +67 −0 VuelingExam/VuelingExam.Bussiness.Facade/Controllers/InsuranceControllerV2.cs
  3. +3 −1 VuelingExam/VuelingExam.Bussiness.Facade/JwtConfigure.cs
  4. +6 −3 VuelingExam/VuelingExam.Bussiness.Facade/Modules/FacadeModule.cs
  5. +11 −4 VuelingExam/VuelingExam.Bussiness.Facade/Startup.cs
  6. +2 −0 VuelingExam/VuelingExam.Bussiness.Facade/VuelingExam.Bussiness.Facade.csproj
  7. +1 −2 VuelingExam/VuelingExam.Bussiness.Logic/ExternalData/HTTPClientFetcherBL.cs
  8. +1 −2 VuelingExam/VuelingExam.Bussiness.Logic/ExternalData/HTTPPolicyFetcherBL.cs
  9. +1 −0 VuelingExam/VuelingExam.Common.Logic/Models/Client.cs
  10. +6 −3 VuelingExam/VuelingExam.Common.Logic/Models/Policy.cs
  11. +2 −1 VuelingExam/VuelingExam.Common.Logic/Utils/RepositoryCleaner.cs
  12. +1 −0 VuelingExam/VuelingExam.Common.Logic/VuelingExam.Common.Logic.csproj
  13. +1 −1 VuelingExam/VuelingExam.RepositoryLogic/Interfaces/Crud/IGetEntityById.cs
  14. +1 −2 VuelingExam/VuelingExam.RepositoryLogic/Interfaces/Crud/IGetListFilteredByConstraintId.cs
  15. +1 −1 VuelingExam/VuelingExam.RepositoryLogic/Interfaces/IAuthRepository.cs
  16. +1 −4 VuelingExam/VuelingExam.RepositoryLogic/Interfaces/IClientRepository.cs
  17. +3 −5 VuelingExam/VuelingExam.RepositoryLogic/Interfaces/IPolicyRepository.cs
  18. +2 −5 VuelingExam/VuelingExam.RepositoryLogic/Repositories/AuthenticationRepository.cs
  19. +0 −1 VuelingExam/VuelingExam.RepositoryLogic/Repositories/ClientRepository.cs
  20. +12 −8 VuelingExam/VuelingExam.RepositoryLogic/Repositories/PolicyRepository.cs
@@ -1,14 +1,17 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RiskFirst.Hateoas;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Bussiness.Logic.Interfaces;
using VuelingExam.Common.Logic.Models;

namespace VuelingExam.Bussiness.Facade.Controllers
namespace VuelingExam.Bussiness.Facade.Controllers.V1
{
[Authorize]
[ApiVersion("1.0")]
[Route("api/[controller]/[action]")]
[ApiController]
public class InsuranceController : ControllerBase
@@ -22,26 +25,26 @@ public InsuranceController(IInsurance insuranceBL)

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[HttpGet("{clientId:guid}")]
public ActionResult<Client> GetClientById(Guid clientId)
[HttpGet("{Id:guid}")]
public ActionResult<Client> GetClientById(Guid Id)
{
return Ok(InsuranceBL.GetClientData(clientId));
return Ok(InsuranceBL.GetClientData(Id));
}

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[HttpGet("{clientName}")]
public ActionResult<Client> GetClientByName(string clientName)
[HttpGet("{Name}")]
public ActionResult<Client> GetClientByName(string Name)
{
return Ok(InsuranceBL.GetClientData(clientName));
return Ok(InsuranceBL.GetClientData(Name));
}

[ConnectionFilter]
[Authorize(Roles = "admin")]
[HttpGet("{policyId:Guid}")]
public ActionResult<Client> GetClientFromPolicy(Guid policyId)
[HttpGet("{Id:Guid}")]
public ActionResult<Client> GetClientFromPolicy(Guid Id)
{
return Ok(InsuranceBL.GetClientFromPolicy(policyId));
return Ok(InsuranceBL.GetClientFromPolicy(Id));
}

[ConnectionFilter]
@@ -0,0 +1,67 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RiskFirst.Hateoas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Bussiness.Logic.Interfaces;
using VuelingExam.Common.Logic.Models;

namespace VuelingExam.Bussiness.Facade.Controllers.V2
{
[Authorize]
[ApiVersion("2.0")]
[Route("api/[controller]/[action]")]
[ApiController]
public class InsuranceV2 : ControllerBase
{
private readonly IInsurance InsuranceBL;
private readonly ILinksService LinksService;

public InsuranceV2(IInsurance insuranceBL, ILinksService linksService)
{
InsuranceBL = insuranceBL;
LinksService = linksService;
}

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[HttpGet("{Id:guid}", Name = "GetClientById")]
public async Task<ActionResult<Client>> GetClientById(Guid Id)
{
Client model = InsuranceBL.GetClientData(Id);
// await LinksService.AddLinksAsync(model);
return Ok(model);
}

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[HttpGet("{Name}", Name = "GetClientByName")]
public async Task<ActionResult<Client>> GetClientByName(string Name)
{
var model = InsuranceBL.GetClientData(Name);
// await LinksService.AddLinksAsync(model);
return Ok(model);
}

[ConnectionFilter]
[Authorize(Roles = "admin")]
[HttpGet("{Id:Guid}", Name = "GetClientFromPolicy")]
public async Task<ActionResult<Client>> GetClientFromPolicy(Guid Id)
{
var model = InsuranceBL.GetClientFromPolicy(Id);
// await LinksService.AddLinksAsync(model);
return Ok(model);
}

[ConnectionFilter]
[Authorize(Roles = "admin")]
[HttpGet("{clientId:Guid}", Name = "GetClientPolicies")]
public ActionResult<List<Policy>> GetClientPolicies(Guid clientId)
{
return Ok(InsuranceBL.GetClientLinkedPolicies(clientId));
}
}
}
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -1,6 +1,7 @@
using Autofac;
using Autofac.Integration.WebApi;
using System.Reflection;
using System.Transactions;
using VuelingExam.Bussiness.Logic;
using VuelingExam.Bussiness.Logic.ExternalData;
using VuelingExam.Bussiness.Logic.Interfaces;
@@ -36,16 +37,18 @@ protected override void Load(ContainerBuilder builder)
.As<IInsurance>()
.SingleInstance();


builder
.RegisterType<HttpPolicyFetcherBL>()
.AsSelf()
.AutoActivate();
.RegisterType<HttpPolicyFetcherBL>()
.AsSelf()
.AutoActivate();

builder
.RegisterType<HttpClientFetcherBL>()
.AsSelf()
.AutoActivate();


builder.RegisterModule(new BussinessModule());

base.Load(builder);
@@ -1,17 +1,16 @@
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Swashbuckle.AspNetCore.Swagger;
using RiskFirst.Hateoas;
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Common.Logic;
using VuelingExam.Common.Logic.Models;
using VuelingExam.Common.Logic.Utils;

namespace VuelingExam.Bussiness.Facade
@@ -36,6 +35,14 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
config.Filters.Add(new ConnectionFilterAttribute());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
o.ApiVersionReader = new HeaderApiVersionReader("api-version");
});

SwaggerConfigure.Configure(services);
JwtConfigure.Configure(services);

@@ -13,8 +13,10 @@
<PackageReference Include="Autofac.WebApi2" Version="4.2.0" />
<PackageReference Include="AutofacSerilogIntegration" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
<PackageReference Include="RiskFirst.Hateoas" Version="3.1.1" />
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
@@ -72,8 +72,7 @@ public void StoreListToDatabase()

ClientList.ForEach(client =>
{

Repository.Add(client);
Repository.Add(client);
});

Logger.Information("Done!");
@@ -74,8 +74,7 @@ public void StoreListToDatabase()

PolicyList.ForEach(policy =>
{

Repository.Add(policy);
Repository.Add(policy);
});

Logger.Information("Done!");
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using RiskFirst.Hateoas.Models;
using System;
using VuelingExam.Common.Logic.Enums;

@@ -1,4 +1,5 @@
using System;
using RiskFirst.Hateoas.Models;
using System;

namespace VuelingExam.Common.Logic.Models
{
@@ -12,7 +13,8 @@ public class Policy
public Guid ClientId { get; set; }
public virtual Client Client { get; set; }

public Policy(Guid id, decimal insuredAmount, string email, DateTime inceptionDate, bool installmentPayment, Guid clientId)
public Policy(Guid id, decimal insuredAmount, string email, DateTime inceptionDate,
bool installmentPayment, Guid clientId)
{
Id = id;
AmountInsured = insuredAmount;
@@ -22,7 +24,8 @@ public Policy(Guid id, decimal insuredAmount, string email, DateTime inceptionDa
ClientId = clientId;
}

public Policy(decimal insuredAmount, string email, DateTime inceptionDate, bool installmentPayment, Guid clientId)
public Policy(decimal insuredAmount, string email, DateTime inceptionDate,
bool installmentPayment, Guid clientId)
{
AmountInsured = insuredAmount;
Email = email;
@@ -5,7 +5,8 @@ namespace VuelingExam.Common.Logic.Utils
{
public static class RepositoryCleaner
{
private readonly static string ConnectionString = ConfigHelper.Config["dbConnectionString"];
private readonly static string ConnectionString =
ConfigHelper.Config["dbConnectionString"];

public static void CleanTables(List<string> tableNames)
{
@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="RiskFirst.Hateoas" Version="3.1.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
</ItemGroup>

@@ -2,7 +2,7 @@

namespace VuelingExam.RepositoryLogic.Interfaces.Crud
{
public interface IGetEntityById<T>
public interface IGetEntityById<out T>
{
T GetEntityById(Guid id);
}
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace VuelingExam.RepositoryLogic.Interfaces.Crud
{
public interface IGetListFilteredByConstraintId <T>
public interface IGetListFilteredByConstraintId<T>
{
List<T> GetListFilteredByConstraintId(Guid clientId);
}
@@ -1,6 +1,6 @@
namespace VuelingExam.RepositoryLogic.Interfaces
{
public interface IAuthRepository<T>
public interface IAuthRepository<in T>
{
string GenerateToken(T credentials);
}
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Common.Logic.Models;
using VuelingExam.Common.Logic.Models;
using VuelingExam.RepositoryLogic.Interfaces.Crud;

namespace VuelingExam.RepositoryLogic.Interfaces
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Common.Logic.Models;
using VuelingExam.Common.Logic.Models;
using VuelingExam.RepositoryLogic.Interfaces.Crud;

namespace VuelingExam.RepositoryLogic.Interfaces
{
public interface IPolicyRepository : IRepository<Policy>, IGetListFilteredByConstraintId<Policy>
public interface IPolicyRepository : IRepository<Policy>,
IGetListFilteredByConstraintId<Policy>
{
}
}
@@ -1,14 +1,11 @@
using Microsoft.IdentityModel.Tokens;
using Serilog;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using VuelingExam.Common.Logic;
using VuelingExam.Common.Logic.Models;
using VuelingExam.RepositoryLogic.Exceptions;
using VuelingExam.RepositoryLogic.Interfaces;
using VuelingExam.RepositoryLogic.Resources;

@@ -17,7 +14,6 @@ namespace VuelingExam.RepositoryLogic
public class AuthenticationRepository : IAuthRepository<Client>
{

private readonly static string ConnectionString = ConfigHelper.Config["dbConnectionString"];
private readonly static string TokenSecret = ConfigHelper.Config["tokenSecret"];
private readonly ILogger Logger;

@@ -41,7 +37,8 @@ public string GenerateToken(Client credentials)
new Claim(ClaimTypes.Role, credentials.Role.ToString())
}),
Expires = DateTime.UtcNow.AddMinutes(30),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256Signature)
};

var createdToken = tokenHandler.CreateToken(tokenDescriptor);
@@ -94,7 +94,6 @@ public List<Client> GetAll()
clientList.Add(client);
}
}

}
}
}
Oops, something went wrong.

0 comments on commit 904023c

Please sign in to comment.
You can’t perform that action at this time.