@@ -12,26 +12,27 @@ namespace WebApplicationBeanstalk.Service
12
12
{
13
13
public class AWSServices
14
14
{
15
- AmazonDynamoDBClient Client ;
16
- DynamoDBContext Context ;
17
15
string Tmp = AppDomain . CurrentDomain . BaseDirectory ;
18
- RegionEndpoint BucketRegion = RegionEndpoint . USWest1 ;
16
+ RegionEndpoint Region = RegionEndpoint . CACentral1 ;
19
17
20
- public AWSServices ( )
18
+ IAmazonDynamoDB dynamoDBClient { get ; set ; }
19
+
20
+ public AWSServices ( IAmazonDynamoDB dynamoDBClient )
21
21
{
22
- Client = new AmazonDynamoDBClient ( BucketRegion ) ;
23
- Context = new DynamoDBContext ( Client ) ;
22
+ this . dynamoDBClient = dynamoDBClient ;
24
23
CreateTable ( ) ;
25
24
}
26
25
public async Task < User > Register ( User user )
27
26
{
27
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
28
28
await Context . SaveAsync ( user , default ( System . Threading . CancellationToken ) ) ;
29
29
User newUser = await Context . LoadAsync < User > ( user . Email , default ( System . Threading . CancellationToken ) ) ;
30
30
return user ;
31
31
}
32
32
33
33
public async Task < User > LogIn ( String email , String password )
34
34
{
35
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
35
36
User userDB = await Context . LoadAsync < User > ( email ) ;
36
37
if ( userDB != null )
37
38
{
@@ -45,39 +46,52 @@ public async Task<User> LogIn(String email, String password)
45
46
46
47
public async Task < List < Movie > > GetMovies ( )
47
48
{
49
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
50
+
48
51
var conditions = new List < ScanCondition > ( ) ;
49
52
List < Movie > movies = await Context . ScanAsync < Movie > ( conditions ) . GetRemainingAsync ( ) ;
50
53
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
+
52
58
return movies ;
53
59
}
54
60
55
61
public async Task < Movie > GetMovie ( string Id , Boolean WithVideo )
56
62
{
63
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
64
+
57
65
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());
59
67
if ( WithVideo )
60
- movie . Video . DownloadTo ( Tmp + movie . Id + movie . Video . GetType ( ) ) ;
68
+ {
69
+ //movie.Video.DownloadTo(Tmp + movie.Id + movie.Video.GetType());
70
+ }
61
71
return movie ;
62
72
}
63
73
64
74
public async Task < Movie > UploadMovie ( String bucketName , String Title , String VideoPath , String CoverPath )
65
75
{
76
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
77
+
66
78
Movie movie = new Movie ( ) ;
67
79
movie . Id = System . Guid . NewGuid ( ) . ToString ( ) ;
68
80
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 ) ;
71
83
72
- movie . Cover . UploadFrom ( CoverPath ) ;
73
- movie . Video . UploadFrom ( VideoPath ) ;
84
+ // movie.Cover.UploadFrom(CoverPath);
85
+ // movie.Video.UploadFrom(VideoPath);
74
86
75
87
await Context . SaveAsync < Movie > ( movie ) ;
76
88
return await GetMovie ( movie . Id , false ) ;
77
89
}
78
90
79
91
public async void AddComment ( string email , string Id , String comment , int rate )
80
92
{
93
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
94
+
81
95
Movie movie = await GetMovie ( Id , false ) ;
82
96
movie . Ratings . Add ( new Rating ( )
83
97
{
@@ -90,20 +104,23 @@ public async void AddComment(string email,string Id, String comment, int rate)
90
104
await Context . SaveAsync < Movie > ( movie ) ;
91
105
}
92
106
93
- public User GetUser ( string email )
107
+ public async Task < User > GetUser ( string email )
94
108
{
95
- return Context . Load < User > ( email ) ;
109
+ DynamoDBContext Context = new DynamoDBContext ( dynamoDBClient ) ;
110
+
111
+ return await Context . LoadAsync < User > ( email ) ;
96
112
}
97
113
98
114
99
115
public void CreateTable ( )
100
116
{
101
117
String tableName = "User" ;
102
- List < string > currentTables = Client . ListTables ( ) . TableNames ;
118
+ Task < ListTablesResponse > table = dynamoDBClient . ListTablesAsync ( ) ;
119
+ List < string > currentTables = table . Result . TableNames ;
103
120
bool tablesAdded = false ;
104
121
if ( ! currentTables . Contains ( tableName ) )
105
122
{
106
- Client . CreateTableAsync ( new CreateTableRequest
123
+ dynamoDBClient . CreateTableAsync ( new CreateTableRequest
107
124
{
108
125
TableName = tableName ,
109
126
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 3 , WriteCapacityUnits = 1 } ,
@@ -121,7 +138,7 @@ public void CreateTable()
121
138
} ,
122
139
} ) ;
123
140
124
- Client . CreateTableAsync ( new CreateTableRequest
141
+ dynamoDBClient . CreateTableAsync ( new CreateTableRequest
125
142
{
126
143
TableName = "Movie" ,
127
144
ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 3 , WriteCapacityUnits = 1 } ,
@@ -149,7 +166,7 @@ public void CreateTable()
149
166
allActive = true ;
150
167
Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
151
168
152
- TableStatus tableStatus = GetTableStatus ( Client , tableName ) ;
169
+ TableStatus tableStatus = GetTableStatus ( tableName ) ;
153
170
if ( ! object . Equals ( tableStatus , TableStatus . ACTIVE ) )
154
171
allActive = false ;
155
172
@@ -158,11 +175,12 @@ public void CreateTable()
158
175
}
159
176
160
177
161
- private static TableStatus GetTableStatus ( AmazonDynamoDBClient client , string tableName )
178
+ private TableStatus GetTableStatus ( string tableName )
162
179
{
163
180
try
164
181
{
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 ;
166
184
return ( table == null ) ? null : table . TableStatus ;
167
185
}
168
186
catch ( AmazonDynamoDBException db )
0 commit comments