public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("GetRating function triggered..."); string ratingId = req.Query["ratingId"]; Guid parsedRatingId; if (string.IsNullOrWhiteSpace(ratingId) || Guid.TryParse(ratingId, out parsedRatingId) == false) { log.LogInformation($"Invalid rating id {ratingId}"); return(new BadRequestObjectResult($"Invalid rating id {ratingId}")); } // get data from cosmos db ProductRating productRating = null; try { productRating = new ProductRatings().Get(ratingId); return(new OkObjectResult(JsonConvert.SerializeObject(productRating))); } catch { return(new NotFoundObjectResult($"ProductRating not found for rating id {ratingId}")); } }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("GetRatings function triggered..."); var userId = req.Query["userId"]; Guid parsedUserId; if (string.IsNullOrWhiteSpace(userId) || Guid.TryParse(userId, out parsedUserId) == false) { log.LogInformation($"Invalid user id {userId}"); return(new BadRequestObjectResult($"Invalid user id {userId}")); } //resync // get the ratings for the user from your database try { IEnumerable <ProductRating> productRatings = new ProductRatings().All(userId); return(new OkObjectResult(JsonConvert.SerializeObject(productRatings))); } catch { return(new NotFoundObjectResult($"no ProductRating found for user id {userId}")); } }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { bool invalidinput = false; string errorString = ""; ProductRatings prs = new ProductRatings(); string getProductURL = "https://serverlessohproduct.trafficmanager.net/api/GetProduct?productId="; string getUserURL = "https://serverlessohuser.trafficmanager.net/api/GetUser?userId="; log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ProductRating rating = JsonConvert.DeserializeObject <ProductRating>(requestBody); HttpWebResponse queryResult; var webReq = HttpWebRequest.CreateHttp(getProductURL + rating.ProductId); try { queryResult = (HttpWebResponse)webReq.GetResponse(); if (queryResult.StatusCode != HttpStatusCode.OK) { errorString += $"Invalid product {rating.ProductId}\n"; log.LogInformation(errorString); } } catch { errorString += $"Invalid product {rating.ProductId}\n"; log.LogInformation(errorString); invalidinput = true; } webReq = HttpWebRequest.CreateHttp(getUserURL + rating.UserId); try { queryResult = (HttpWebResponse)webReq.GetResponse(); if (queryResult.StatusCode != HttpStatusCode.OK) { errorString += $"Invalid User {rating.UserId}\n"; log.LogInformation(errorString); } } catch //test git { errorString += $"Invalid User {rating.UserId}\n"; log.LogInformation(errorString); invalidinput = true; } //var test = new StreamReader(queryResult.GetResponseStream()).ReadToEnd(); if (rating.Rating < 0 || rating.Rating > 5) { errorString += $"Invalid product rating {rating.Rating}\n"; log.LogInformation(errorString); invalidinput = true; } if (!invalidinput) { rating.TimeStamp = DateTime.UtcNow; var ratingId = prs.Add(rating); rating.Id = ratingId; string resultObject = JsonConvert.SerializeObject(rating); return(new OkObjectResult($"{resultObject}")); } else { return(new BadRequestObjectResult(errorString)); } // string name = req.Query["name"]; // dynamic data = JsonConvert.DeserializeObject(requestBody); // name = name ?? data?.name; // return name != null ? // (ActionResult)new OkObjectResult($"Hello, {name}") : // new BadRequestObjectResult("Please pass a name on the query string or in the request body"); }