Skip to content

Commit 36e8b87

Browse files
feat: Add API versioning sample
1 parent 55ff197 commit 36e8b87

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using GettingThingsDone.ApplicationCore.Helpers;
4+
using GettingThingsDone.Contracts.Dto;
5+
using GettingThingsDone.Contracts.Interface;
6+
using GettingThingsDone.WebApi.Controllers;
7+
using Microsoft.AspNetCore.Mvc;
8+
using Serilog;
9+
using Microsoft.AspNetCore.Mvc.Versioning;
10+
11+
12+
namespace GettingThingsDone.WebApiV2.Controllers
13+
{
14+
15+
[ApiVersion("2.0")]
16+
[Route("api/actions")]
17+
[ApiController]
18+
public class ActionController: BaseController
19+
{
20+
private static class Routes
21+
{
22+
public const string GetActionById = nameof(GetActionById);
23+
}
24+
25+
private readonly IActionService _actionService;
26+
27+
public ActionController(IActionService actionService)
28+
{
29+
_actionService = actionService;
30+
}
31+
32+
[HttpGet]
33+
public async Task<ActionResult<List<ActionDto>>> GetAll()
34+
{
35+
Log.Information("Get all Actions!");
36+
return FromValueServiceResult(await _actionService.GetAll());
37+
}
38+
39+
[HttpGet("{id}", Name = Routes.GetActionById)]
40+
public async Task<ActionResult<ActionDto>> GetById(int id)
41+
{
42+
return FromValueServiceResult(await _actionService.GetAction(id));
43+
}
44+
45+
[HttpPost]
46+
public async Task<IActionResult> Create([FromBody] ActionDto value)
47+
{
48+
Log.Warning("Action create {@value}", value);
49+
50+
if (!value.RepresentsNewEntity)
51+
return BadRequest();
52+
53+
var result = await _actionService.CreateOrUpdate(value);
54+
55+
if (!result.IsOk())
56+
return FromServiceResult(result);
57+
58+
return CreatedAtRoute(Routes.GetActionById, new { id = result.Value.Id }, result.Value);
59+
}
60+
61+
[HttpPut]
62+
public async Task<IActionResult> Update([FromBody] ActionDto value)
63+
{
64+
if (value.RepresentsNewEntity)
65+
return BadRequest();
66+
67+
var result = await _actionService.CreateOrUpdate(value);
68+
69+
if (!result.IsOk())
70+
return FromServiceResult(result);
71+
72+
return NoContent();
73+
}
74+
75+
[HttpDelete("{id}")]
76+
public async Task<IActionResult> Delete(int id)
77+
{
78+
var result = await _actionService.Delete(id);
79+
80+
if (!result.IsOk())
81+
return FromServiceResult(result);
82+
83+
return NoContent();
84+
}
85+
86+
[HttpPut("{id}/move-to/{listId}")]
87+
public async Task<IActionResult> MoveToList(int id, int listId)
88+
{
89+
var result = await _actionService.MoveToList(id, listId);
90+
91+
if (!result.IsOk())
92+
return FromServiceResult(result);
93+
94+
return NoContent();
95+
}
96+
97+
[HttpPut("{id}/assign-to/{projectId}")]
98+
public async Task<IActionResult> AssignToProject(int id, int projectId)
99+
{
100+
var result = await _actionService.AssignToProject(id, projectId);
101+
102+
if (!result.IsOk())
103+
return FromServiceResult(result);
104+
105+
return NoContent();
106+
}
107+
}
108+
}

src/GettingThingsDone.WebApi/GettingThingsDone.WebApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<ItemGroup>
2020
<PackageReference Include="Microsoft.AspNetCore.App" />
2121
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
22+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.3.0" />
2223
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
2324
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
2425
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />

src/GettingThingsDone.WebApi/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Builder;
88
using Microsoft.AspNetCore.Hosting;
99
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.AspNetCore.Mvc.Versioning;
1011
using Microsoft.EntityFrameworkCore;
1112
using Microsoft.EntityFrameworkCore.Storage;
1213
using Microsoft.Extensions.Configuration;
@@ -61,6 +62,12 @@ public void ConfigureServices(IServiceCollection services)
6162
});
6263
//register OlderThen Handler
6364
services.AddSingleton<IAuthorizationHandler, OlderThenHandler>();
65+
//versioning
66+
services.AddApiVersioning(v =>
67+
{
68+
v.AssumeDefaultVersionWhenUnspecified = true;
69+
v.ApiVersionReader = new HeaderApiVersionReader("api-version");
70+
});
6471

6572
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
6673
}

0 commit comments

Comments
 (0)