Skip to content

Commit fe150ec

Browse files
committed
Convert to .NET Core 3.1
1 parent 9abbe70 commit fe150ec

7 files changed

Lines changed: 45 additions & 183 deletions

File tree

webapi/Controllers/AuthorsController.cs

Lines changed: 0 additions & 124 deletions
This file was deleted.

webapi/Controllers/BooksController.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Data;
4-
using System.Data.Entity.Infrastructure;
51
using System.Linq;
62
using System.Net;
7-
using System.Net.Http;
83
using System.Threading.Tasks;
9-
using System.Web.Http;
10-
using System.Web.Http.Description;
114
using webapi.Data;
125
using webapi.Models;
136
using Microsoft.EntityFrameworkCore;
147
using Microsoft.AspNetCore.Mvc;
158

169
namespace webapi.Controllers
1710
{
11+
[Route("api/[controller]")]
1812
[ApiController]
1913
public class BooksController : ControllerBase
2014
{
2115
private webapiContext db = new webapiContext();
2216
// GET: api/Books
17+
[HttpGet]
2318
public IQueryable<Book> GetBooks()
2419
{
2520
return db.Books.Include(b => b.Author);
2621
}
2722

2823
// GET: api/Books/5
29-
[ResponseType(typeof(Book))]
30-
public async Task<IHttpActionResult> GetBook(int id)
24+
[HttpGet("{id}")]
25+
public async Task<ActionResult<Book>> GetBook(int id)
3126
{
3227
Book book = await db.Books.FindAsync(id);
3328
if (book == null)
@@ -39,8 +34,8 @@ public async Task<IHttpActionResult> GetBook(int id)
3934
}
4035

4136
// PUT: api/Books/5
42-
[ResponseType(typeof(void))]
43-
public async Task<IHttpActionResult> PutBook(int id, Book book)
37+
[HttpPut("{id}")]
38+
public async Task<ActionResult> PutBook(int id, Book book)
4439
{
4540
if (!ModelState.IsValid)
4641
{
@@ -69,12 +64,12 @@ public async Task<IHttpActionResult> PutBook(int id, Book book)
6964
}
7065
}
7166

72-
return StatusCode(HttpStatusCode.NoContent);
67+
return NoContent();
7368
}
7469

7570
// POST: api/Books
76-
[ResponseType(typeof(Book))]
77-
public async Task<IHttpActionResult> PostBook(Book book)
71+
[HttpPost("{id}")]
72+
public async Task<ActionResult<Book>> PostBook(Book book)
7873
{
7974
if (!ModelState.IsValid)
8075
{
@@ -92,8 +87,8 @@ public async Task<IHttpActionResult> PostBook(Book book)
9287
}
9388

9489
// DELETE: api/Books/5
95-
[ResponseType(typeof(Book))]
96-
public async Task<IHttpActionResult> DeleteBook(int id)
90+
[HttpDelete("{id}")]
91+
public async Task<ActionResult<Book>> DeleteBook(int id)
9792
{
9893
Book book = await db.Books.FindAsync(id);
9994
if (book == null)
@@ -106,16 +101,6 @@ public async Task<IHttpActionResult> DeleteBook(int id)
106101
return Ok(book);
107102
}
108103

109-
protected override void Dispose(bool disposing)
110-
{
111-
if (disposing)
112-
{
113-
db.Dispose();
114-
}
115-
116-
base.Dispose(disposing);
117-
}
118-
119104
private bool BookExists(int id)
120105
{
121106
return db.Books.Count(e => e.Id == id) > 0;

webapi/Data/webapiConnectionStringBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public static string ConnectionString
2626
static webapiConnectionStringBuilder()
2727
{
2828
var client = new AmazonSecretsManagerClient();
29-
var response = client.GetSecretValue(new GetSecretValueRequest{SecretId = Environment.GetEnvironmentVariable("DATABASE_CREDENTIALS_SECRET_ARN")});
29+
var responseTask = client.GetSecretValueAsync(new GetSecretValueRequest{SecretId = $"{Environment.GetEnvironmentVariable("SECRETS_NAMESPACE")}dotnet/Database/SAUser"});
30+
var response = responseTask.GetAwaiter().GetResult();
3031
var credentials = JsonConvert.DeserializeObject<Credentials>(response.SecretString);
31-
var builder = new SqlConnectionStringBuilder{DataSource = Environment.GetEnvironmentVariable("DATABASE_ADDRESS"), UserID = credentials.username, Password = credentials.password, InitialCatalog = "books"};
32+
var builder = new SqlConnectionStringBuilder{DataSource = Environment.GetEnvironmentVariable("DB_ADDRESS"), UserID = credentials.username, Password = credentials.password, InitialCatalog = "books"};
3233
_connectionString = builder.ToString();
3334
}
3435
}

webapi/Data/webapiContext.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@ namespace webapi.Data
44
{
55
public class webapiContext : DbContext
66
{
7-
public webapiContext(): base(webapiConnectionStringBuilder.ConnectionString)
8-
{
9-
Database.SetInitializer(new webapiInitializer());
10-
}
11-
12-
public System.Data.Entity.DbSet<webapi.Models.Author> Authors
7+
public DbSet<webapi.Models.Author> Authors
138
{
149
get;
1510
set;
1611
}
1712

18-
public System.Data.Entity.DbSet<webapi.Models.Book> Books
13+
public DbSet<webapi.Models.Book> Books
1914
{
2015
get;
2116
set;
2217
}
2318

2419
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2520
{ /* Use Configuration from static ConfigurationManager to access connection string in appsettings.json. Example below: optionsBuilder.UseSqlServer(ConfigurationManager.Configuration.GetConnectionString("CONNECTIONSTRINGNAME")); */
21+
optionsBuilder.UseSqlServer(webapiConnectionStringBuilder.ConnectionString);
2622
base.OnConfiguring(optionsBuilder);
2723
}
2824
}

webapi/Data/webapiInitializer.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

webapi/LambdaEntryPoint.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
3+
namespace webapi
4+
{
5+
/// <summary>
6+
/// This class extends from APIGatewayProxyFunction which contains the method FunctionHandlerAsync which is the
7+
/// actual Lambda function entry point. The Lambda handler field should be set to
8+
///
9+
/// webapi::webapi.LambdaEntryPoint::FunctionHandlerAsync
10+
/// </summary>
11+
public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction
12+
{
13+
/// <summary>
14+
/// The builder has configuration, logging and Amazon API Gateway already configured. The startup class
15+
/// needs to be configured in this method using the UseStartup<>() method.
16+
/// </summary>
17+
/// <param name="builder"></param>
18+
protected override void Init(IWebHostBuilder builder)
19+
{
20+
builder
21+
.UseStartup<Startup>();
22+
}
23+
}
24+
}

webapi/webapi.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="5.3.1" />
9+
<PackageReference Include="AWSSDK.SecretsManager" Version="3.5.0.72" />
810
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="*" />
911
<PackageReference Include="Microsoft.Data.SqlClient" Version="*" />
1012
<PackageReference Include="EntityFramework" Version="6.4.4" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
14+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1115

1216
</ItemGroup>
1317
</Project>

0 commit comments

Comments
 (0)