Skip to content

Commit 9abbe70

Browse files
committed
Merge remote-tracking branch 'txase/partially-ported' into core
2 parents 27da0f2 + 53f2e30 commit 9abbe70

14 files changed

Lines changed: 176 additions & 353 deletions

webapi/App_Start/WebApiConfig.cs

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

webapi/Controllers/AuthorsController.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
using System.Data.Entity;
54
using System.Data.Entity.Infrastructure;
65
using System.Linq;
76
using System.Net;
@@ -11,13 +10,15 @@
1110
using System.Web.Http.Description;
1211
using webapi.Data;
1312
using webapi.Models;
13+
using Microsoft.EntityFrameworkCore;
14+
using Microsoft.AspNetCore.Mvc;
1415

1516
namespace webapi.Controllers
1617
{
17-
public class AuthorsController : ApiController
18+
[ApiController]
19+
public class AuthorsController : ControllerBase
1820
{
1921
private webapiContext db = new webapiContext();
20-
2122
// GET: api/Authors
2223
public IQueryable<Author> GetAuthors()
2324
{
@@ -52,7 +53,6 @@ public async Task<IHttpActionResult> PutAuthor(int id, Author author)
5253
}
5354

5455
db.Entry(author).State = EntityState.Modified;
55-
5656
try
5757
{
5858
await db.SaveChangesAsync();
@@ -83,8 +83,12 @@ public async Task<IHttpActionResult> PostAuthor(Author author)
8383

8484
db.Authors.Add(author);
8585
await db.SaveChangesAsync();
86+
return CreatedAtRoute("DefaultApi", new
87+
{
88+
id = author.Id
89+
}
8690

87-
return CreatedAtRoute("DefaultApi", new { id = author.Id }, author);
91+
, author);
8892
}
8993

9094
// DELETE: api/Authors/5
@@ -99,7 +103,6 @@ public async Task<IHttpActionResult> DeleteAuthor(int id)
99103

100104
db.Authors.Remove(author);
101105
await db.SaveChangesAsync();
102-
103106
return Ok(author);
104107
}
105108

@@ -109,6 +112,7 @@ protected override void Dispose(bool disposing)
109112
{
110113
db.Dispose();
111114
}
115+
112116
base.Dispose(disposing);
113117
}
114118

webapi/Controllers/BooksController.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data;
4-
using System.Data.Entity;
54
using System.Data.Entity.Infrastructure;
65
using System.Linq;
76
using System.Net;
@@ -11,18 +10,19 @@
1110
using System.Web.Http.Description;
1211
using webapi.Data;
1312
using webapi.Models;
13+
using Microsoft.EntityFrameworkCore;
14+
using Microsoft.AspNetCore.Mvc;
1415

1516
namespace webapi.Controllers
1617
{
17-
public class BooksController : ApiController
18+
[ApiController]
19+
public class BooksController : ControllerBase
1820
{
1921
private webapiContext db = new webapiContext();
20-
2122
// GET: api/Books
2223
public IQueryable<Book> GetBooks()
2324
{
24-
return db.Books
25-
.Include(b => b.Author);
25+
return db.Books.Include(b => b.Author);
2626
}
2727

2828
// GET: api/Books/5
@@ -53,7 +53,6 @@ public async Task<IHttpActionResult> PutBook(int id, Book book)
5353
}
5454

5555
db.Entry(book).State = EntityState.Modified;
56-
5756
try
5857
{
5958
await db.SaveChangesAsync();
@@ -84,8 +83,12 @@ public async Task<IHttpActionResult> PostBook(Book book)
8483

8584
db.Books.Add(book);
8685
await db.SaveChangesAsync();
86+
return CreatedAtRoute("DefaultApi", new
87+
{
88+
id = book.Id
89+
}
8790

88-
return CreatedAtRoute("DefaultApi", new { id = book.Id }, book);
91+
, book);
8992
}
9093

9194
// DELETE: api/Books/5
@@ -100,7 +103,6 @@ public async Task<IHttpActionResult> DeleteBook(int id)
100103

101104
db.Books.Remove(book);
102105
await db.SaveChangesAsync();
103-
104106
return Ok(book);
105107
}
106108

@@ -110,6 +112,7 @@ protected override void Dispose(bool disposing)
110112
{
111113
db.Dispose();
112114
}
115+
113116
base.Dispose(disposing);
114117
}
115118

webapi/Data/webapiConnectionStringBuilder.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using Amazon.SecretsManager;
1+
using Amazon.SecretsManager;
22
using Amazon.SecretsManager.Model;
33
using Newtonsoft.Json;
44
using System;
5-
using System.Data.SqlClient;
5+
using Microsoft.Data.SqlClient;
66

77
namespace webapi.Data
88
{
@@ -15,7 +15,6 @@ private class Credentials
1515
}
1616

1717
private static string _connectionString;
18-
1918
public static string ConnectionString
2019
{
2120
get
@@ -27,21 +26,9 @@ public static string ConnectionString
2726
static webapiConnectionStringBuilder()
2827
{
2928
var client = new AmazonSecretsManagerClient();
30-
var response = client.GetSecretValue(new GetSecretValueRequest
31-
{
32-
SecretId = Environment.GetEnvironmentVariable("DATABASE_CREDENTIALS_SECRET_ARN")
33-
});
34-
29+
var response = client.GetSecretValue(new GetSecretValueRequest{SecretId = Environment.GetEnvironmentVariable("DATABASE_CREDENTIALS_SECRET_ARN")});
3530
var credentials = JsonConvert.DeserializeObject<Credentials>(response.SecretString);
36-
37-
var builder = new SqlConnectionStringBuilder
38-
{
39-
DataSource = Environment.GetEnvironmentVariable("DATABASE_ADDRESS"),
40-
UserID = credentials.username,
41-
Password = credentials.password,
42-
InitialCatalog = "books"
43-
};
44-
31+
var builder = new SqlConnectionStringBuilder{DataSource = Environment.GetEnvironmentVariable("DATABASE_ADDRESS"), UserID = credentials.username, Password = credentials.password, InitialCatalog = "books"};
4532
_connectionString = builder.ToString();
4633
}
4734
}

webapi/Data/webapiContext.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
using System.Data.Entity;
1+
using Microsoft.EntityFrameworkCore;
22

33
namespace webapi.Data
44
{
55
public class webapiContext : DbContext
66
{
7-
public webapiContext() : base(webapiConnectionStringBuilder.ConnectionString)
7+
public webapiContext(): base(webapiConnectionStringBuilder.ConnectionString)
88
{
99
Database.SetInitializer(new webapiInitializer());
1010
}
1111

12-
public System.Data.Entity.DbSet<webapi.Models.Author> Authors { get; set; }
12+
public System.Data.Entity.DbSet<webapi.Models.Author> Authors
13+
{
14+
get;
15+
set;
16+
}
17+
18+
public System.Data.Entity.DbSet<webapi.Models.Book> Books
19+
{
20+
get;
21+
set;
22+
}
1323

14-
public System.Data.Entity.DbSet<webapi.Models.Book> Books { get; set; }
24+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25+
{ /* Use Configuration from static ConfigurationManager to access connection string in appsettings.json. Example below: optionsBuilder.UseSqlServer(ConfigurationManager.Configuration.GetConnectionString("CONNECTIONSTRINGNAME")); */
26+
base.OnConfiguring(optionsBuilder);
27+
}
1528
}
16-
}
29+
}

webapi/Data/webapiInitializer.cs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,24 @@
1-
using System.Data.Entity;
21
using System.Data.Entity.Migrations;
32
using webapi.Models;
3+
using Microsoft.EntityFrameworkCore;
44

55
namespace webapi.Data
66
{
77
public class webapiInitializer : CreateDatabaseIfNotExists<webapiContext>
88
{
99
protected override void Seed(webapiContext context)
1010
{
11-
context.Authors.AddOrUpdate(
12-
x => x.Id,
13-
new Author() { Id = 1, Name = "Jane Austen" },
14-
new Author() { Id = 2, Name = "Charles Dickens" },
15-
new Author() { Id = 3, Name = "Miguel de Cervantes" }
16-
);
17-
18-
context.Books.AddOrUpdate(x => x.Id,
19-
new Book()
20-
{
21-
Id = 1,
22-
Title = "Pride and Prejudice",
23-
Year = 1813,
24-
AuthorId = 1,
25-
Price = 9.99M,
26-
Genre = "Comedy of manners"
27-
},
28-
new Book()
29-
{
30-
Id = 2,
31-
Title = "Northanger Abbey",
32-
Year = 1817,
33-
AuthorId = 1,
34-
Price = 12.95M,
35-
Genre = "Gothic parody"
36-
},
37-
new Book()
38-
{
39-
Id = 3,
40-
Title = "David Copperfield",
41-
Year = 1850,
42-
AuthorId = 2,
43-
Price = 15,
44-
Genre = "Bildungsroman"
45-
},
46-
new Book()
47-
{
48-
Id = 4,
49-
Title = "Don Quixote",
50-
Year = 1617,
51-
AuthorId = 3,
52-
Price = 8.95M,
53-
Genre = "Picaresque"
54-
}
55-
);
56-
11+
context.Authors.AddOrUpdate(x => x.Id, new Author()
12+
{Id = 1, Name = "Jane Austen"}, new Author()
13+
{Id = 2, Name = "Charles Dickens"}, new Author()
14+
{Id = 3, Name = "Miguel de Cervantes"});
15+
context.Books.AddOrUpdate(x => x.Id, new Book()
16+
{Id = 1, Title = "Pride and Prejudice", Year = 1813, AuthorId = 1, Price = 9.99M, Genre = "Comedy of manners"}, new Book()
17+
{Id = 2, Title = "Northanger Abbey", Year = 1817, AuthorId = 1, Price = 12.95M, Genre = "Gothic parody"}, new Book()
18+
{Id = 3, Title = "David Copperfield", Year = 1850, AuthorId = 2, Price = 15, Genre = "Bildungsroman"}, new Book()
19+
{Id = 4, Title = "Don Quixote", Year = 1617, AuthorId = 3, Price = 8.95M, Genre = "Picaresque"});
5720
context.SaveChanges();
5821
base.Seed(context);
5922
}
6023
}
61-
}
24+
}

webapi/Global.asax

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapi/Global.asax.cs

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

webapi/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace webapi
12+
{
13+
14+
public class Program
15+
{
16+
public static void Main(string[] args)
17+
{
18+
CreateHostBuilder(args).Build().Run();
19+
}
20+
21+
public static IHostBuilder CreateHostBuilder(string[] args) =>
22+
Host.CreateDefaultBuilder(args)
23+
.ConfigureWebHostDefaults(webBuilder =>
24+
{
25+
webBuilder.UseStartup<Startup>();
26+
});
27+
}
28+
29+
30+
}

0 commit comments

Comments
 (0)