Skip to content

Commit 14870c9

Browse files
authored
Merge pull request #3 from csharpfritz/feature_Update31
Feature update31
2 parents 47986b8 + e53f10c commit 14870c9

File tree

32 files changed

+251
-165
lines changed

32 files changed

+251
-165
lines changed

docs/1. Create BackEnd API project.md

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
> ***Note:* If not using Visual Studio, create the project using `dotnet new webapi` at the cmd line, details as follows:**
99
> 1. Create folder ConferencePlanner and call `dotnet new sln` at the cmd line to create a solution
1010
> 2. Create sub-folder BackEnd and create a project using `dotnet new webapi` at the cmd line inside the folder BackEnd
11-
> 3. Add the project to the solution using `dotnet sln add BackEnd/BackEnd.csproj`
12-
1. Add a new `Models` folder to the root of the application.
13-
1. Add a new `Speaker` class using the following code:
11+
> 3. From the ConferencePlanner folder, add the project to the solution using `dotnet sln add BackEnd/BackEnd.csproj`
12+
1. Add a new `Models` folder to the BackEnd project.
13+
1. Add a new `Speaker` class inside the Models folder using the following code:
1414
```csharp
1515
using System;
1616
using System.Collections.Generic;
@@ -36,10 +36,10 @@
3636
}
3737
}
3838
```
39-
1. Add a reference to the NuGet package `Microsoft.EntityFrameworkCore.SqlServer` version `3.0.0`.
40-
> This can be done from the command line using `dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.0.0`
41-
1. Add a reference to the NuGet package `Microsoft.EntityFrameworkCore.Sqlite` version `3.0.0`.
42-
> This can be done from the command line using `dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 3.0.0`
39+
1. Add a reference in the BackEnd project to the NuGet package `Microsoft.EntityFrameworkCore.SqlServer` version `3.1.3`.
40+
> This can be done from the command line in the `BackEnd` folder using `dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.1.3`
41+
1. Add a reference in the BackEnd project to the NuGet package `Microsoft.EntityFrameworkCore.Sqlite` version `3.1.3`.
42+
> This can be done from the command line in the `BackEnd` folder using `dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 3.1.3`
4343
1. Next we'll create a new Entity Framework DbContext. Create a new `ApplicationDbContext` class in the `Models` folder using the following code:
4444
```csharp
4545
using Microsoft.EntityFrameworkCore;
@@ -59,7 +59,7 @@
5959
}
6060

6161
```
62-
1. Add a connection string to the appsettings.json file for this database:
62+
1. Add a connection string to the `appsettings.json` file for this database:
6363

6464
```json
6565
{
@@ -93,11 +93,17 @@
9393
});
9494
```
9595
> This code registers the `ApplicationDbContext` service so it can be injected into controllers. Additionally, it configures operating system specific database technologies and connection strings
96+
1. Ensure the following 'using' statements are at the top of the `Startup.cs` file to allow the references in our code to work:
97+
```csharp
98+
using BackEnd.Models;
99+
using System.Runtime.InteropServices;
100+
using Microsoft.EntityFrameworkCore;
101+
```
96102

97103
## Configuring EF Migrations
98104

99-
1. Add a reference to the NuGet package `Microsoft.EntityFrameworkCore.Tools` version `3.0.0`.
100-
> **If you're not using Visual Studio** install the package from the command line with `dotnet add package Microsoft.EntityFrameworkCore.Tools --version 3.0.0`
105+
1. Add a reference to the NuGet package `Microsoft.EntityFrameworkCore.Tools` version `3.1.3`.
106+
> **If you're not using Visual Studio** install the package from the command line in the `BackEnd` folder with `dotnet add package Microsoft.EntityFrameworkCore.Tools --version 3.1.3`
101107

102108
### Visual Studio: Package Manager Console
103109

@@ -111,9 +117,9 @@
111117

112118
### Command line
113119

114-
1. Install the EntityFramework global tool `dotnet-ef` using the following command:
120+
1. Install the EntityFramework global tool `dotnet-ef` using the following command in the `BackEnd` folder:
115121
```console
116-
dotnet tool install -g dotnet-ef --version 3.0.0
122+
dotnet tool install -g dotnet-ef --version 3.1.3
117123
```
118124

119125
1. Open a command prompt and navigate to the project directory. (The directory containing the `Startup.cs` file).
@@ -147,12 +153,12 @@ First, open the `Controllers` folder and take a quick look at the `WeatherForeca
147153
### Using the cmd line
148154
1. Install the "Microsoft.VisualStudio.Web.CodeGeneration.Design" package
149155
```console
150-
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.0.0
156+
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.1.2
151157
```
152158

153159
1. Install the `aspnet-codegenerator` global tool by running the following command:
154160
```console
155-
dotnet tool install -g dotnet-aspnet-codegenerator --version 3.0.0
161+
dotnet tool install -g dotnet-aspnet-codegenerator --version 3.1.2
156162
```
157163

158164
> Note: You will need to close and reopen the console window to be able to use this tool.
@@ -171,8 +177,8 @@ In this section, we'll be adding documentation to our API using the Swashbuckle
171177

172178
Additional information on using Swashbuckle in ASP.NET Core is available in this tutorial: [ASP.NET Web API Help Pages using Swagger](https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger)
173179

174-
1. Add a reference to the NuGet package `Swashbuckle.AspNetCore` version `5.0.0-rc4`.
175-
> This can be done from the command line using `dotnet add package Swashbuckle.AspNetCore --version 5.0.0-rc4`
180+
1. Add a reference to the NuGet package `Swashbuckle.AspNetCore` version `5.3.0`.
181+
> This can be done from the command line using `dotnet add package Swashbuckle.AspNetCore --version 5.3.0`
176182

177183
1. Add the Swashbuckle services in your `ConfigureServices` method:
178184
```csharp
@@ -182,6 +188,14 @@ Additional information on using Swashbuckle in ASP.NET Core is available in this
182188
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Conference Planner API", Version = "v1" })
183189
);
184190
```
191+
192+
1. Ensure your Startup.cs file contains the following 'using' statements:
193+
```csharp
194+
using Microsoft.OpenApi.Models;
195+
using System.Threading.Tasks;
196+
```
197+
198+
185199
1. Configure Swashbuckle by adding the following lines just before `UseRouting` in the `Configure` method in `Startup.cs`:
186200
```csharp
187201
app.UseSwagger();
@@ -190,17 +204,18 @@ Additional information on using Swashbuckle in ASP.NET Core is available in this
190204
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Conference Planner API v1")
191205
);
192206
```
193-
> ***Note:* Due to how the middleware and pipeline are structured, you'll want to place this before the `app.UseEndpoints()` statement.**
194-
1. Add a redirect to the end of the pipeline that redirects to the swagger end point.
207+
1. Add a MapGet to the beginning of the `UseEndpoints` statement in the pipeline that redirects requests from the root of our application to the swagger end point.
195208
```csharp
196-
app.Run(context =>
197-
{
198-
context.Response.Redirect("/swagger");
199-
return Task.CompletedTask;
200-
});
201-
```
202-
203-
> ***Note:* You'll need to place this after the `app.UseMvc()` statement.**
209+
app.UseEndpoints(endpoints =>
210+
{
211+
endpoints.MapGet("/", context => {
212+
context.Response.Redirect("/swagger/");
213+
return Task.CompletedTask;
214+
});
215+
endpoints.MapControllers();
216+
});
217+
```
218+
204219
1. Run the application (F5 in Visual Studio or `dotnet run` from console).
205220
1. Browse to the Swagger UI at `http://localhost:<random_port>/swagger`.
206221
![](images/swagger-speakers.png)
@@ -210,9 +225,9 @@ Additional information on using Swashbuckle in ASP.NET Core is available in this
210225
![](images/swagger-create-speaker.png)
211226
```json
212227
{
213-
"name": "Tyrion Lannister",
214-
"bio": "Drinks and knows things",
215-
"webSite": "http://giphy.com/search/tyrion-lannister"
228+
"name": "Dorothy Gale",
229+
"bio": "Amateur meteorologist, Oz aficionado, and lead guide for Whirlwind Tours of Kansas",
230+
"webSite": "https://en.wikipedia.org/wiki/Dorothy_Gale"
216231
}
217232
```
218233
1. When you click the *Try it out!* button, you should see a success response from the server. Now, clicking the *GET* button above should show your newly added speaker.

docs/2. Build out BackEnd and Refactor.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ In this session, we'll add the rest of our models and controllers that expose th
3131
## Refactoring the Speaker model into the ConferenceDTO project
3232
1. Copy the `Speaker.cs` class from the *BackEnd* application into the root of the new ConferenceDTO project, and change the namespace to `ConferenceDTO`.
3333
1. The data annotations references should be broken at this point, to resovle it, we need to add a nuget the missing NuGet package into the `ConferenceDTO` project.
34-
1. Add a reference to the NuGet package `System.ComponentModel.Annotations` version `4.6.0`.
35-
> This can be done from the command line using `dotnet add package System.ComponentModel.Annotations --version 4.6.0`
34+
1. Add a reference to the NuGet package `System.ComponentModel.Annotations` version `4.7.0`.
35+
> This can be done from the command line using `dotnet add package System.ComponentModel.Annotations --version 4.7.0`
3636
3737
1. When the package restore completes, you should see that your data annotations are now resolved.
3838
1. Go back to the *BackEnd* application and modify the code in `Speaker.cs` as shown:

docs/4. Add auth features.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ In this module we're going to add the capability for users to register and sign-
1818
```
1919
dotnet tool install -g dotnet-aspnet-codegenerator
2020
```
21-
1. Add the `Microsoft.VisualStudio.Web.CodeGeneration.Design` version `3.0.0` package to the project.
21+
1. Add the `Microsoft.VisualStudio.Web.CodeGeneration.Design` version `3.1.2` package to the project.
2222
```
23-
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.0.0
23+
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.1.2
2424
```
2525
1. Run this command to generate the code to use Identity.
2626
```

docs/6. Production Readiness and Deployment.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Production Readiness and Deployment
22

33
## Adding EF Healthchecks to the BackEnd
4-
1. Add a reference to the NuGet package `Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore` version `3.0.0`.
5-
> This can be done from the command line using `dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore --version 3.0.0`
4+
1. Add a reference to the NuGet package `Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore` version `3.1.3`.
5+
> This can be done from the command line using `dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore --version 3.1.3`
66
1. Add a DbContext health check in `Startup.cs` by adding the following code to `ConfigureServices`:
77
```csharp
88
services.AddHealthChecks()
@@ -13,7 +13,7 @@
1313

1414
## Adding EF Healthchecks to the FrontEnd
1515
1. Add a reference to the NuGet package `Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore` version `3.0.0`.
16-
> This can be done from the command line using `dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore --version 3.0.0`
16+
> This can be done from the command line using `dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore --version 3.1.3`
1717
1818
> Note: The `FrontEnd` and `BackEnd` projects have different versions of this package because they reference different versions of EntityFrameworkCore.
1919
1. Add a DbContext health check in `Startup.cs` by adding the following code to `ConfigureServices`:
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
9-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
11-
<PrivateAssets>all</PrivateAssets>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
1211
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13-
</PackageReference>
14-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0">
1512
<PrivateAssets>all</PrivateAssets>
16-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1713
</PackageReference>
18-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
14+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.0" />
1916
</ItemGroup>
2017

18+
2119
</Project>

save-points/1-Create-API-and-EF-Model/ConferencePlanner/BackEnd/Controllers/SpeakersController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using System.Threading.Tasks;
44
using Microsoft.AspNetCore.Mvc;
@@ -7,7 +7,7 @@
77

88
namespace BackEnd.Controllers
99
{
10-
[Route("api/[controller]")]
10+
[Route("api/[controller]")]
1111
[ApiController]
1212
public class SpeakersController : ControllerBase
1313
{
@@ -40,6 +40,8 @@ public async Task<ActionResult<Speaker>> GetSpeaker(int id)
4040
}
4141

4242
// PUT: api/Speakers/5
43+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
44+
// more details see https://aka.ms/RazorPagesCRUD.
4345
[HttpPut("{id}")]
4446
public async Task<IActionResult> PutSpeaker(int id, Speaker speaker)
4547
{
@@ -70,6 +72,8 @@ public async Task<IActionResult> PutSpeaker(int id, Speaker speaker)
7072
}
7173

7274
// POST: api/Speakers
75+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
76+
// more details see https://aka.ms/RazorPagesCRUD.
7377
[HttpPost]
7478
public async Task<ActionResult<Speaker>> PostSpeaker(Speaker speaker)
7579
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace BackEnd.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
22+
{
23+
_logger = logger;
24+
}
25+
26+
[HttpGet]
27+
public IEnumerable<WeatherForecast> Get()
28+
{
29+
var rng = new Random();
30+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31+
{
32+
Date = DateTime.Now.AddDays(index),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = Summaries[rng.Next(Summaries.Length)]
35+
})
36+
.ToArray();
37+
}
38+
}
39+
}
Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.EntityFrameworkCore.Metadata;
2-
using Microsoft.EntityFrameworkCore.Migrations;
1+
using Microsoft.EntityFrameworkCore.Migrations;
32

43
namespace BackEnd.Migrations
54
{
@@ -12,7 +11,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
1211
columns: table => new
1312
{
1413
Id = table.Column<int>(nullable: false)
15-
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
14+
.Annotation("SqlServer:Identity", "1, 1"),
1615
Name = table.Column<string>(maxLength: 200, nullable: false),
1716
Bio = table.Column<string>(maxLength: 4000, nullable: true),
1817
WebSite = table.Column<string>(maxLength: 1000, nullable: true)

save-points/1-Create-API-and-EF-Model/ConferencePlanner/BackEnd/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1414
{
1515
#pragma warning disable 612, 618
1616
modelBuilder
17-
.HasAnnotation("ProductVersion", "3.0.0")
17+
.HasAnnotation("ProductVersion", "3.1.0")
1818
.HasAnnotation("Relational:MaxIdentifierLength", 128)
1919
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
2020

2121
modelBuilder.Entity("BackEnd.Models.Speaker", b =>
2222
{
2323
b.Property<int>("Id")
2424
.ValueGeneratedOnAdd()
25+
.HasColumnType("int")
2526
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
2627

2728
b.Property<string>("Bio")
29+
.HasColumnType("nvarchar(4000)")
2830
.HasMaxLength(4000);
2931

3032
b.Property<string>("Name")
3133
.IsRequired()
34+
.HasColumnType("nvarchar(200)")
3235
.HasMaxLength(200);
3336

3437
b.Property<string>("WebSite")
38+
.HasColumnType("nvarchar(1000)")
3539
.HasMaxLength(1000);
3640

3741
b.HasKey("Id");

save-points/1-Create-API-and-EF-Model/ConferencePlanner/BackEnd/Models/ApplicationDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
22

33
namespace BackEnd.Models
44
{

0 commit comments

Comments
 (0)