Beispiel #1
0
        public async Task <IActionResult> Ratings(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Ratings function processed a request.");

            string coinId = req.Query["coinId"];

            if (coinId == null)
            {
                return(new BadRequestObjectResult("Please pass a coinId on the query string"));
            }

            await ratingsDataTableStorage.Init();

            var ratings = await ratingsDataTableStorage.GetRatings(coinId);

            if (ratings.Count() == 0)
            {
                return(new NotFoundResult());
            }

            string filter   = req.Query["order"];
            string limitStr = req.Query["limit"];

            if (filter != null &&
                limitStr != null &&
                int.TryParse(limitStr, out var limit))
            {
                switch (filter.ToLower())
                {
                case "top":
                    ratings = ratings.OrderByDescending(x => x.AverageRating).Take(limit);
                    break;

                case "bad":
                    ratings = ratings.OrderBy(x => x.AverageRating).Take(limit);
                    break;

                default:
                    break;
                }
            }

            return(new OkObjectResult(ratings));
        }
        private async Task <double> GetCoinRating(string coinId)
        {
            var ratings = await ratingDataTableStorage.GetRatings(coinId);

            if (ratings.Count() == 0)
            {
                return(0);
            }

            return(ratings.Average(x => x.AverageRating));
        }