public static async Task <Todo> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "todos")] HttpRequest request,
            ILogger logger)
        {
            await DatabaseConfiguration.EnsuresDatabaseCreated();

            var connectionString = Environment.GetEnvironmentVariable("ConnectionString");

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                string requestBody = await new StreamReader(request.Body).ReadToEndAsync();
                var    todo        = JsonConvert.DeserializeObject <Todo>(requestBody);

                string query = @"
UPDATE [dbo].[Todos]
SET [Content] = @Content
WHERE [Id] = @Id

SELECT *
FROM [dbo].[Todos]
WHERE [Id] = @Id
";

                return((await connection.QueryAsync <Todo>(query, todo)).Single());
            }
        }
Пример #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "todos/{id}")] HttpRequest request,
            string id,
            ILogger logger)
        {
            await DatabaseConfiguration.EnsuresDatabaseCreated();

            if (!int.TryParse(id, out int valueId))
            {
                return(new BadRequestObjectResult("Please pass a valid id on the query string"));
            }

            var connectionString = Environment.GetEnvironmentVariable("ConnectionString");

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                string query = @"
UPDATE [dbo].[Todos]
SET [IsDeleted] = 1
WHERE [Id] = @Id
";

                await connection.ExecuteAsync(query, new { Id = valueId });
            }

            return((ActionResult) new OkObjectResult($"Todo #{id} removed"));
        }
Пример #3
0
        public static async Task <IEnumerable <Todo> > Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "todos")] HttpRequest request,
            ILogger logger)
        {
            await DatabaseConfiguration.EnsuresDatabaseCreated();

            var connectionString = Environment.GetEnvironmentVariable("ConnectionString");

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                string query = @"
SELECT *
FROM [dbo].[Todos]
WHERE [IsDeleted] = 0
";

                return(await connection.QueryAsync <Todo>(query));
            }
        }