public bool CheckUsageLimit(paymentDto payment) { var result = _context.TblPayment.Where(x => x.TransactionDate.Month == DateTime.Now.Month) .Where(x => x.AccountId == Convert.ToInt32(payment.AccountId)); result = result.GroupBy(o => o.AccountId) .Select(g => new TblPayment { AccountId = g.Key, Amount = g.Sum(i => i.Amount) }); var monthCreditLimit = _context.TblAccount.Where(x => x.AccountId == Convert.ToInt32(payment.AccountId)) .Select(x => x.MonthlyCreditLimit).SingleOrDefault(); if (Convert.ToDouble(payment.Amount) > monthCreditLimit) { return(false); } foreach (var group in result) { if (Convert.ToDouble(group.Amount) + Convert.ToDouble(payment.Amount) > monthCreditLimit) { return(false); } else { return(true); } } return(true); }
public async Task <IActionResult> Create(paymentDto oPaymentDto) { if (!_repo.CheckUsageLimit(oPaymentDto)) { oPaymentDto.Response = "Amount exceeds the Monthly Limit"; return(Ok(oPaymentDto)); } // INSERT if (oPaymentDto.TransactionId == null) { var payment = new TblPayment { AccountId = Convert.ToInt32(oPaymentDto.AccountId), Amount = Convert.ToDouble(oPaymentDto.Amount), TransactionDate = DateTime.Now }; var createdAccount = await _repo.Create(payment); return(Ok(createdAccount)); } // UPDATE else { var payment = new TblPayment { TransactionId = Convert.ToInt32(oPaymentDto.TransactionId), AccountId = Convert.ToInt32(oPaymentDto.AccountId), Amount = Convert.ToDouble(oPaymentDto.Amount), TransactionDate = DateTime.Now }; var updatedUser = _repo.Update(payment); return(Ok(updatedUser)); } }