示例#1
0
        public async Task UpsertAsync(CovidDataQuery row)
        {
            try
            {
                // Create an item in the container representing the Andersen family. Note we provide the value of the partition key for this item, which is "Andersen".
                ItemResponse <CovidDataQuery> insertResponse = await _container.UpsertItemAsync <CovidDataQuery>(row);

                // Note that after creating the item, we can access the body of the item with the Resource property of the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
                Console.WriteLine("Created item in database with text: {0} Operation consumed {1} RUs.\n", insertResponse.Resource.QueryText, insertResponse.RequestCharge);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.Conflict)
            {
                Console.WriteLine("Item in database with id: {0} already exists\n", row.QueryText);
            }
        }
示例#2
0
        public async Task <IActionResult> SaveQuery(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);
            var     text        = (string)(data?.text?.ToString());

            if (string.IsNullOrWhiteSpace(text))
            {
                return(new BadRequestObjectResult("expected request body to contain 'text' as a json field"));
            }

            CovidDataQuery query = await _queriesRepo.Fetch(text);

            if (query == null)
            {
                query = new CovidDataQuery(text);
            }
            else
            {
                query.NumTimesHit++;
                query.LastUpdated = DateTime.UtcNow;
            }

            try
            {
                await _queriesRepo.UpsertAsync(query);
            }
            catch (Exception)
            {
                // TODO: a typical app should log this.
                // This will automatically translate to an http 500 error for the caller.  That's enough for this.
                // This try/catch block is here for demonstration -- it's other useless code.
                throw;
            }

            return(new OkResult());
        }