public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "todo2/{id}")]
            HttpRequest request,
            string id,
            [Table("todos", "todo", Connection = "AzureWebJobsStorage")] CloudTable table,
            [Table("todos", "todo", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity tableEntity,
            ILogger logger)
        {
            logger.LogInformation("Deleting todo item");

            if (tableEntity == null)
            {
                return(new NotFoundResult());
            }

            var deleteOperation       = TableOperation.Delete(tableEntity);
            var deleteOperationResult = await table.ExecuteAsync(deleteOperation);

            if (deleteOperationResult.HttpStatusCode == 204)
            {
                return(new OkResult());
            }

            logger.LogError("Cannot delete todo item");

            return(new InternalServerErrorResult());
        }
Ejemplo n.º 2
0
 public static Todo ToTodo(this TodoTableEntity todo)
 {
     return(new Todo()
     {
         Id = todo.RowKey,
         CreatedTime = todo.CreatedTime,
         IsCompleted = todo.IsCompleted,
         TaskDescription = todo.TaskDescription
     });
 }
Ejemplo n.º 3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "todo2/{id}")]
            HttpRequest request,
            string id,
            [Table("todos", "todo", Connection = "AzureWebJobsStorage")] CloudTable table,
            [Table("todos", "todo", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity tableEntity,
            ILogger logger)
        {
            logger.LogInformation("Updating todo");

            if (tableEntity == null)
            {
                return(new NotFoundResult());
            }
            //
            // NOTE:
            // Instead of using binding to get the entity, we could have get the item through code as shown below.
            // But why do we want to do it by ourselves if the bindings does it for us?! :D
            //
            var retrieveOperation       = TableOperation.Retrieve <TodoTableEntity>("todo", id);
            var retrieveOperationResult = await table.ExecuteAsync(retrieveOperation);

            var todoToUpdate = retrieveOperationResult.Result as TodoTableEntity;

            var todoContent    = await new StreamReader(request.Body).ReadToEndAsync();
            var updatedTodoDto = JsonConvert.DeserializeObject <TodoUpdateDto>(todoContent);

            if (updatedTodoDto == null)
            {
                return(new BadRequestResult());
            }

            if (!string.IsNullOrEmpty(updatedTodoDto?.Description))
            {
                tableEntity.Description = updatedTodoDto.Description;
            }

            tableEntity.IsCompleted = updatedTodoDto.IsCompleted;

            var replaceOperation = TableOperation.Replace(tableEntity);
            var operation        = await table.ExecuteAsync(replaceOperation);

            if (operation.HttpStatusCode == 204)
            {
                var todo = (operation.Result as TodoTableEntity).ToTodo();

                return(new OkObjectResult(todo));
            }

            logger.LogError("Cannot update todo item");

            return(new InternalServerErrorResult());
        }
Ejemplo n.º 4
0
 public static IActionResult GetTodoById(
     [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todos-table/{id}")] HttpRequest req,
     [Table("todos", "TODO", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity todo,
     ILogger log, string id)
 {
     log.LogInformation("Getting todo item by id");
     if (todo == null)
     {
         log.LogInformation($"Item {id} not found");
         return(new NotFoundResult());
     }
     return(new OkObjectResult(todo.ToTodo()));
 }
 public static IActionResult GetItemById(
     [HttpTrigger(AUTH_LEVEL, METHOD_GET, Route = ROUTE + "/{id}")] HttpRequest req,
     [Table(AZURE_TABLE, AZURE_TABLE_PARTITION_KEY, "{id}", Connection = AZURE_TABLE_CONNECTION_STRING)] TodoTableEntity todo,
     TraceWriter log,
     string id)
 {
     log.Info("Getting todo item by id");
     if (todo == null) // << The todo is loaded automatically
     {
         log.Info($"Item {id} not found");
         return(new NotFoundResult());
     }
     return(new OkObjectResult(todo.ToTodo()));
 }
Ejemplo n.º 6
0
        public static IActionResult GetTodoByIdAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "todo/{id}")] HttpRequest request,
            [Table("todos", "TODO", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity entity,
            ILogger logger,
            Guid id)
        {
            logger.LogInformation("Getting todo by id {@id}...", id);

            if (entity == null)
            {
                logger.LogInformation("Item {@Id} not found", id);
                return(new NotFoundResult());
            }

            return(new OkObjectResult(entity.ToTodo()));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todo2/{id}")]
            HttpRequest request,
            [Table("todos", "todo", "{id}", Connection = "AzureWebJobsStorage")] TodoTableEntity todoTableEntity,
            ILogger logger)
        {
            logger.LogInformation($"Getting todo item by id");

            if (todoTableEntity == null)
            {
                return(new NotFoundResult());
            }

            var todo = todoTableEntity.ToTodo();

            return(new OkObjectResult(todo));
        }