Exemple #1
0
        public async Task <IActionResult> Create(Account account)
        {
            if (ModelState.IsValid)
            {
                account.EncryptPassword(account.Password);
                _context.Add(account);
                _context.Add(account.Customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(account));
        }
Exemple #2
0
        public async Task MarkAsPaid(long quoteId, string transactionId)
        {
            if (String.IsNullOrEmpty(transactionId))
            {
                throw new BadRequestException();
            }

            using (InsuranceDbContext ctx = ContextFactory.CreateContext())
            {
                Policy policy = await ctx.Policies.SingleAsync(x => x.Id == quoteId);

                if (policy.IsIssued)
                {
                    throw new BadRequestException("Cannot update an issued Policy.");
                }

                // set the state of the policy to policy now we know it's been paid.
                policy.State   = PolicyState.Policy;
                policy.Payment = new PaymentDetail()
                {
                    TransactionId = transactionId,
                    CreatedAt     = DateTime.UtcNow
                };

                policy.PolicyNumber = "BKE" + policy.Id.ToString("D9");

                await ctx.SaveChangesAsync();
            }
        }
        //Works as upsert. This partially handles conflicts but still there is a small chance of race condition
        //Using some locks over the transaction (like isolation level serializable) is also possible
        //but may have side effects while reading the data being updating.
        //I think, letting back office throw ex better than affecting the insurance calculation performance as it affects customer experience
        public async Task SaveSurchargeRateAsync(SaveSurchargeRateRequest request)
        {
            var existingRule = await _dbContext
                               .InsuranceProductTypeRules
                               .FirstOrDefaultAsync(rule => rule.Type == InsuranceProductTypeRuleType.AppliesToProduct &&
                                                    rule.ProductTypeId == request.ProductTypeId);

            if (existingRule != null)
            {
                existingRule.InsuranceCost = request.InsuranceCost;
            }
            else
            {
                _dbContext
                .InsuranceProductTypeRules
                .Add(new InsuranceProductTypeRule
                {
                    Type          = InsuranceProductTypeRuleType.AppliesToProduct,
                    InsuranceCost = request.InsuranceCost,
                    ProductTypeId = request.ProductTypeId
                });
            }

            await _dbContext.SaveChangesAsync();
        }
        public async Task <Policy> Create(BicyclePolicyDetailDTO policyDto)
        {
            using (InsuranceDbContext ctx = ContextFactory.CreateContext())
            {
                Policy policy = new Policy()
                {
                    State       = PolicyState.Quote,
                    Description = "Bicycle Insurance",
                    CreatedAt   = DateTime.UtcNow,
                    Detail      = CreateDetailModelFromDto(policyDto)
                };

                policy.Option = await CalculatePolicyOption(policy);

                ctx.Policies.Add(policy);
                await ctx.SaveChangesAsync();

                policy.PolicyNumber = $"QTE{policy.Id:D9}";
                await ctx.SaveChangesAsync();

                return(policy);
            }
        }
Exemple #5
0
        public async Task <Policy> SetContact(long id, PolicyContactDTO contact)
        {
            if (contact == null)
            {
                throw new BadRequestException();
            }

            using (InsuranceDbContext context = ContextFactory.CreateContext())
            {
                Policy policy = await FetchPolicy(context, id, true);

                policy.Contact = CreateContactFromDto(policy.Contact, contact);

                await context.SaveChangesAsync();

                return(policy);
            }
        }
        public async Task <Policy> SetDetails(long id, BicyclePolicyDetailDTO details)
        {
            if (details == null)
            {
                throw new BadRequestException();
            }

            using (InsuranceDbContext ctx = ContextFactory.CreateContext())
            {
                Policy policy = await ctx.Policies.SingleAsync(x => x.Id == id);

                if (policy.IsIssued)
                {
                    throw new BadRequestException("Cannot update an issued Policy.");
                }

                policy.Detail = CreateDetailModelFromDto(details);
                policy.Option = await CalculatePolicyOption(policy);

                await ctx.SaveChangesAsync();

                return(policy);
            }
        }
Exemple #7
0
        public async Task <Policy> SetPolicyOption(long policyId, PolicyOptionDTO option)
        {
            if (option == null)
            {
                throw new BadRequestException();
            }

            using (InsuranceDbContext context = ContextFactory.CreateContext())
            {
                Policy policy = await FetchPolicy(context, policyId, true);

                if (policy.Option == null)
                {
                    await CalculatePolicyOption(policy);
                }

                policy.Option = CreatePolicyOptionFromCoverOption(policy.Option, option);
                policy.Option = await CalculatePolicyOption(policy);

                await context.SaveChangesAsync();

                return(policy);
            }
        }
        public async Task Create(TEntity entity)
        {
            await Context.Set <TEntity>().AddAsync(entity);

            await Context.SaveChangesAsync();
        }
 public async Task SaveChangesAsync()
 {
     await _context.SaveChangesAsync();
 }