Beispiel #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Customer Add begins");

            try
            {
                string customerBody          = await new StreamReader(req.Body).ReadToEndAsync();
                var    customer              = JsonConvert.DeserializeObject <Dictionary <string, string> >(customerBody);
                var    cosmosDbDatabaseName  = Environment.GetEnvironmentVariable("databaseId", EnvironmentVariableTarget.Process);
                var    cosmosDbContainerName = Environment.GetEnvironmentVariable("containerId", EnvironmentVariableTarget.Process);
                var    cosmosDbPartitionKey  = Environment.GetEnvironmentVariable("CustomerPartitionKey", EnvironmentVariableTarget.Process);
                //var customerContainer = CosmosUtilities.GetContainer(_cosmosClient, cosmosDbDatabaseName, cosmosDbContainerName, cosmosDbPartitionKey);
                //await customerContainer.CreateItemAsync(customer, customer.PartitionKey);
                if (CosmosUtilities.ValidateObject(customer, cosmosDbPartitionKey))
                {
                    var result = await CosmosUtilities.CreateItem(_cosmosClient, cosmosDbDatabaseName, cosmosDbContainerName, cosmosDbPartitionKey, customer);

                    return(new StatusCodeResult((int)result.StatusCode));
                }
                return(new StatusCodeResult(StatusCodes.Status400BadRequest));
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            HttpResponseMessage response;

            try
            {
                IDictionary <string, string> queryParams = req.GetQueryParameterDictionary();
                CustomerDetail customer = null;
                if (queryParams.ContainsKey("lastName") && queryParams.ContainsKey("id"))
                {
                    string lastName              = queryParams["lastName"].Replace("\"", "");
                    string id                    = queryParams["id"].Replace("\"", "");
                    var    cosmosDbDatabaseName  = Environment.GetEnvironmentVariable("databaseId", EnvironmentVariableTarget.Process);
                    var    cosmosDbContainerName = Environment.GetEnvironmentVariable("containerId", EnvironmentVariableTarget.Process);
                    var    cosmosDbPartitionKey  = Environment.GetEnvironmentVariable("CustomerPartitionKey", EnvironmentVariableTarget.Process);
                    var    customerContainer     = CosmosUtilities.GetContainer(_cosmosClient, cosmosDbDatabaseName, cosmosDbContainerName, cosmosDbPartitionKey);
                    var    returnResult          = await customerContainer.ReadItemAsync <CustomerCosmos>(id, new PartitionKey(lastName));

                    var result = returnResult.Resource;
                    if (result != null)
                    {
                        customer = new CustomerDetail
                        {
                            FirstName   = result.FirstName,
                            LastName    = result.LastName,
                            Address     = result.Address,
                            Phonenumber = result.Phonenumber
                        };
                    }
                }
                if (null != customer)
                {
                    string jsonValue = JsonConvert.SerializeObject(customer);
                    response         = new HttpResponseMessage(HttpStatusCode.OK);
                    response.Content = new StringContent(jsonValue, UnicodeEncoding.UTF8, "application/json");
                }
                else
                {
                    response         = new HttpResponseMessage(HttpStatusCode.BadRequest);
                    response.Content = new StringContent("Invalid Request!", UnicodeEncoding.UTF8, "application/text");
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                log.LogError(ex.InnerException.Message);
                response         = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                response.Content = new StringContent(ex.Message, UnicodeEncoding.UTF8, "application/text");
            }
            return(response);
        }
        public async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            HttpResponseMessage response;

            log.LogInformation("C# HTTP trigger function processed a request.");
            try
            {
                string sqlQueryText = "";
                IDictionary <string, string> queryParams = req.GetQueryParameterDictionary();
                if (queryParams.ContainsKey("lastName"))
                {
                    string lastName = queryParams["lastName"].Replace("\"", "");
                    sqlQueryText = "SELECT c.FirstName,c.LastName,c.Address,c.Phonenumber FROM c WHERE (lower(c.LastName) =lower('" + lastName + "'))";
                }
                else
                {
                    sqlQueryText = "SELECT c.FirstName,c.LastName,c.Address,c.Phonenumber FROM c";
                }
                var cosmosDbDatabaseName  = Environment.GetEnvironmentVariable("databaseId", EnvironmentVariableTarget.Process);
                var cosmosDbContainerName = Environment.GetEnvironmentVariable("containerId", EnvironmentVariableTarget.Process);
                var cosmosDbPartitionKey  = Environment.GetEnvironmentVariable("CustomerPartitionKey", EnvironmentVariableTarget.Process);
                var returnList            = await CosmosUtilities.ReadItems(_cosmosClient, cosmosDbDatabaseName, cosmosDbContainerName, cosmosDbPartitionKey, sqlQueryText);

                string jsonValue = "";
                if (null != returnList)
                {
                    jsonValue = JsonConvert.SerializeObject(returnList);
                }
                response         = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StringContent(jsonValue, UnicodeEncoding.UTF8, "application/json");
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                log.LogError(ex.InnerException.Message);
                response         = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                response.Content = new StringContent(ex.Message, UnicodeEncoding.UTF8, "application/text");
            }


            return(response);
        }