Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
112 lines (99 sloc) 4.56 KB
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using Microsoft.WindowsAzure.Storage;
namespace AzureFunctionsTodo
{
public static class TodoApiTableStorage
{
private const string route = "todo2";
[FunctionName("Table_CreateTodo")]
public static async Task<IActionResult>CreateTodo(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = route)]HttpRequest req,
[Table("todos", Connection="AzureWebJobsStorage")] IAsyncCollector<TodoTableEntity> todoTable,
TraceWriter log)
{
log.Info("Creating a new todo list item");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<TodoCreateModel>(requestBody);
var todo = new Todo() { TaskDescription = input.TaskDescription };
await todoTable.AddAsync(todo.ToTableEntity());
return new OkObjectResult(todo);
}
[FunctionName("Table_GetTodos")]
public static async Task<IActionResult> GetTodos(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = route)]HttpRequest req,
// unfortunately IQueryable<TodoTableEntity> binding not supported in functions v2
[Table("todos", Connection = "AzureWebJobsStorage")] CloudTable todoTable,
TraceWriter log)
{
log.Info("Getting todo list items");
var query = new TableQuery<TodoTableEntity>();
var segment = await todoTable.ExecuteQuerySegmentedAsync(query, null);
return new OkObjectResult(segment.Select(Mappings.ToTodo));
}
[FunctionName("Table_GetTodoById")]
public static IActionResult GetTodoById(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = route + "/{id}")]HttpRequest req,
[Table("todos", "TODO", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity todo,
TraceWriter log, string id)
{
log.Info("Getting todo item by id");
if (todo == null)
{
log.Info($"Item {id} not found");
return new NotFoundResult();
}
return new OkObjectResult(todo.ToTodo());
}
[FunctionName("Table_UpdateTodo")]
public static async Task<IActionResult> UpdateTodo(
[HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = route + "/{id}")]HttpRequest req,
[Table("todos", Connection = "AzureWebJobsStorage")] CloudTable todoTable,
TraceWriter log, string id)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var updated = JsonConvert.DeserializeObject<TodoUpdateModel>(requestBody);
var findOperation = TableOperation.Retrieve<TodoTableEntity>("TODO", id);
var findResult = await todoTable.ExecuteAsync(findOperation);
if (findResult.Result == null)
{
return new NotFoundResult();
}
var existingRow = (TodoTableEntity)findResult.Result;
existingRow.IsCompleted = updated.IsCompleted;
if (!string.IsNullOrEmpty(updated.TaskDescription))
{
existingRow.TaskDescription = updated.TaskDescription;
}
var replaceOperation = TableOperation.Replace(existingRow);
await todoTable.ExecuteAsync(replaceOperation);
return new OkObjectResult(existingRow.ToTodo());
}
[FunctionName("Table_DeleteTodo")]
public static async Task<IActionResult> DeleteTodo(
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = route + "/{id}")]HttpRequest req,
[Table("todos", Connection = "AzureWebJobsStorage")] CloudTable todoTable,
TraceWriter log, string id)
{
var deleteOperation = TableOperation.Delete(new TableEntity() { PartitionKey = "TODO", RowKey = id, ETag = "*" });
try
{
var deleteResult = await todoTable.ExecuteAsync(deleteOperation);
}
catch (StorageException e) when (e.RequestInformation.HttpStatusCode == 404)
{
return new NotFoundResult();
}
return new OkResult();
}
}
}