private async System.Threading.Tasks.Task <bool> saveTaskView(dynamic taskView, ILogger log) { //If they still have tasks to approve, go ahead and upsert the document. Otherwise, delete it for good housekeeping. if ((taskView.mytasks.Count > 0) || (taskView.approvaltasks.Count > 0)) { return(await _cosmosHelper.UpsertItemAsync("Tasks", "TaskViews", taskView, true, log)); } else { return(await _cosmosHelper.DeleteItemAsync("Tasks", "TaskViews", taskView, true, log)); } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "put", Route = null)] HttpRequest req, ILogger log) { //Get the json from the payload and turn it into a dynamic object so we don't need a model class. string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic task = JsonConvert.DeserializeObject <ExpandoObject>(requestBody); //If this is a new task, it will not have an id. If we cast the ExpandoObject as a Dictionary, we can do a lookup to see if the attribute exists. //Create the guid here so we can easily return it in the response. if (((IDictionary <String, object>)task).ContainsKey("id") == false) { task.id = Guid.NewGuid().ToString(); //In theory, we'd want the function to set the created date for new records. However, the client data generator may set it for us //to simulate date ranges. if (((IDictionary <String, object>)task).ContainsKey("createddate") == false) { task.createddate = DateTime.UtcNow.ToString("o"); } } //If the task is complete, use ttl to let it get deleted. This way the change feed will pickup the document update and alter the managed views. if (task.status.Equals("complete")) { //Set the ttl to 5 minutes. (60 seconds * 5 minutes) task.ttl = 60 * 5; //In theory, we'd want the function to set the completed date for new records. However, the client data generator may set it for us //to simulate date ranges. if (((IDictionary <String, object>)task).ContainsKey("completeddate") == false) { task.completeddate = DateTime.UtcNow.ToString("o"); } } //CosmosNote - Upsert the document in cosmos and return the task id. This is different from V2 where we use a Document output binding to handle it for us. await _cosmosHelper.UpsertItemAsync("Tasks", "TaskItem", task, false, log); return(new OkObjectResult(task.id)); }