public async Task <ActionResult> DeleteTodoItem(string id)
        {
            _logger.LogInformation($"Deleting Todo item, id: {id}");

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var deleteOp = TableOperation.Delete(new TableEntity
            {
                PartitionKey = TodoItemMapper.TablePartitionKey,
                RowKey       = id,
                ETag         = "*"
            });

            try
            {
                var deleteResult = await table.ExecuteAsync(deleteOp);
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == StatusCodes.Status404NotFound)
                {
                    return(new NotFoundResult());
                }
            }

            return(new NoContentResult());
        }
        public async Task <ActionResult <TodoItem> > UpdateTodoItem(string id, TodoItem todoItem)
        {
            _logger.LogInformation($"Updating Todo item, id: {id}");

            if (todoItem == null || id != todoItem.Id || string.IsNullOrWhiteSpace(todoItem.Name))
            {
                return(new BadRequestResult());
            }

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var findOp     = TableOperation.Retrieve <TodoItemEntity>(TodoItemMapper.TablePartitionKey, id);
            var findResult = await table.ExecuteAsync(findOp);

            if (findResult.Result == null)
            {
                return(new NotFoundResult());
            }

            var todoEntity = (TodoItemEntity)findResult.Result;

            todoEntity.Name        = todoItem.Name;
            todoEntity.IsCompleted = todoItem.IsCompleted;

            var replaceOp = TableOperation.Replace(todoEntity);
            await table.ExecuteAsync(replaceOp);

            return(new OkObjectResult(todoEntity.ToModel()));
        }
        public async Task <IActionResult> GetHealthCheck()
        {
            _logger.LogInformation("Checking Health of Application");

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var tableExists = await table.ExistsAsync();

            return(tableExists ? new OkResult() : new StatusCodeResult((int)HttpStatusCode.ServiceUnavailable));
        }
        public async Task <ActionResult <IEnumerable <TodoItem> > > GetTodoItems()
        {
            _logger.LogInformation("Getting array of all Todo items");

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var query   = new TableQuery <TodoItemEntity>();
            var segment = await table.ExecuteQuerySegmentedAsync(query, null);

            var items = segment.Select(TodoItemMapper.ToModel);

            return(new OkObjectResult(items));
        }
        public async Task <ActionResult <TodoItem> > GetTodoItem(string id)
        {
            _logger.LogInformation($"Getting Todo item, id: {id}");

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var findOp     = TableOperation.Retrieve <TodoItemEntity>(TodoItemMapper.TablePartitionKey, id);
            var findResult = await table.ExecuteAsync(findOp);

            if (findResult.Result == null)
            {
                return(new NotFoundResult());
            }

            var todoEntity = (TodoItemEntity)findResult.Result;

            var todoItem = TodoItemMapper.ToModel(todoEntity);

            return(new OkObjectResult(todoItem));
        }
        public async Task <ActionResult <TodoItem> > CreateTodoItem(TodoItem todoItem)
        {
            _logger.LogInformation($"Creating new Todo item");

            if (todoItem == null || string.IsNullOrWhiteSpace(todoItem.Name))
            {
                return(new BadRequestResult());
            }

            var table = TableStorageHelper.GetTableReference(_storageConnectionString, _tableName);

            var newTodoItem = new TodoItem {
                Name = todoItem.Name, IsCompleted = false
            };
            // Create the InsertOrReplace table operation
            TableOperation insertOp = TableOperation.Insert(TodoItemMapper.ToEntity(newTodoItem));

            // Execute the operation.
            TableResult result = await table.ExecuteAsync(insertOp);

            return(CreatedAtAction(nameof(GetTodoItem), new { id = newTodoItem.Id }, newTodoItem));
        }