public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,

            ILogger log)
        {
            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                log.LogInformation("RequestBody" + requestBody);

                var input = JsonConvert.DeserializeObject <Item>(requestBody);
                var ss    = Environment.GetEnvironmentVariable("CosmosDBURI");



                CosmosClient cosmosClient = new CosmosClient(Environment.GetEnvironmentVariable("CosmosDBURI"), Environment.GetEnvironmentVariable("CosmosDBAccountKey"),
                                                             new CosmosClientOptions()
                {
                    ApplicationRegion = Regions.WestEurope,
                });

                CosmosDbService _cosmosDbService = new CosmosDbService(cosmosClient, "pracdb", "praccon");

                await _cosmosDbService.AddItemAsync(input);


                return(new OkObjectResult("Document created successfully"));
            }
            catch (Exception ex)
            {
                log.LogError($"Couldn't insert item. Exception thrown: {ex.Message}");
                return(new UnprocessableEntityObjectResult(ex.Message));
            }
        }
Exemplo n.º 2
0
        private static async Task <CosmosDbService <ClientData> > InitializeCosmosClientInstanceClientDataAsync(IConfigurationSection configurationSection)
        {
            string databaseName  = configurationSection.GetSection("DatabaseName").Value;
            string containerName = configurationSection.GetSection("ClientsContainerName").Value;
            string account       = configurationSection.GetSection("Account").Value;
            string key           = configurationSection.GetSection("Key").Value;

            Microsoft.Azure.Cosmos.CosmosClient client          = new Microsoft.Azure.Cosmos.CosmosClient(account, key);
            CosmosDbService <ClientData>        cosmosDbService = new CosmosDbService <ClientData>(client, databaseName, containerName);

            Microsoft.Azure.Cosmos.DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName);

            await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");

            //Upsert client seeding data
            if (await cosmosDbService.GetItemAsync(CreateBcnOfficeClient().Id) == null)
            {
                await cosmosDbService.AddItemAsync(CreateBcnOfficeClient());
            }
            else
            {
                // await cosmosDbService.UpdateItemAsync(CreateBcnOfficeClient().Id, CreateBcnOfficeClient());
            }

            return(cosmosDbService);
        }