Пример #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string userId       = req.Query["userId"];
            string productId    = req.Query["productId"];
            string locationName = req.Query["locationName"];
            string rating       = req.Query["rating"];
            string userNotes    = req.Query["userNotes"];

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

            userId       = userId ?? data?.userId;
            productId    = productId ?? data?.productId;
            locationName = locationName ?? data?.locationName;
            rating       = rating ?? data?.rating;
            userNotes    = userNotes ?? data?.userNotes;

            var userIdCheck = await CheckUserId(userId);

            var productIdCheck = await CheckProductId(productId);

            bool ratingCheck = false;
            int  test        = int.Parse(rating);

            if (0 <= int.Parse(rating) && int.Parse(rating) <= 5)
            {
                ratingCheck = true;
            }

            if (userIdCheck && productIdCheck && ratingCheck)
            {
                var newItem = new Rating();
                newItem.id           = Guid.NewGuid().ToString();
                newItem.locationName = locationName;
                newItem.productId    = productId;
                newItem.rating       = int.Parse(rating);
                //newItem.timestamp = DateTime.UtcNow.ToString();
                newItem.userId    = userId;
                newItem.userNotes = userNotes;

                // TODO�FConnect DB
                try
                {
                    Console.WriteLine("Beginning operations...\n");
                    CosmosDBAccess p        = new CosmosDBAccess();
                    var            response = p.CerateRatingDataAsync(newItem);
                }
                catch (CosmosException de)
                {
                    Exception baseException = de.GetBaseException();
                    Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: {0}", e);
                }
                finally
                {
                    Console.WriteLine("End of demo, press any key to exit.");
                    //Console.ReadKey();
                }
                return((ActionResult) new OkObjectResult($"success_{newItem}"));
            }


            // Error Message
            string errorMessage = null;

            if (!userIdCheck)
            {
                errorMessage += "Incorrect userId";
            }
            if (!productIdCheck)
            {
                errorMessage += " Incorrect productId";
            }
            if (!ratingCheck)
            {
                errorMessage += " Incorect rating";
            }


            return(userIdCheck && productIdCheck && ratingCheck
                ? (ActionResult) new OkObjectResult($"Succeed. TODO Create JSON")
                : new BadRequestObjectResult($"Error:{errorMessage}"));
        }