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

Commit a002949

Browse files
committed
Added foundation for describing Web API, as well as migration
1 parent 17dde0c commit a002949

File tree

11 files changed

+870
-4
lines changed

11 files changed

+870
-4
lines changed

Exam.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exam.Infrastructure", "Src\
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exam.Persistence", "Src\Persistence\Exam.Persistence.csproj", "{498657F6-2A48-4721-AE4D-43814071C472}"
2121
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exam.Clients.WebApi", "Src\Clients\WebAPI\Exam.Clients.WebApi.csproj", "{9CA5C67C-BE40-479E-9073-13112F7AB732}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +43,10 @@ Global
4143
{498657F6-2A48-4721-AE4D-43814071C472}.Debug|Any CPU.Build.0 = Debug|Any CPU
4244
{498657F6-2A48-4721-AE4D-43814071C472}.Release|Any CPU.ActiveCfg = Release|Any CPU
4345
{498657F6-2A48-4721-AE4D-43814071C472}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{9CA5C67C-BE40-479E-9073-13112F7AB732}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{9CA5C67C-BE40-479E-9073-13112F7AB732}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{9CA5C67C-BE40-479E-9073-13112F7AB732}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{9CA5C67C-BE40-479E-9073-13112F7AB732}.Release|Any CPU.Build.0 = Release|Any CPU
4450
EndGlobalSection
4551
GlobalSection(SolutionProperties) = preSolution
4652
HideSolutionNode = FALSE
@@ -52,6 +58,7 @@ Global
5258
{F0B6131C-BEC7-4C50-B6B1-252846C2CCA0} = {A281748D-C6AC-4CB4-BA6A-54D45317CA47}
5359
{2094A04E-4CA0-4FF3-9E19-FB0A9F88F3B9} = {99E2E88C-67B8-4148-A406-1B4F4A8A98DA}
5460
{498657F6-2A48-4721-AE4D-43814071C472} = {99E2E88C-67B8-4148-A406-1B4F4A8A98DA}
61+
{9CA5C67C-BE40-479E-9073-13112F7AB732} = {5B32103E-EC77-40D2-99CC-5DE9C8299C41}
5562
EndGlobalSection
5663
GlobalSection(ExtensibilityGlobals) = postSolution
5764
SolutionGuid = {7E938145-EDBD-4DC8-BCFA-00934E3E571F}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="Controllers\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />
13+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.3" />
14+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="3.1.3" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\Infrastructure\Exam.Infrastructure.csproj" />
19+
<ProjectReference Include="..\..\Persistence\Exam.Persistence.csproj" />
20+
</ItemGroup>
21+
22+
23+
</Project>

Src/Clients/WebAPI/Program.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Exam.Application.Storage.Seeding;
5+
using Exam.Persistence.Context;
6+
using MediatR;
7+
using Microsoft.AspNetCore;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Logging;
13+
14+
namespace Exam.Clients.WebApi
15+
{
16+
// ReSharper disable once ClassNeverInstantiated.Global
17+
public class Program
18+
{
19+
public static async Task Main(string[] args)
20+
{
21+
var host = CreateWebHostBuilder(args).Build();
22+
23+
using (var scope = host.Services.CreateScope())
24+
{
25+
var services = scope.ServiceProvider;
26+
27+
try
28+
{
29+
services.GetRequiredService<FilmsDbContext>().Database.Migrate();
30+
await services.GetRequiredService<IMediator>().Send(new SeedingCommand(), CancellationToken.None);
31+
}
32+
catch (Exception ex)
33+
{
34+
scope.ServiceProvider
35+
.GetRequiredService<ILogger<Program>>()
36+
.LogError(ex, "An error occurred while migrating or initializing the database.");
37+
}
38+
}
39+
40+
host.Run();
41+
}
42+
43+
// ReSharper disable once MemberCanBePrivate.Global
44+
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
45+
{
46+
return WebHost.CreateDefaultBuilder(args)
47+
.ConfigureAppConfiguration((hostingContext, config) =>
48+
{
49+
config.AddJsonFile("appsettings.json", true, true)
50+
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json",
51+
true, true)
52+
.AddJsonFile("appsettings.Local.json", true, true);
53+
54+
config.AddEnvironmentVariables();
55+
}).UseStartup<Startup>();
56+
}
57+
}
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:59812",
8+
"sslPort": 44306
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "weatherforecast",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"Exam.Clients.WebApi": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"launchUrl": "weatherforecast",
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}

Src/Clients/WebAPI/Startup.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Exam.Application;
2+
using Exam.Application.Common.Interfaces;
3+
using Exam.Infrastructure;
4+
using Exam.Persistence;
5+
using Exam.Persistence.Context;
6+
using FluentValidation.AspNetCore;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Hosting;
13+
14+
namespace Exam.Clients.WebApi
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
19+
{
20+
Configuration = configuration;
21+
Environment = environment;
22+
}
23+
24+
public IConfiguration Configuration { get; }
25+
public IWebHostEnvironment Environment { get; }
26+
27+
// This method gets called by the runtime. Use this method to add services to the container.
28+
public void ConfigureServices(IServiceCollection services)
29+
{
30+
services.AddInfrastructure();
31+
services.AddPersistence(Configuration);
32+
services.AddApplication();
33+
34+
services.AddHealthChecks().AddDbContextCheck<FilmsDbContext>();
35+
36+
services.AddHttpContextAccessor();
37+
38+
services.AddControllers()
39+
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<IFilmsDbContext>());
40+
41+
services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);
42+
}
43+
44+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
45+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
46+
{
47+
if (env.IsDevelopment())
48+
{
49+
app.UseDeveloperExceptionPage();
50+
app.UseDatabaseErrorPage();
51+
}
52+
else
53+
{
54+
app.UseHsts();
55+
}
56+
57+
app.UseHealthChecks("/health");
58+
app.UseHttpsRedirection();
59+
60+
app.UseRouting();
61+
62+
app.UseAuthorization();
63+
64+
app.UseEndpoints(endpoints => endpoints.MapControllers());
65+
}
66+
}
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"ConnectionStrings": {
3+
"FilmsDatabase": "Server=.;Database=FilmsTest;Trusted_Connection=True;MultipleActiveResultSets=true;"
4+
},
5+
"Logging": {
6+
"LogLevel": {
7+
"Default": "Warning"
8+
}
9+
},
10+
"AllowedHosts": "*"
11+
}

Src/Persistence/Exam.Persistence.csproj

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

7-
<ItemGroup>
8-
<Folder Include="Migrations\" />
9-
</ItemGroup>
10-
117
<ItemGroup>
128
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3">
139
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)