Skip to content

Commit 0055763

Browse files
Migration
1 parent e5f3bff commit 0055763

30 files changed

+366
-131
lines changed

.vs/WebApplicationBeanstalk/v16/.suo

13 KB
Binary file not shown.

Controllers/HomeController.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Security.Cryptography.X509Certificates;
66
using System.Threading.Tasks;
7+
using Amazon.DynamoDBv2;
78
using Microsoft.AspNetCore.Identity.UI.Pages.Account.Internal;
89
using Microsoft.AspNetCore.Mvc;
910
using WebApplicationBeanstalk.Models;
@@ -13,7 +14,13 @@ namespace WebApplicationBeanstalk.Controllers
1314
{
1415
public class HomeController : Controller
1516
{
16-
AWSServices services = new AWSServices();
17+
private IAmazonDynamoDB dynamoDBClient;
18+
19+
public HomeController(IAmazonDynamoDB dynamoDBClient)
20+
{
21+
this.dynamoDBClient = dynamoDBClient;
22+
}
23+
1724
public IActionResult Index()
1825
{
1926
return View();
@@ -27,6 +34,7 @@ public IActionResult RegistrationForm()
2734
[HttpPost]
2835
public IActionResult LogIn (string email, string password)
2936
{
37+
AWSServices services = new AWSServices(dynamoDBClient);
3038
User user = services.LogIn(email, password).Result;
3139

3240
if (user == null)
@@ -40,9 +48,10 @@ public IActionResult LogIn (string email, string password)
4048
[HttpGet]
4149
public IActionResult Movies(string email)
4250
{
51+
AWSServices services = new AWSServices(dynamoDBClient);
4352
return View("Movies", new UserXMovies()
4453
{
45-
User = services.GetUser(email),
54+
User = services.GetUser(email).Result,
4655
Movies = services.GetMovies().Result
4756
});
4857
}
@@ -51,16 +60,18 @@ public IActionResult Movies(string email)
5160
[HttpGet]
5261
public IActionResult MoviesDetails(string email, string movieId)
5362
{
63+
AWSServices services = new AWSServices(dynamoDBClient);
5464
return View("MoviesDetails", new UserXMovie()
5565
{
56-
User = services.GetUser(email),
66+
User = services.GetUser(email).Result,
5767
Movie = services.GetMovie(movieId,false).Result
5868
});
5969
}
6070

6171
[HttpGet]
6272
public ActionResult DownloadMovie(string Id)
6373
{
74+
AWSServices services = new AWSServices(dynamoDBClient);
6475
string Tmp = AppDomain.CurrentDomain.BaseDirectory;
6576
Movie movie = services.GetMovie(Id, true).Result;
6677
return PhysicalFile(Tmp + movie.Id + movie.Video.GetType(), "video/avi", movie.Title);
@@ -69,6 +80,7 @@ public ActionResult DownloadMovie(string Id)
6980
[HttpPost]
7081
public IActionResult AddComment(string email, string movieId, string comment,int rate)
7182
{
83+
AWSServices services = new AWSServices(dynamoDBClient);
7284
services.AddComment(email, movieId, comment, rate);
7385
return MoviesDetails(email, movieId);
7486
}
@@ -77,6 +89,7 @@ public IActionResult AddComment(string email, string movieId, string comment,int
7789
[HttpPost]
7890
public IActionResult AddUser (User user)
7991
{
92+
AWSServices services = new AWSServices(dynamoDBClient);
8093
if (ModelState.IsValid)
8194
{
8295
User newUser = services.Register(user).Result;

Service/AWSServices.cs

+39-21
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ namespace WebApplicationBeanstalk.Service
1212
{
1313
public class AWSServices
1414
{
15-
AmazonDynamoDBClient Client;
16-
DynamoDBContext Context;
1715
string Tmp = AppDomain.CurrentDomain.BaseDirectory;
18-
RegionEndpoint BucketRegion = RegionEndpoint.USWest1;
16+
RegionEndpoint Region = RegionEndpoint.CACentral1;
1917

20-
public AWSServices()
18+
IAmazonDynamoDB dynamoDBClient { get; set; }
19+
20+
public AWSServices(IAmazonDynamoDB dynamoDBClient)
2121
{
22-
Client = new AmazonDynamoDBClient(BucketRegion);
23-
Context = new DynamoDBContext(Client);
22+
this.dynamoDBClient = dynamoDBClient;
2423
CreateTable();
2524
}
2625
public async Task<User> Register(User user)
2726
{
27+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
2828
await Context.SaveAsync(user, default(System.Threading.CancellationToken));
2929
User newUser = await Context.LoadAsync<User>(user.Email, default(System.Threading.CancellationToken));
3030
return user;
3131
}
3232

3333
public async Task<User> LogIn(String email, String password)
3434
{
35+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
3536
User userDB = await Context.LoadAsync<User>(email);
3637
if (userDB != null)
3738
{
@@ -45,39 +46,52 @@ public async Task<User> LogIn(String email, String password)
4546

4647
public async Task<List<Movie>> GetMovies()
4748
{
49+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
50+
4851
var conditions = new List<ScanCondition>();
4952
List<Movie> movies= await Context.ScanAsync<Movie>(conditions).GetRemainingAsync();
5053
foreach(Movie movie in movies)
51-
movie.Cover.DownloadTo(Tmp + movie.Id + movie.Cover.GetType());
54+
{
55+
//movie.Cover.DownloadTo(Tmp + movie.Id + movie.Cover.GetType());
56+
}
57+
5258
return movies;
5359
}
5460

5561
public async Task<Movie> GetMovie(string Id, Boolean WithVideo)
5662
{
63+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
64+
5765
Movie movie = await Context.LoadAsync<Movie>(Id, default(System.Threading.CancellationToken));
58-
movie.Cover.DownloadTo(Tmp + movie.Id + movie.Cover.GetType());
66+
//movie.Cover.DownloadTo(Tmp + movie.Id + movie.Cover.GetType());
5967
if (WithVideo)
60-
movie.Video.DownloadTo(Tmp + movie.Id + movie.Video.GetType());
68+
{
69+
//movie.Video.DownloadTo(Tmp + movie.Id + movie.Video.GetType());
70+
}
6171
return movie;
6272
}
6373

6474
public async Task<Movie> UploadMovie(String bucketName, String Title, String VideoPath, String CoverPath)
6575
{
76+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
77+
6678
Movie movie = new Movie();
6779
movie.Id = System.Guid.NewGuid().ToString();
6880
movie.Title = Title;
69-
movie.Cover = S3Link.Create(Context, bucketName, movie.Id + "1", BucketRegion);
70-
movie.Video = S3Link.Create(Context, bucketName, movie.Id + "2", BucketRegion);
81+
movie.Cover = S3Link.Create(Context, bucketName, movie.Id + "1", Region);
82+
movie.Video = S3Link.Create(Context, bucketName, movie.Id + "2", Region);
7183

72-
movie.Cover.UploadFrom(CoverPath);
73-
movie.Video.UploadFrom(VideoPath);
84+
//movie.Cover.UploadFrom(CoverPath);
85+
//movie.Video.UploadFrom(VideoPath);
7486

7587
await Context.SaveAsync<Movie>(movie);
7688
return await GetMovie(movie.Id,false);
7789
}
7890

7991
public async void AddComment(string email,string Id, String comment, int rate)
8092
{
93+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
94+
8195
Movie movie = await GetMovie(Id, false);
8296
movie.Ratings.Add(new Rating()
8397
{
@@ -90,20 +104,23 @@ public async void AddComment(string email,string Id, String comment, int rate)
90104
await Context.SaveAsync<Movie>(movie);
91105
}
92106

93-
public User GetUser(string email)
107+
public async Task<User> GetUser(string email)
94108
{
95-
return Context.Load<User>(email);
109+
DynamoDBContext Context = new DynamoDBContext(dynamoDBClient);
110+
111+
return await Context.LoadAsync<User>(email);
96112
}
97113

98114

99115
public void CreateTable()
100116
{
101117
String tableName = "User";
102-
List<string> currentTables = Client.ListTables().TableNames;
118+
Task<ListTablesResponse> table = dynamoDBClient.ListTablesAsync();
119+
List<string> currentTables = table.Result.TableNames;
103120
bool tablesAdded = false;
104121
if (!currentTables.Contains(tableName))
105122
{
106-
Client.CreateTableAsync(new CreateTableRequest
123+
dynamoDBClient.CreateTableAsync(new CreateTableRequest
107124
{
108125
TableName = tableName,
109126
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 3, WriteCapacityUnits = 1 },
@@ -121,7 +138,7 @@ public void CreateTable()
121138
},
122139
});
123140

124-
Client.CreateTableAsync(new CreateTableRequest
141+
dynamoDBClient.CreateTableAsync(new CreateTableRequest
125142
{
126143
TableName = "Movie",
127144
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 3, WriteCapacityUnits = 1 },
@@ -149,7 +166,7 @@ public void CreateTable()
149166
allActive = true;
150167
Thread.Sleep(TimeSpan.FromSeconds(5));
151168

152-
TableStatus tableStatus = GetTableStatus(Client, tableName);
169+
TableStatus tableStatus = GetTableStatus(tableName);
153170
if (!object.Equals(tableStatus, TableStatus.ACTIVE))
154171
allActive = false;
155172

@@ -158,11 +175,12 @@ public void CreateTable()
158175
}
159176

160177

161-
private static TableStatus GetTableStatus(AmazonDynamoDBClient client, string tableName)
178+
private TableStatus GetTableStatus(string tableName)
162179
{
163180
try
164181
{
165-
var table = client.DescribeTable(new DescribeTableRequest { TableName = tableName }).Table;
182+
Task<DescribeTableResponse> tableResp = dynamoDBClient.DescribeTableAsync(new DescribeTableRequest { TableName = tableName });
183+
TableDescription table = tableResp.Result.Table;
166184
return (table == null) ? null : table.TableStatus;
167185
}
168186
catch (AmazonDynamoDBException db)

Startup.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Amazon.DynamoDBv2;
56
using Microsoft.AspNetCore.Builder;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Http;
@@ -30,8 +31,10 @@ public void ConfigureServices(IServiceCollection services)
3031
options.MinimumSameSitePolicy = SameSiteMode.None;
3132
});
3233

33-
34-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
34+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
35+
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
36+
// services.AddAWSService<IAmazonS3>();
37+
services.AddAWSService<IAmazonDynamoDB>();
3538
}
3639

3740
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

WebApplicationBeanstalk.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="AWSSDK" Version="2.3.55.2" />
8+
<PackageReference Include="AWSSDK.Core" Version="3.5.1.29" />
9+
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.5.1.7" />
10+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.101" />
11+
<PackageReference Include="AWSSDK.S3" Version="3.5.3.7" />
912
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.7" />
1013
<PackageReference Include="Microsoft.AspNetCore.App" />
1114
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />

appsettings.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Warning"
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning"
5+
}
6+
},
7+
"AllowedHosts": "*",
8+
"AWS": {
9+
"Profile": "default",
10+
"Region": "us-west-2"
511
}
6-
},
7-
"AllowedHosts": "*"
812
}
Binary file not shown.
Binary file not shown.

bin/Debug/netcoreapp2.1/WebApplicationBeanstalk.deps.json

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"WebApplicationBeanstalk/1.0.0": {
2626
"dependencies": {
2727
"AWSSDK.Core": "3.5.1.29",
28+
"AWSSDK.DynamoDBv2": "3.5.1.7",
2829
"AWSSDK.Extensions.NETCore.Setup": "3.3.101",
2930
"AWSSDK.S3": "3.5.3.7",
3031
"Microsoft.AspNetCore": "2.1.7",
@@ -61,6 +62,20 @@
6162
"lib/netstandard2.0/AWSSDK.Core.dll": {}
6263
}
6364
},
65+
"AWSSDK.DynamoDBv2/3.5.1.7": {
66+
"dependencies": {
67+
"AWSSDK.Core": "3.5.1.29"
68+
},
69+
"runtime": {
70+
"lib/netstandard2.0/AWSSDK.DynamoDBv2.dll": {
71+
"assemblyVersion": "3.3.0.0",
72+
"fileVersion": "3.5.1.7"
73+
}
74+
},
75+
"compile": {
76+
"lib/netstandard2.0/AWSSDK.DynamoDBv2.dll": {}
77+
}
78+
},
6479
"AWSSDK.Extensions.NETCore.Setup/3.3.101": {
6580
"dependencies": {
6681
"AWSSDK.Core": "3.5.1.29",
@@ -3732,6 +3747,13 @@
37323747
"path": "awssdk.core/3.5.1.29",
37333748
"hashPath": "awssdk.core.3.5.1.29.nupkg.sha512"
37343749
},
3750+
"AWSSDK.DynamoDBv2/3.5.1.7": {
3751+
"type": "package",
3752+
"serviceable": true,
3753+
"sha512": "sha512-KWn440NkYevyp0UVloZnhhTxfYypqwQmIlrrjBnYeChhYg9oGQIPxmP3a9KNTOyLZMXc8lbJrxcJm/DpnR3xwA==",
3754+
"path": "awssdk.dynamodbv2/3.5.1.7",
3755+
"hashPath": "awssdk.dynamodbv2.3.5.1.7.nupkg.sha512"
3756+
},
37353757
"AWSSDK.Extensions.NETCore.Setup/3.3.101": {
37363758
"type": "package",
37373759
"serviceable": true,
Binary file not shown.
Binary file not shown.
+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Warning"
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Warning"
5+
}
6+
},
7+
"AllowedHosts": "*",
8+
"AWS": {
9+
"Profile": "default",
10+
"Region": "us-west-2"
511
}
6-
},
7-
"AllowedHosts": "*"
812
}

0 commit comments

Comments
 (0)