Ejemplo n.º 1
0
 public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "rating/{ratingId}")] HttpRequest req, [CosmosDB("Ratings", "Ratings", ConnectionStringSetting = "hackfestapi_DOCUMENTDB", Id = "{ratingId}")] RatingData rating, ILogger log, string ratingId)
 {
     if (rating != null)
     {
         return(new OkObjectResult(rating));
     }
     return(new NotFoundObjectResult($"No rating exists with id {ratingId}"));
 }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log,
                                                     [CosmosDB("Ratings", "Ratings", ConnectionStringSetting = "hackfestapi_DOCUMENTDB")] IAsyncCollector <RatingData> ratingData)
        {
            log.LogInformation("Received request");
            HttpResponseMessage response = null;
            string  requestBody          = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);

            string userId    = data?.userId;
            string productId = data?.productId;

            if (int.TryParse(data?.rating.ToString(), out int rating))
            {
                if (rating > 5 && rating < 0)
                {
                    return(new BadRequestObjectResult("Rating must be between 0 and 5"));
                }
            }
            else
            {
                return(new BadRequestObjectResult("Invalid Rating"));
            }

            if (productId != null)
            {
                string productApiUrl = $"http://serverlessohproduct.trafficmanager.net/api/GetProduct?productId={productId}";
                response = await client.GetAsync(productApiUrl);

                if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    return(new BadRequestObjectResult("Invalid Product id"));
                }

                string productsAPIResonse = await response.Content.ReadAsStringAsync();

                dynamic products = JsonConvert.DeserializeObject(productsAPIResonse);
                if (products?.productId == null)
                {
                    return(new BadRequestObjectResult("Invalid Product id"));
                }
            }
            else
            {
                return(new BadRequestObjectResult("Product id is required"));
            }

            if (userId != null)
            {
                string userApiUrl = $"http://serverlessohuser.trafficmanager.net/api/Getuser?userId={userId}";
                response = await client.GetAsync(userApiUrl);

                if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    return(new BadRequestObjectResult("Invalid Product id"));
                }
                string usersAPIResonse = await response.Content.ReadAsStringAsync();

                dynamic users = JsonConvert.DeserializeObject(usersAPIResonse);
                if (users?.userId == null)
                {
                    return(new BadRequestObjectResult("Invalid user id"));
                }
            }
            else
            {
                return(new BadRequestObjectResult("user id is required"));
            }

            var ratingDocument = new RatingData
            {
                id        = Guid.NewGuid().ToString(),
                userId    = data.userId,
                productId = data.productId,
                timestamp = DateTime.UtcNow,
                rating    = rating,
                userNotes = data.userNotes
            };
            await ratingData.AddAsync(ratingDocument);

            return(new OkObjectResult(ratingDocument));
        }