public static async Task LoadCoupons(int allotCoupons = 100) { var lastPoint = GetCheckPoint(); //get all codes not in production var availCoupons = _context.CouponRepositories .Where(c => c.InProduction == false) .Count(); //get all code in the repository var totalRepo = _context.CouponRepositories.Count(); if (allotCoupons >= availCoupons) { await CouponCodes.Run(totalRepo, allotCoupons); } var getCodes = _context.CouponRepositories .Where(c => c.CouponRepositoryID >= (lastPoint + 1) && c.CouponRepositoryID <= (lastPoint + allotCoupons)) .ToList(); //Load Repo Code to Production //Update Repo Code Status In repository foreach (var code in getCodes) { _context = new ApplicationDbContext(); var productionCode = new ProductionCode { CouponCode = code.CouponCode }; _context.ProductionCodes.Add(productionCode); var repoCode = _context.CouponRepositories.FirstOrDefault(c => c.CouponRepositoryID == code.CouponRepositoryID); repoCode.InProduction = true; await _context.SaveChangesAsync(); } //Update Check point var lastValue = getCodes[getCodes.Count - 1]; _context.CheckPoints.Add(new CheckPoint { LastPoint = lastValue.CouponRepositoryID }); await _context.SaveChangesAsync(); }
public async void Execute(IJobExecutionContext context) { //throw new NotImplementedException(); //Get All Coupons var getAllCoupons = _context.CouponRepositories.Count(); //Gets coupons in production var inProduction = _context.CouponRepositories.Where(x => x.InProduction == true).Count(); if (getAllCoupons == 0) { await CouponCodes.Run(getAllCoupons, GenerateCode); } if (await DoIGenerateAsync(inProduction: inProduction, totalCouponsInRepo: getAllCoupons)) { await CouponCodes.Run(getAllCoupons, GenerateCode); } }