public async Task <IActionResult> PostAsync([FromBody] Transference transference) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var isSuccessful = await _transactionService.Create(transference.OriginAccountId, transference.DestinationAccountId, transference.Value); if (!isSuccessful) { return(this.StatusCode((int)HttpStatusCode.BadRequest, new { message = "Ops! Tente novamente mais tarde." })); } return(Ok()); } catch (Exception) { ///TODO: logar erro (criar middleware que faça esse trabalho). /// O ideal é não subir exception e sim retornar um obj com o erro. Exceptions custam processamento! return(this.StatusCode((int)HttpStatusCode.BadRequest, new { message = "Ops! Tente novamente mais tarde." })); } }
public void Transfer(Transference transference) { var receptor = UserList.FirstOrDefault(x => x.Identification == transference.Receptor); if (receptor == null) { return; } Deposit(receptor, transference.MoneyToBeTransfered); Withdraw(transference.CurrentUser, transference.MoneyToBeTransfered); }
public ActionResult <string> Post([FromBody] Transference transference) { try { return(businessService.postTransference(transference)); } catch (Exception ex) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; throw ex; } }
public string postTransference(Transference transference) { try { if (transference.destinyAccountId.Equals(transference.originAccountId)) { return("Destiny and origin accounts can't be equals. Transference failed"); } if (transference.value.Equals(0)) { return("Transference value can't be zero. Transference failed"); } if (transference.destinyAccountId == null) { return("Destiny account id can`t be null. Transference failed"); } if (transference.originAccountId == null) { return("Origin account id can`t be null. Transference failed"); } //ORIGIN TRANSACTION Transaction originAccountTransaction = new Transaction(); originAccountTransaction.value = transference.value * -1; originAccountTransaction.description = transference.description; originAccountTransaction.accountId = transference.originAccountId; postTransaction(originAccountTransaction); //DESTINY TRANSACTION Transaction destinyAccountTransaction = new Transaction(); destinyAccountTransaction.value = transference.value; destinyAccountTransaction.description = transference.description; destinyAccountTransaction.accountId = transference.destinyAccountId; postTransaction(destinyAccountTransaction); return("Transference successfully recorded."); } catch (Exception ex) { throw ex; } }
public async Task UnsuccessfullyAuthentication(int originId, int destinationId, double value) { var transference = new Transference() { OriginAccountId = originId, DestinationAccountId = destinationId, Value = value }; var reqJson = JsonConvert.SerializeObject(transference); var resp = await _client.PostAsync("/api/transferences", new StringContent(reqJson, Encoding.UTF8, "application/json")); Assert.NotNull(resp); Assert.True(resp.StatusCode == HttpStatusCode.BadRequest); }
public void Transfer(Transference transference) { _userRepository.Transfer(transference); }
public static void Main(string[] args) { Login: Console.WriteLine("Please Enter Identification:"); var userIdentification = Console.ReadLine(); Console.WriteLine("Please Enter the access Code:"); var userCode = Console.ReadLine(); var user = UserService.GetUser(userIdentification, userCode); if (user == null) { throw new InvalidCredentialException("Invalid Credentials"); } Reset: Console.Clear(); Console.WriteLine($"Welcome {user.Name}"); Console.WriteLine("Please select one of the options below."); Console.WriteLine("1...............................Deposit"); Console.WriteLine("2...............................Withdraw"); Console.WriteLine("3...............................Transfer"); Console.WriteLine("4...............................Logout"); Console.WriteLine("5...............................Cancel"); var selectedOption = Console.ReadLine(); Console.Clear(); switch (selectedOption) { case "1": { Console.WriteLine("Introduce amount to be deposit:"); var amountToBeDeposit = Convert.ToDouble(Console.ReadLine()); if (amountToBeDeposit > 10000) { throw new DepositAmountExceedException("Deposit max amount has been exceeded"); } UserService.Deposit(user, amountToBeDeposit); break; } case "2": { Console.WriteLine("Introduce amount withdraw:"); var amountToWithdraw = Convert.ToDouble(Console.ReadLine()); if (amountToWithdraw > 20000) { throw new WithdrawAmountExceedException("Withdraw max amount has been exceeded"); } UserService.Withdraw(user, amountToWithdraw); break; } case "3": { Console.WriteLine("Introduce receptor identification number:"); var receptor = Console.ReadLine(); Console.WriteLine("Introduce amount to transfer:"); var amountToTransfer = Convert.ToDouble(Console.ReadLine()); var transference = new Transference { CurrentUser = user, MoneyToBeTransfered = amountToTransfer, Receptor = receptor }; UserService.Transfer(transference); break; } case "4": { goto Login; break; } case "5": { return; break; } default: { Console.WriteLine("Invalid Option"); return; } } Console.Clear(); Console.WriteLine($"Actual Balance: {user.Money}, Name:{user.Name}"); Console.WriteLine("Do you want to realize another action? y/n"); if (Console.ReadLine()?.ToUpper() == "Y") { goto Reset; } Console.ReadKey(); }