예제 #1
0
        public static async Task SaveClaimToDb(
            [ActivityTrigger] WhatIfDemoDbDataContext.Claim claim,
            ILogger log)
        {
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            ctx.Claims.Add(claim);

            await ctx.SaveChangesIdempotentlyAsync(ex => {
                // Explicitly handling the case of duplicated execution.
                // Which might happen, if the process crashes or restarts.
                log.LogError($"Failed to add policy {claim.id} from userId {claim.userId}: {ex.Message}");
            });

            log.LogWarning($"Saved claim {claim.id} from userId {claim.userId}");
        }
        public static async Task <WhatIfDemoDbDataContext.Policy> SavePolicyToDb(
            [ActivityTrigger] WhatIfDemoDbDataContext.Policy policy,
            ILogger log)
        {
            // Saving the policy to Azure SQL DB
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            ctx.Policies.Add(policy);

            await ctx.SaveChangesIdempotentlyAsync(ex => {
                // Explicitly handling the case of duplicated execution.
                // Which might happen, if the process crashes or restarts.
                log.LogError($"Failed to add policy {policy.id} from userId {policy.userId}: {ex.Message}");
            });

            log.LogWarning($"Saved policy {policy.id} from userId {policy.userId}");

            // Returning the Policy object back, just to show this context propagation mechanism
            return(policy);
        }
예제 #3
0
        public static async Task Cleanup(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
            HttpRequest request,
            ILogger log)
        {
            string userId = await Helpers.GetAccessingUserIdAsync(request, acceptedAuthProviders);

            // Loading user-specific data from Azure SQL database
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            // Removing all records for this user from SQL DB
            foreach (var policy in ctx.Policies.Where(p => p.userId == userId))
            {
                ctx.Policies.Remove(policy);
            }

            foreach (var claim in ctx.Claims.Where(p => p.userId == userId))
            {
                ctx.Claims.Remove(claim);
            }

            await ctx.SaveChangesAsync();
        }
예제 #4
0
        public static async Task <IEnumerable <Quote> > GetQuotes(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
            HttpRequest request,
            // Getting all documents from Cosmos DB "Products" collection as a method parameter
            [CosmosDB(databaseName: "WhatIfDemoDb", collectionName: "Products", ConnectionStringSetting = "CosmosDBConnection", SqlQuery = "select * from c")]
            IEnumerable <Product> products,
            ILogger log)
        {
            string userId = await Helpers.GetAccessingUserIdAsync(request);

            // Loading user-specific data from Azure SQL database
            var ctx = await WhatIfDemoDbDataContext.CreateAsync();

            int policiesCount = await ctx.Policies.CountAsync(p => p.userId == userId);

            int claimsCount = await ctx.Claims.CountAsync(c => c.userId == userId);

            // Giving 10% penalty for each claim, but not more than 200%
            decimal penalty = 1M + 0.1M * claimsCount;

            penalty = penalty > 2M ? 2M : penalty;

            // Giving 5% discount for each previously bought policy, but not more than 50%
            decimal discount = 1M - 0.05M * policiesCount;

            discount = discount < 0.5M ? 0.5M : discount;

            return(products.Select(p => new Quote
            {
                productId = p.id,
                // Inferring quoteId from productId and the number of existing policies.
                // quoteId will then be used as identifier for order message, and this is how we deduplicate them
                quoteId = p.id + "_" + policiesCount,
                title = p.title,
                price = p.price * penalty * discount
            }));
        }