Beispiel #1
0
        public static void WriteToCosmos(RatingResponse ratingExample)
        {
            // var cosmosUrl = new Uri()
            var collectionUri = UriFactory.CreateDocumentCollectionUri("RatingsDB", "Ratings");
            var endpointUrl   = new Uri("https://hacker12.documents.azure.com:443/");
            var cosmosKey     = "kMox9lT5nplspflT4HDRKttMj3M8GUZlsMIjqFnmPcoMMhP3sqVJfbIVt3bum3boU83QWfT0wq4p4yhHj4nMvg==";

            DocumentClient dClient = new DocumentClient(endpointUrl, cosmosKey);
            IDocumentQuery <RatingResponse> query = dClient.CreateDocumentQuery <RatingResponse>(collectionUri)
                                                    .AsDocumentQuery();
            var idanIds = dClient.CreateDocumentQuery(collectionUri);

            using (dClient)
            {
                dClient.CreateDatabaseIfNotExistsAsync(new Database()
                {
                    Id = "RatingsDB"
                }).GetAwaiter().GetResult();

                dClient.CreateDocumentCollectionIfNotExistsAsync(
                    UriFactory.CreateDatabaseUri("RatingsDB"),
                    new DocumentCollection {
                    Id = "Ratings"
                }).
                GetAwaiter()
                .GetResult();


                dClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("RatingsDB", "Ratings"), ratingExample).GetAwaiter().GetResult();
            }
            OkObjectResult results = new OkObjectResult(ratingExample);

            var idan = "blabbla";
        }
Beispiel #2
0
        public static async System.Threading.Tasks.Task <IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            var      id        = new System.Guid().ToString();
            DateTime timestamp = DateTime.Now;

            //string name = req.Query["name"];

            string  requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data        = JsonConvert.DeserializeObject <RatingPayload>(requestBody);

            RatingPayload rating = (RatingPayload)data;

            var  error          = String.Empty;
            bool isValidUser    = IsValidUser(rating.UserId);
            bool isValidProduct = IsValidProduct(rating.ProductId);
            bool isValidRating  = IsValidRating(rating.Rating);

            ActionResult result;

            error = isValidUser ? error : error + $"Rating Must have valid User\n";
            error = isValidProduct ? error : error + $"Rating Must have valid Product\n";
            error = isValidRating ? error : error + $"Rating Must have valid Rating i.e. 0-5\n";

            if (isValidUser && isValidProduct && isValidRating)
            {
                var rslt = new RatingResponse();
                rslt.id           = System.Guid.NewGuid().ToString();
                rslt.TimeStamp    = DateTime.Now;
                rslt.UserId       = rating.UserId;
                rslt.ProductId    = rating.ProductId;
                rslt.LocationName = rating.LocationName;
                rslt.Rating       = rating.Rating;
                rslt.UserNotes    = rating.UserNotes;

                //rating.id = System.Guid.NewGuid().ToString();
                //rating.TimeStamp = DateTime.Now;

                WriteToCosmos(rslt);
                result = (ActionResult) new OkObjectResult(rating);
            }
            else
            {
                result = new BadRequestObjectResult($"{error}");
            }

            return(result);
        }