-
Notifications
You must be signed in to change notification settings - Fork 19
/
MoviesHandler.cs
59 lines (49 loc) · 2.08 KB
/
MoviesHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Net;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using DynamoDbServerless.Services;
using Models;
namespace DynamoDbServerless.Handlers
{
public class MoviesHandler
{
private const string TableName = "Movies";
private readonly IDynamoDbReader _dynamoDbReader;
private readonly IJsonConverter _jsonConverter;
private readonly ILogger _logger;
public MoviesHandler() : this(null, null, null)
{
}
public MoviesHandler(IDynamoDbReader dynamoDbReader, IJsonConverter jsonConverter, ILogger logger)
{
_dynamoDbReader = dynamoDbReader ?? new DynamoDbReader();
_jsonConverter = jsonConverter ?? new JsonConverter();
_logger = logger ?? new Logger();
}
public async Task<APIGatewayProxyResponse> GetMovie(APIGatewayProxyRequest request, ILambdaContext context)
{
context.Logger.LogLine($"Query request: {_jsonConverter.SerializeObject(request)}");
var title = WebUtility.UrlDecode(request.PathParameters["title"]);
_logger.LogInformation("GetMovie invoked with {Title}", title);
var document = await _dynamoDbReader.GetDocumentAsync(TableName, title);
context.Logger.LogLine($"Query response: {_jsonConverter.SerializeObject(document)}");
if (document == null)
{
_logger.LogInformation("GetMovie produced no results for {Title}", title);
return new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.NotFound };
}
var movie = new Movie
{
Title = document["Title"],
Genre = (MovieGenre)int.Parse(document["Genre"])
};
_logger.LogInformation("GetMovie result is {Title}, {@Content}", movie.Title, movie);
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = _jsonConverter.SerializeObject(movie)
};
}
}
}