// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure( IApplicationBuilder app, IWebHostEnvironment env, IDateKeeper dateKeeper, IBalanceReport balanceRepo, IAccountRepository accountRepo) { //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} //else //{ // app.UseExceptionHandler("/Home/Error"); // // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. // app.UseHsts(); //} app.UseExceptionHandler("/error"); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); RecordBalancaData.CheckDate(dateKeeper, balanceRepo, accountRepo); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
//Create a new entry for DateKeeper if doesnt exist public static bool CreateDateKeeper(IDateKeeper dateKeeper, DateTime todaysDate) { var lastVisit = new DateKeeper { LastStarted = todaysDate }; return dateKeeper.Create(lastVisit).Result; }
//Had to use .Result because was having an "Disposed Object" error. public static void CheckDate(IDateKeeper dateKeeper, IBalanceReport balanceRepo, IAccountRepository accountRepo) { bool success; var exist = dateKeeper.Exists(1).Result; var todaysDate = DateTime.Now.Date; if (!exist) success = CreateDateKeeper(dateKeeper, todaysDate); else { var lastVisit = dateKeeper.FindById(1).Result; //if the month has changed since last record, Make new record and update date. if(lastVisit.LastStarted.GetValueOrDefault().Month != todaysDate.Month) { success = RecordBalance(balanceRepo, accountRepo).Result; lastVisit.LastStarted = todaysDate; success = dateKeeper.Update(lastVisit).Result; } } }