Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (101 sloc) 3.58 KB
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DynamoDbLocalSample.Controllers
{
[Route("api/[controller]")]
[ApiController]
public partial class ValuesController : ControllerBase
{
private const string TableName = "SampleData";
private readonly IAmazonDynamoDB _amazonDynamoDb;
public ValuesController(IAmazonDynamoDB amazonDynamoDb)
{
_amazonDynamoDb = amazonDynamoDb;
}
// GET api/values/init
[HttpGet]
[Route("init")]
public async Task Initialise()
{
var request = new ListTablesRequest
{
Limit = 10
};
var response = await _amazonDynamoDb.ListTablesAsync(request);
var results = response.TableNames;
if (!results.Contains(TableName))
{
var createRequest = new CreateTableRequest
{
TableName = TableName,
AttributeDefinitions = new List<AttributeDefinition>
{
new AttributeDefinition
{
AttributeName = "Id",
AttributeType = "N"
}
},
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "Id",
KeyType = "HASH" //Partition key
}
},
ProvisionedThroughput = new ProvisionedThroughput
{
ReadCapacityUnits = 2,
WriteCapacityUnits = 2
}
};
await _amazonDynamoDb.CreateTableAsync(createRequest);
}
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<ActionResult<string>> Get(int id)
{
var request = new GetItemRequest
{
TableName = TableName,
Key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = id.ToString() } } }
};
var response = await _amazonDynamoDb.GetItemAsync(request);
if (!response.IsItemSet)
return NotFound();
return response.Item["Title"].S;
}
// POST api/values
[HttpPost]
public async Task Post([FromBody] PostInput input)
{
var request = new PutItemRequest
{
TableName = TableName,
Item = new Dictionary<string, AttributeValue>
{
{ "Id", new AttributeValue { N = input.Id.ToString() }},
{ "Title", new AttributeValue { S = input.Title }}
}
};
await _amazonDynamoDb.PutItemAsync(request);
}
// DELETE api/values/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var request = new DeleteItemRequest
{
TableName = TableName,
Key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = id.ToString() } } }
};
var response = await _amazonDynamoDb.DeleteItemAsync(request);
return StatusCode((int)response.HttpStatusCode);
}
}
}