C
C#•10mo ago
GuardianZ

Unable to calculate/store correct value to the db

Hello. I am developing a mortgage tracker for myself and currently trying to calculate my daily interest in my service controller. I am capturing my current interest rate and outstanding balance using the front end form, then the idea is to retrieve this value from the database in my service, calculate my actual daily interest rate and store it back into the db in my main interest rate controller. When I open my SQL explorer the value is unfortunately stored as 0.0000 decimal value and I can't debug it for the world at the moment as to why? This is the service controller:
csharp
namespace Finance101.Services
{
public class DailyInterestRateService
{

private readonly Finance101Context _context;

public DailyInterestRateService()
{
}

public DailyInterestRateService(Finance101Context context)
{
_context = context;
}

public async Task<decimal> CalculateDailyInterestRate()
{
int daysInYear = 365;
decimal dailyInterestRate;
//retrieve current interest rate from the database
var mortgageForm = await _context.MortgageForm.FirstOrDefaultAsync();
if (mortgageForm == null)
{
throw new Exception("No mortgage form data found in the database");
}
else
{
dailyInterestRate = (decimal)mortgageForm.CurrentInterestRate * mortgageForm.OutstandingMortgageBalance / (100 * daysInYear);
}

return dailyInterestRate;
}
}
}
csharp
namespace Finance101.Services
{
public class DailyInterestRateService
{

private readonly Finance101Context _context;

public DailyInterestRateService()
{
}

public DailyInterestRateService(Finance101Context context)
{
_context = context;
}

public async Task<decimal> CalculateDailyInterestRate()
{
int daysInYear = 365;
decimal dailyInterestRate;
//retrieve current interest rate from the database
var mortgageForm = await _context.MortgageForm.FirstOrDefaultAsync();
if (mortgageForm == null)
{
throw new Exception("No mortgage form data found in the database");
}
else
{
dailyInterestRate = (decimal)mortgageForm.CurrentInterestRate * mortgageForm.OutstandingMortgageBalance / (100 * daysInYear);
}

return dailyInterestRate;
}
}
}
3 Replies
GuardianZ
GuardianZOP•10mo ago
And my main controller below that stores the calculated value back into db:
namespace Finance101.Controllers
{
public class InterestRateController : Controller
{
private readonly DailyInterestRateService _dailyInterestRateService;
private readonly Finance101Context _context;

public InterestRateController(DailyInterestRateService dailyInterestRateService, Finance101Context context)
{
_dailyInterestRateService = dailyInterestRateService;
_context = context;
}

[HttpPost]
public async Task<IActionResult> SaveDailyInterestRate()
{
try
{
//retrieve calculated daily interest rate from the service
decimal dailyInterestRate = await _dailyInterestRateService.CalculateDailyInterestRate();
//create a new instance of the MortgageForm model
var mortgageForm = new MortgageForm
{
DailyInterestRate = dailyInterestRate
};

//save the daily interest rate to the database
_context.MortgageForm.Add(mortgageForm);
await _context.SaveChangesAsync();

return Ok();
}
catch (Exception ex)
{
//ILogger.LogError(ex, "Error saving daily interest rate");
return StatusCode(500, ex);
}
}
}
}
namespace Finance101.Controllers
{
public class InterestRateController : Controller
{
private readonly DailyInterestRateService _dailyInterestRateService;
private readonly Finance101Context _context;

public InterestRateController(DailyInterestRateService dailyInterestRateService, Finance101Context context)
{
_dailyInterestRateService = dailyInterestRateService;
_context = context;
}

[HttpPost]
public async Task<IActionResult> SaveDailyInterestRate()
{
try
{
//retrieve calculated daily interest rate from the service
decimal dailyInterestRate = await _dailyInterestRateService.CalculateDailyInterestRate();
//create a new instance of the MortgageForm model
var mortgageForm = new MortgageForm
{
DailyInterestRate = dailyInterestRate
};

//save the daily interest rate to the database
_context.MortgageForm.Add(mortgageForm);
await _context.SaveChangesAsync();

return Ok();
}
catch (Exception ex)
{
//ILogger.LogError(ex, "Error saving daily interest rate");
return StatusCode(500, ex);
}
}
}
}
Angius
Angius•10mo ago
Does CalculateDailyInterestRate method return what you'd expect it to return?
GuardianZ
GuardianZOP•10mo ago
it does I think, but realised in the mean time I am being completely dumb as usual. I haven't invoked the method anywhere or on any action, so I decided to call the method when I hit the submit button on the form. The idea is that it saves the form data to the db and then invokes the method to perform calculation on it but now I am getting a completely new unhandled exception which I don't understand at the moment, but working on it: this is my main controller code for submitting the form now:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SubmitForm([Bind("Id,OutstandingMortgageBalance,RepaymentTerm,CurrentInterestRate,CurrentMonthlyPayment")] MortgageForm mortgageForm)
{
if (ModelState.IsValid)
{
_context.Add(mortgageForm);
await _context.SaveChangesAsync();

// call method in InterestRateController to calculate and save daily interest rate
await _interestRateController.SaveDailyInterestRate();

return RedirectToAction(nameof(Index));
}
return View(mortgageForm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SubmitForm([Bind("Id,OutstandingMortgageBalance,RepaymentTerm,CurrentInterestRate,CurrentMonthlyPayment")] MortgageForm mortgageForm)
{
if (ModelState.IsValid)
{
_context.Add(mortgageForm);
await _context.SaveChangesAsync();

// call method in InterestRateController to calculate and save daily interest rate
await _interestRateController.SaveDailyInterestRate();

return RedirectToAction(nameof(Index));
}
return View(mortgageForm);
}
It seems the current issue might be that I am injecting controller with my method into another controller, instead of having a service for this? Edit: I haven't registered my service in Program.cs file, all good now! 🙂
Want results from more Discord servers?
Add your server