public static async Task RunAsync(
            [QueueTrigger("userUpdatesQueue")] string user,
            [Table("Costs")] CloudTable costsTable,
            [Table("CostsByMonth")] CloudTable costsByMonthTable,
            ILogger log)
        {
            log.LogInformation($"Regenerating Read Models for User '{user}'");

            var costs = await CostTable.GetCostsForUser(user, costsTable);

            var monthlyUserCosts =
                costs
                .GroupBy(cost => cost.Date.Year.ToString() + "-" + cost.Date.Month.ToString("D2"))
                .Select(monthGroup =>
                        new MonthlySummaryTableEntity {
                PartitionKey = user,
                RowKey       = monthGroup.Key,
                Month        = monthGroup.Key,
                User         = user,
                ValuePence   = (int)(monthGroup.Sum(cost => cost.Value) * 100)
            });

            foreach (var monthlyUserCost in monthlyUserCosts)
            {
                await costsByMonthTable.ExecuteAsync(TableOperation.InsertOrReplace(monthlyUserCost));
            }
        }
示例#2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Costs/{User}")] HttpRequest req,
            [Table("Costs")] CloudTable costsTable,
            string User,
            ILogger log)
        {
            log.LogInformation($"Get Costs/{User} Called");

            var costs = await CostTable.GetCostsForUser(User, costsTable);

            var sortedCosts = costs.OrderByDescending(cost => cost.Date);

            var outputCosts = sortedCosts.Select(cost => new CostOutputModel {
                Item  = cost.Item,
                Value = cost.Value,
                Date  = cost.Date.LocalDateTime.ToString("dd-MMM-yyyy")
            });

            return(new JsonResult(outputCosts));
        }