Example #1
0
        // 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?}");
            });
        }
        //Record the state of all accounts if the month has changed
        public async static Task<bool> RecordBalance(IBalanceReport balanceRepo, IAccountRepository accountRepo)
        {
            //created a variable to keep async bool returns, set it to false just in case the code doesnt work, will return false
            bool success = false;
            //I record onyl Crdit and Debit account balances because only their balances matter. 
            var accounts = await accountRepo.GetAccountByTwoTypes(AccountType.Credit, AccountType.Debit);
            //Because the record is happening in the new month any first day the app is accessed, but
            //the report needs to be for the end of the last month. So the date is last months date. 
            var lastMonthsDate = DateTime.Now.AddMonths(-1);

            foreach (var acc in accounts)
            {
                var balanceReport = new BalanceReport
                {
                    AccountId = acc.Id,
                    Date = lastMonthsDate,
                    Value = acc.Value
                };
                success = await balanceRepo.Create(balanceReport);
            }

            return success;
        }
        //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;

                }

            }
        }
 public BalanceReportController(IMapper mapper, IBalanceReport balanceReport)
 {
     _balanceRepo = balanceReport;
     _mapper      = mapper;
 }