public async Task <IActionResult> CreateAccount([FromBody] AccountDto bodyPayload) { try { long timeStamp = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(); bodyPayload.CreatedAt = timeStamp; bodyPayload.ModifiedAt = timeStamp; using (var connection = _databaseContext.Connection) { var result = await connection.ExecuteAsync("insert into Account (BankUserId,AccountNo,IsStudent,CreatedAt,ModifiedAt,Amount)" + "values (@BankUserId,@AccountNo,@IsStudent,@CreatedAt,@ModifiedAt,@Amount)", bodyPayload); if (result != 1) { return(NotFound("Account could not be created.")); } return(new ObjectResult("Account created.") { StatusCode = StatusCodes.Status204NoContent }); } } catch (Exception ex) { return(NotFound(ex.ToString())); } }
public async Task <IActionResult> UpdateBankUser([FromBody] BankUserDto bodyPayload) { /* TODO * Update FK refererences too * Transaction */ long timeStamp = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(); bodyPayload.ModifiedAt = timeStamp; using (var connection = _databaseContext.Connection) { var result = await connection.ExecuteAsync(@"update BankUser set UserId = @UserId, ModifiedAt = @ModifiedAt where Id = @Id ", bodyPayload); if (result == 1) { return new ObjectResult("BankUser updated.") { StatusCode = StatusCodes.Status204NoContent } } ; else { return(NotFound("BankUser not found or could not be updated.")); } } }
public async Task <IActionResult> CreateBankUser([FromBody] BankUserDto bodyInput) { try { long timeStamp = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(); bodyInput.CreatedAt = timeStamp; bodyInput.ModifiedAt = timeStamp; using (var connection = _databaseContext.Connection) { var result = await connection.ExecuteAsync(@"insert into BankUser (UserId, CreatedAt, ModifiedAt) values (@UserId, @CreatedAt, @ModifiedAt )", bodyInput); if (result == 1) { return new ObjectResult("BankUser created.") { StatusCode = StatusCodes.Status204NoContent } } ; else { return(NotFound("BankUser could not be created.")); } } } catch (Exception ex) { return(NotFound(ex.ToString())); } }
public async Task <IActionResult> AddDeposit([FromBody] DepositeRequest bodyPayload) { /* * Will receive a body with an amount and a BankUserId (/) * Amount deposited cannot be null or negative (/) * A deposit amount will first be sent to the interest rate function –the result will be saved in the database (/) * A record will be inserted in the deposits table as well (/) */ if (bodyPayload.Amount <= 0) { return(Conflict("Amount cannot be null negative.")); } try { double interestRateFuncResponse = 0; HttpResponseMessage intrestFunctResp = await HTTP.PostRequest("http://interest_rate_func/api/Interest_rate_function", new { amount = bodyPayload.Amount }, CancellationToken.None); // url is to be replaced if (intrestFunctResp != null && intrestFunctResp.StatusCode == HttpStatusCode.OK) { var temp = await intrestFunctResp.Content.ReadAsStringAsync(); interestRateFuncResponse = Convert.ToDouble(temp.Replace(".", ",")); } if (interestRateFuncResponse == 0) { return(NotFound("Interest rate function may be offline. Try again later.")); } DepositDto depositToInsert = new DepositDto(bodyPayload.BankUserId, TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(), bodyPayload.Amount); using (var connection = _databaseContext.Connection) { var result = await connection.ExecuteAsync("insert into Deposit (BankUserId,CreatedAt,Amount)" + "values (@BankUserId,@CreatedAt,@Amount)", depositToInsert); if (result != 1) { NotFound("Deposit could not be added."); } } return(Ok("Deposit added.")); } catch (Exception ex) { return(NotFound(ex)); } }
public async Task <IActionResult> UpdateAccount([FromBody] AccountDto bodyPayload) { long timeStamp = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(); bodyPayload.ModifiedAt = timeStamp; using (var connection = _databaseContext.Connection) { var result = await connection.ExecuteAsync("update Account set BankUserId = @BankUserId, AccountNo = @AccountNo, " + "IsStudent = @IsStudent, Amount = @Amount, ModifiedAt = @ModifiedAt", bodyPayload); if (result != 1) { return(NotFound("Account not found or could not be updated.")); } return(new ObjectResult("Account updated.") { StatusCode = StatusCodes.Status204NoContent }); } }
public async Task <IActionResult> PayLoan([FromBody] LoanRequest bodyInput) { /* * The loan can be paid integrally by using the /pay-loan endpoint. (/) * The request will contain the BankUserId and the LoanId as well. (/) * This will make the amount from a loan 0 and will subtract that amount from the accountof that user. (/) * If there aren’t enough money on the account, an error will be returned. (/) */ using (var connection = _databaseContext.Connection) { using (var transaction = connection.BeginTransaction()) { try { var data = new { BankUserId = bodyInput.BankUserId, LoanId = bodyInput.LoanId, Amount = 0.0, }; var loanAmountResult = await connection.QueryFirstOrDefaultAsync("select Amount from Loan where BankUserId = @BankUserId AND Id = @LoanId", data, transaction); if (loanAmountResult.Amount == 0) { return(Ok("This loan has already been paid before.")); } var IsAmountOnAccountEnoguhResult = await connection.QueryFirstOrDefaultAsync("select Amount from Account where BankUserId = @BankUserId AND Amount > @Amount", new { bodyInput.BankUserId, loanAmountResult.Amount }, transaction); if (IsAmountOnAccountEnoguhResult == null) { return(NotFound("Account balance is low, therefore loan cannot be paid.")); } var setLoanToZeroResult = await connection.ExecuteAsync("update Loan set Amount = 0 where BankUserId = @BankUserId AND Id = @LoanId", data, transaction); var substractAccountAmountResult = await connection.ExecuteAsync("update Account set Amount = Amount - @Amount, ModifiedAt = @ModifiedAt " + "where BankUserId = @BankUserId", new { Amount = loanAmountResult.Amount, ModifiedAt = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(), BankUserId = bodyInput.BankUserId }, transaction); if (substractAccountAmountResult != 1 || setLoanToZeroResult != 1) { new Exception(); // Change to custom exception } transaction.Commit(); return(Ok("Loan sucessfully paid.")); } catch (Exception ex) { transaction.Rollback(); return(NotFound("Loan could not be paid.")); } } } }
public async Task <IActionResult> CreateLoan([FromBody] LoanRequest bodyPayload) { /* * Request will contain the BankUserIdand LoanAmount. (/) * A POST request will be made to the Loan Algorithm Functionwith an amount and the total account amount for that user(/) * If the status code is 200, a loan record will be created (/) * If the status code is 403 or similar, an error will be returned (/) * The amount will be added to the Account of that user –if it is successfulof course. (/) */ if (bodyPayload.LoanAmount <= 0) { return(BadRequest("LoanAmount cannot be 0 or negative")); } using (var connection = _databaseContext.Connection) { using (var transaction = connection.BeginTransaction()) { try { var totalLoanResult = await connection.QueryFirstOrDefaultAsync("select SUM(Amount) as Total from Loan where BankUserId = @BankUserId", new { BankUserId = bodyPayload.BankUserId }, transaction); if (totalLoanResult == null) { return(NotFound("Account could not be fetched")); } var response = await HTTP.PostRequest("http://loan_algorythm_func/api/Loan_Algorythm_Function", new { loanAmount = bodyPayload.LoanAmount, accountAmount = totalLoanResult.Total }, CancellationToken.None); // URL is to be filled if (response.StatusCode == HttpStatusCode.OK) { long timeStamp = TimeStamp.GetDateTimeOffsetNowAsUnixTimeStampInSeconds(); LoanDto loanDto = new LoanDto(bodyPayload.BankUserId, timeStamp, timeStamp, bodyPayload.LoanAmount); var loanResult = await connection.ExecuteAsync("insert into Loan(BankUserId,CreatedAt,ModifiedAt,Amount)" + "values (@BankUserId,@CreatedAt,@ModifiedAt,@Amount)", loanDto, transaction); AccountDto accountDto = new AccountDto(); accountDto.Amount = bodyPayload.LoanAmount; accountDto.ModifiedAt = timeStamp; accountDto.BankUserId = bodyPayload.BankUserId; var accountResult = await connection.ExecuteAsync("update Account set Amount = @Amount, ModifiedAt = @ModifiedAt where BankUserId = @BankUserId", accountDto, transaction); if (loanResult != 1 && accountResult != 1) { new Exception(); // Need be replaced with custom exception. } } else { return(Conflict("This account is not eligible for further loans.")); } transaction.Commit(); return(Ok("Loan created.")); } catch (Exception ex) { transaction.Rollback(); return(NotFound("Loan could not be created.")); } } } }