Skip to content
Permalink
Browse files

Fix API versioning

  • Loading branch information...
gariasf committed Mar 12, 2019
1 parent 904023c commit 5366c1273673a18042e6854087b54187ea8a7c45
@@ -1,10 +1,3 @@
USE [VuelingExam]
GO
/****** Object: Table [dbo].[Client] Script Date: 08/03/2019 10:37:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Client](
[Id] [uniqueidentifier] NOT NULL,
[Name] [varchar](100) NOT NULL,
@@ -1,10 +1,3 @@
USE [VuelingExam]
GO
/****** Object: Table [dbo].[Policy] Script Date: 08/03/2019 10:37:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Policy](
[Id] [uniqueidentifier] NOT NULL,
[AmountInsured] [decimal](7, 2) NOT NULL,

This file was deleted.

Oops, something went wrong.
@@ -11,8 +11,8 @@
namespace VuelingExam.Bussiness.Facade.Controllers.V1
{
[Authorize]
[ApiVersion("1.0")]
[Route("api/[controller]/[action]")]
[ApiVersion("1")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class InsuranceController : ControllerBase
{
@@ -25,6 +25,7 @@ public InsuranceController(IInsurance insuranceBL)

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[MapToApiVersion("1")]
[HttpGet("{Id:guid}")]
public ActionResult<Client> GetClientById(Guid Id)
{
@@ -33,6 +34,7 @@ public ActionResult<Client> GetClientById(Guid Id)

[ConnectionFilter]
[Authorize(Roles = "user, admin")]
[MapToApiVersion("1")]
[HttpGet("{Name}")]
public ActionResult<Client> GetClientByName(string Name)
{
@@ -41,6 +43,7 @@ public ActionResult<Client> GetClientByName(string Name)

[ConnectionFilter]
[Authorize(Roles = "admin")]
[MapToApiVersion("1")]
[HttpGet("{Id:Guid}")]
public ActionResult<Client> GetClientFromPolicy(Guid Id)
{
@@ -49,6 +52,7 @@ public ActionResult<Client> GetClientFromPolicy(Guid Id)

[ConnectionFilter]
[Authorize(Roles = "admin")]
[MapToApiVersion("1")]
[HttpGet("{clientId:Guid}")]
public ActionResult<List<Policy>> GetClientPolicies(Guid clientId)
{
@@ -12,52 +12,56 @@
namespace VuelingExam.Bussiness.Facade.Controllers.V2
{
[Authorize]
[ApiVersion("2.0")]
[Route("api/[controller]/[action]")]
[ApiVersion("2")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class InsuranceV2 : ControllerBase
public class InsuranceController : ControllerBase
{
private readonly IInsurance InsuranceBL;
private readonly ILinksService LinksService;

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

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

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

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

[ConnectionFilter]
[Authorize(Roles = "admin")]
[MapToApiVersion("2")]
[HttpGet("{clientId:Guid}", Name = "GetClientPolicies")]
public ActionResult<List<Policy>> GetClientPolicies(Guid clientId)
{
@@ -39,9 +39,9 @@ protected override void Load(ContainerBuilder builder)


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

builder
.RegisterType<HttpClientFetcherBL>()
@@ -8,6 +8,8 @@
using RiskFirst.Hateoas;
using System;
using System.Collections.Generic;
using VuelingExam.Bussiness.Facade.Controllers.V1;
using VuelingExam.Bussiness.Facade.Controllers.V2;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Common.Logic;
using VuelingExam.Common.Logic.Models;
@@ -17,9 +19,7 @@ namespace VuelingExam.Bussiness.Facade
{
public class Startup
{
private readonly static string SwaggerVersion = ConfigHelper.Config["swaggerVersion"];
private readonly static string SwaggerTitle = ConfigHelper.Config["swaggerTitle"];
private readonly static string SwaggerEndpoint = ConfigHelper.Config["swaggerEndpoint"];

public Startup(IConfiguration configuration)
{
@@ -41,6 +41,15 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
o.ApiVersionReader = new HeaderApiVersionReader("api-version");
o.Conventions.Controller<Controllers.V1.InsuranceController>().HasApiVersion(new ApiVersion(1, 0));
o.Conventions.Controller<Controllers.V2.InsuranceController>().HasApiVersion(new ApiVersion(2, 0));
});

services.AddLinks(config =>
{
config.AddPolicy<Client>(policy => {
policy.RequireRoutedLink("self", "GetClientById", x => new { id = x.Id });
});
});

SwaggerConfigure.Configure(services);
@@ -73,7 +82,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint(SwaggerEndpoint, SwaggerTitle + SwaggerVersion);
// I know this shouldn't be hardcoded, give me a break
c.SwaggerEndpoint("/swagger/v1/swagger.json", SwaggerTitle + "v1");
c.SwaggerEndpoint("/swagger/v2/swagger.json", SwaggerTitle + "v2");
});
}
}
@@ -1,29 +1,50 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VuelingExam.Bussiness.Facade.Utils;
using VuelingExam.Common.Logic;

namespace VuelingExam.Bussiness.Facade
{
public static class SwaggerConfigure
{
private readonly static string SwaggerVersion = ConfigHelper.Config["swaggerVersion"];
private readonly static string SwaggerTitle = ConfigHelper.Config["swaggerTitle"];
private readonly static string SwaggerDescription = ConfigHelper.Config["swaggerDescription"];

public static IServiceCollection Configure (IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(SwaggerVersion, new Info
// Should these versions be hardcoded? I don't think so
c.SwaggerDoc("v1", new Info
{
Version = SwaggerVersion,
Title = SwaggerTitle,
Version = "v1",
Title = SwaggerTitle + "V1",
Description = SwaggerDescription
});

c.SwaggerDoc("v2", new Info
{
Version = "v2",
Title = SwaggerTitle + "V2",
Description = SwaggerDescription
});

c.DocInclusionPredicate((docName, apiDesc) =>
{
var versions = apiDesc.ControllerAttributes()
.OfType<ApiVersionAttribute>()
.SelectMany(attr => attr.Versions);

return versions.Any(v => $"v{v.ToString()}" == docName);
});

c.DocumentFilter<SetVersionInPaths>();
});

return services;
@@ -0,0 +1,21 @@
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace VuelingExam.Bussiness.Facade.Utils
{
public class SetVersionInPaths : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Paths = swaggerDoc.Paths
.ToDictionary(
path => path.Key.Replace("v{version}", swaggerDoc.Info.Version),
path => path.Value
);
}
}
}
@@ -17,7 +17,8 @@ public class PolicyBussinessCrudBL : IInsurancePolicy
private readonly IClientRepository ClientRepository;
private readonly ILogger Logger;

public PolicyBussinessCrudBL(IPolicyRepository policyRepository, IClientRepository clientRepository, ILogger logger)
public PolicyBussinessCrudBL(IPolicyRepository policyRepository, IClientRepository clientRepository,
ILogger logger)
{
PolicyRepository = policyRepository;
ClientRepository = clientRepository;
@@ -33,8 +34,7 @@ public Policy Add(Policy model)

try
{
result = PolicyRepository.Add(model);

result = PolicyRepository.Add(model);
}
catch (VuelingDaoException e)
{
@@ -46,11 +46,6 @@ public Policy Add(Policy model)
return result;
}

public bool Delete(Guid id)
{
throw new NotImplementedException();
}

public List<Policy> GetAll()
{
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
@@ -6,7 +6,6 @@ public interface IHttpFetcher<T>
{
List<T> FetchListFromAPI();
void FetchAndStoreData();

void StoreListToDatabase();
}
}
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Bussiness.Logic.Interfaces.Crud;
using VuelingExam.Common.Logic.Models;

namespace VuelingExam.Bussiness.Logic.Interfaces
@@ -6,7 +6,8 @@

namespace VuelingExam.Bussiness.Logic.Interfaces
{
public interface IInsurancePolicy : IBussinessCrud<Policy>, IGetListFilteredByConstraintId<Policy>
public interface IInsurancePolicy : IBussinessCrud<Policy>,
IGetListFilteredByConstraintId<Policy>
{
}
}
@@ -6,7 +6,7 @@

namespace VuelingExam.Common.Logic.Models
{
public class Client
public class Client : LinkContainer
{
public Guid Id { get; set; }
public string Name { get; set; }

0 comments on commit 5366c12

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