Skip to content
This repository was archived by the owner on Oct 16, 2022. It is now read-only.

Commit 7d7a60d

Browse files
committed
Foundation for writing a Web API
1 parent a002949 commit 7d7a60d

File tree

10 files changed

+78
-15
lines changed

10 files changed

+78
-15
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using MediatR;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace Exam.Clients.WebApi.Controllers
6+
{
7+
[ApiController]
8+
[Route("api/[controller]/[action]")]
9+
public abstract class BaseController : ControllerBase
10+
{
11+
private IMediator _mediator;
12+
13+
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
using Exam.Application.Storage.Films.Queries.Get.AsList;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace Exam.Clients.WebApi.Controllers
7+
{
8+
public class FilmsController : BaseController
9+
{
10+
[HttpGet]
11+
[ProducesResponseType(StatusCodes.Status200OK)]
12+
public async Task<ActionResult<FilmsListViewModel>> GetAll()
13+
{
14+
return Ok(await Mediator.Send(new GetFilmsAsListQuery()));
15+
}
16+
}
17+
}

Src/Clients/WebAPI/Exam.Clients.WebApi.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<Folder Include="Controllers\" />
9-
</ItemGroup>
10-
117
<ItemGroup>
128
<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />
139
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.3" />
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
1411
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="3.1.3" />
12+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
1513
</ItemGroup>
1614

1715
<ItemGroup>
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{
2-
"$schema": "http://json.schemastore.org/launchsettings.json",
1+
{
32
"iisSettings": {
43
"windowsAuthentication": false,
54
"anonymousAuthentication": true,
@@ -8,11 +7,11 @@
87
"sslPort": 44306
98
}
109
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
1111
"profiles": {
1212
"IIS Express": {
1313
"commandName": "IISExpress",
1414
"launchBrowser": true,
15-
"launchUrl": "weatherforecast",
1615
"environmentVariables": {
1716
"ASPNETCORE_ENVIRONMENT": "Development"
1817
}
@@ -21,10 +20,10 @@
2120
"commandName": "Project",
2221
"launchBrowser": true,
2322
"launchUrl": "weatherforecast",
24-
"applicationUrl": "https://localhost:5001;http://localhost:5000",
2523
"environmentVariables": {
2624
"ASPNETCORE_ENVIRONMENT": "Development"
27-
}
25+
},
26+
"applicationUrl": "https://localhost:5001;http://localhost:5000"
2827
}
2928
}
3029
}

Src/Core/Application/Consts.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
namespace Exam.Application
1+
using System.IO;
2+
3+
namespace Exam.Application
24
{
35
public static class Consts
46
{
5-
public static string PathMockFilms => "";
7+
public static string FilmsMockPath
8+
{
9+
get
10+
{
11+
var split = Path.DirectorySeparatorChar;
12+
var up = $"{split}..";
13+
return $"{Directory.GetCurrentDirectory()}{up}{up}{up}{split}mocks{split}seeding{split}mock-films.json";
14+
}
15+
}
616
}
717
}

Src/Core/Application/Storage/Seeding/JsonMocksSeeder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task SeedAllAsync(CancellationToken cancellationToken)
2121
{
2222
if (_context.Films.Any()) return;
2323

24-
await SeedMocksAsync(Consts.PathMockFilms, cancellationToken);
24+
await SeedMocksAsync(Consts.FilmsMockPath, cancellationToken);
2525
}
2626

2727
private async Task SeedMocksAsync(string filePath, CancellationToken cancellationToken)

Src/Infrastructure/DependencyInjection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ namespace Exam.Infrastructure
77
{
88
public static class DependencyInjection
99
{
10+
// ReSharper disable once UnusedMethodReturnValue.Global
1011
public static IServiceCollection AddInfrastructure(this IServiceCollection self)
1112
{
12-
self.AddTransient<IJsonMocksReader<Film>, JsonMocksReader<Film>>();
13+
self.AddTransient<IJsonMocksReader<Film>, JsonFilmsMockReader>();
1314

1415
return self;
1516
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Exam.Domain.Entities;
6+
7+
namespace Exam.Infrastructure.MockReaders
8+
{
9+
public class JsonFilmsMockReader : JsonMockReader<Film>
10+
{
11+
public override async Task<IEnumerable<Film>> ReadAsync(string filePath, CancellationToken cancellationToken)
12+
{
13+
return await await Task.Factory.StartNew(
14+
async () => (await DeserializeAsync(filePath, cancellationToken))
15+
.GroupBy(x => x.Title)
16+
.Select(x => x.FirstOrDefault()), cancellationToken);
17+
}
18+
}
19+
}

Src/Infrastructure/MockReaders/JsonMocksReader.cs renamed to Src/Infrastructure/MockReaders/JsonMockReader.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
namespace Exam.Infrastructure.MockReaders
99
{
10-
public class JsonMocksReader<TEntity> : IJsonMocksReader<TEntity> where TEntity : class, new()
10+
public abstract class JsonMockReader<TEntity> : IJsonMocksReader<TEntity> where TEntity : class, new()
1111
{
12-
public async Task<IEnumerable<TEntity>> ReadAsync(string filePath, CancellationToken cancellationToken)
12+
public abstract Task<IEnumerable<TEntity>> ReadAsync(string filePath, CancellationToken cancellationToken);
13+
14+
protected async Task<IEnumerable<TEntity>> DeserializeAsync(string filePath,
15+
CancellationToken cancellationToken)
1316
{
1417
return await await Task.Factory.StartNew(
1518
async () => JsonConvert.DeserializeObject<IEnumerable<TEntity>>(

mocks/seeding/mock-films.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)