Ejemplo n.º 1
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "ratingsdata",
                 collectionName: "ratings",
                 ConnectionStringSetting = "CosmosDBConnection")] out dynamic cosmosDoc,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string      requestBody = new StreamReader(req.Body).ReadToEnd();
            RatingClass bodyRating  = JsonConvert.DeserializeObject <RatingClass>(requestBody);

            cosmosDoc = null;

            if (bodyRating is null)
            {
                return(new BadRequestResult());
            }

            try{
                Task.Run(() => ValidateUserIdAsync(bodyRating.userId));
                log.LogInformation($"User validated");
            } catch (Exception)
            {
                log.LogInformation("Couldn't validate user");
                return(new BadRequestResult());
            }

            try{
                Task.Run(() => ValidateProductIdAsync(bodyRating.productId));
                log.LogInformation($"Product validated");
            } catch (Exception)
            {
                log.LogInformation("Couldn't validate product");
                return(new BadRequestResult());
            }

            bodyRating.id        = Guid.NewGuid().ToString();
            bodyRating.timeStamp = DateTime.UtcNow;

            if (bodyRating.rating < 0 || bodyRating.rating > 5)
            {
                return(new BadRequestResult());
            }

            cosmosDoc = bodyRating;
            log.LogInformation($"Saving rating to CosmosDB: {cosmosDoc}");

            return(new OkObjectResult(bodyRating));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "ratingsdata",
                 collectionName: "ratingsV2",
                 ConnectionStringSetting = "CosmosDBConnection",
                 Id = "{Query.id}",
                 PartitionKey = "{Query.userId}")] RatingClass rating,
            ILogger log)
        {
            log.LogInformation("GetRating HTTP trigger function processed a request");

            if (rating == null)
            {
                log.LogInformation($"Rating item not found");
                return(new BadRequestResult());
            }
            else
            {
                log.LogInformation($"Found Rating item, Description={rating.id}");
                return(new OkObjectResult(rating));
            }
        }