Exemple #1
0
    public static async Task <IActionResult> UpdateTodo(
        [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = Route + "/{id}")] HttpRequest req,
        [Blob(BlobPath + "/{id}.json", Connection = "AzureWebJobsStorage")] BlobClient blob,
        ILogger log, string id)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        var    updated     = JsonConvert.DeserializeObject <TodoUpdateModel>(requestBody);

        if (!await blob.ExistsAsync())
        {
            return(new NotFoundResult());
        }
        var existingText = await blob.DownloadTextAsync();

        var existingTodo = JsonConvert.DeserializeObject <Todo>(existingText);

        existingTodo.IsCompleted = updated.IsCompleted;
        if (!string.IsNullOrEmpty(updated.TaskDescription))
        {
            existingTodo.TaskDescription = updated.TaskDescription;
        }

        await blob.UploadTextAsync(JsonConvert.SerializeObject(existingTodo));

        return(new OkObjectResult(existingTodo));
    }