예제 #1
0
 /// <summary>
 /// Adds Customer to the database
 /// </summary>
 /// <param name="customer">data to be added</param>
 /// <returns></returns>
 private async Task AddItemsToContainerAsync(CustomerCosmos customer)
 {
     try
     {
         ItemResponse <CustomerCosmos> response = await _container.CreateItemAsync <CustomerCosmos>(customer, new PartitionKey(customer.LastName));
     }
     catch (CosmosException ex)
     {
         throw ex;
     }
 }
예제 #2
0
 /// <summary>
 /// Adds new customer
 /// </summary>
 /// <param name="customer">customer details</param>
 /// <returns></returns>
 public async Task AddCustomerItem(CustomerCosmos customer)
 {
     try
     {
         await CreateDatabaseAsync();
         await CreateContainerAsync("/LastName");
         await AddItemsToContainerAsync(customer);
     }catch (Exception ex)
     {
         throw ex;
     }
 }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "%databaseId%",
                 collectionName: "%containerId%",
                 ConnectionStringSetting = "AzureconnectionString", CreateIfNotExists = true, PartitionKey = "%CustomerPartitionKey%", Id = "id")] IAsyncCollector <object> customers,
            ILogger log)
        {
            log.LogInformation("Customer insertion begins!");
            try
            {
                string         customerBody = await new StreamReader(req.Body).ReadToEndAsync();
                CustomerCosmos customer     = JsonConvert.DeserializeObject <CustomerCosmos>(customerBody);
                await customers.AddAsync(customer);

                log.LogInformation("Customer insertion succesful!");
                return(new OkObjectResult(customers));
            }
            catch (Exception ex)
            {
                log.LogError("Error:" + ex.Message);
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }