public async Task <IActionResult> rescueMoney(int Id, UserBalanceDTO userBalance) { try { var user = await this._userManager.FindByIdAsync(Id.ToString()); if (user == null) { return(this.StatusCode(StatusCodes.Status404NotFound, "Usuário não encontrado")); } if (user.SavedBalance >= userBalance.Balance && userBalance.Balance > 0) { //==================================================== //Atualiza o saldo guardado user.SavedBalance = user.SavedBalance - userBalance.Balance; //Atualiza o saldo da conta user.Balance += userBalance.Balance; //==================================================== //Cria o movimento para ser adicionado ao extrato var movement = await this._repo.CreateMovement("Resgatar Dinheiro", userBalance.Balance, user.UserName); //Adiciona a movimentação ao extrato user.Extract = new List <Extract>(); user.Extract.Add(movement); //==================================================== //Atualiza no banco de dados var result = await this._userManager.UpdateAsync(user); if (result.Succeeded) { return(Ok()); } return(BadRequest(result.Errors)); } return(this.StatusCode(StatusCodes.Status401Unauthorized, "Valor não Autorizado para Resgatar o Dinheiro Solicitado")); } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Banco de dados falhou")); } }
public async System.Threading.Tasks.Task TestBalanceEdit() { (UsersController usersController, OrdersController ordersController, HttpContext httpContext) = setupControllers(); var result = await usersController.PostUser("testUser"); UserDTO user = result.Value; Console.WriteLine(user); Assert.AreEqual("testUser", user.name); httpContext.Request.Headers["token"] = user.token; await usersController.PutBalance("USD", 50); await usersController.PutBalance("BTC", 2); UserBalanceDTO balance = usersController.GetBalance().Result.Value; Assert.AreEqual(balance.dollars, 50); Assert.AreEqual(balance.bitcoins, 2); Assert.IsTrue(balance.dollar_equivalent > 0); }
public async Task <IActionResult> transfer(int Id, string numAccount, UserBalanceDTO userBalance) { try { var userSender = await this._userManager.FindByIdAsync(Id.ToString()); if (userSender == null) { return(this.StatusCode(StatusCodes.Status404NotFound, "Usuário não encontrado")); } var userReceiver = await this._repo.GetUserAsyncByNumAccount(numAccount); if (userReceiver == null) { return(this.StatusCode(StatusCodes.Status404NotFound, "Usuário não encontrado")); } if (userSender.Balance >= userBalance.Balance && userBalance.Balance > 0) { //==================================================== //Atualiza o saldo da conta de quem envia a transfência userSender.Balance = userSender.Balance - userBalance.Balance; //Atualiza o saldo da conta de quem recebe a transfência userReceiver.Balance += userBalance.Balance; //==================================================== //Cria o movimento para ser adicionado ao extrato de quem envia a transfência var movementSender = await this._repo.CreateMovement("Realizou Transferência", userBalance.Balance, userReceiver.UserName); //Adiciona a movimentação ao extrato de quem envia a transfência userSender.Extract = new List <Extract>(); userSender.Extract.Add(movementSender); //==================================================== //Cria o movimento para ser adicionado ao extrato de quem recebe a transfência var movementReceiver = await this._repo.CreateMovement("Recebeu Transferência", userBalance.Balance, userSender.UserName); //Adiciona a movimentação ao extrato de quem recebe a transfência userReceiver.Extract = new List <Extract>(); userReceiver.Extract.Add(movementReceiver); //==================================================== //Atualiza no banco de dados var resultSender = await this._userManager.UpdateAsync(userSender); if (resultSender.Succeeded) { //Atualiza no banco de dados var resultReceiver = await this._userManager.UpdateAsync(userReceiver); if (resultReceiver.Succeeded) { return(Ok()); } return(BadRequest(resultReceiver.Errors)); } return(BadRequest(resultSender.Errors)); } return(this.StatusCode(StatusCodes.Status401Unauthorized, "Valor não Autorizado para a Transferência Solicitada")); } catch (System.Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Banco de dados falhou")); } }
public async System.Threading.Tasks.Task ComplexTest() { //taken from project definition (UsersController usersController, OrdersController ordersController, HttpContext httpContext) = setupControllers(); //4 users set up balance UserDTO a = (await usersController.PostUser("A")).Value; UserDTO b = (await usersController.PostUser("B")).Value; UserDTO c = (await usersController.PostUser("C")).Value; UserDTO d = (await usersController.PostUser("D")).Value; httpContext.Request.Headers["token"] = a.token; await usersController.PutBalance("BTC", 1); httpContext.Request.Headers["token"] = b.token; await usersController.PutBalance("BTC", 10); httpContext.Request.Headers["token"] = c.token; await usersController.PutBalance("USD", 250000); httpContext.Request.Headers["token"] = d.token; await usersController.PutBalance("USD", 300000); // user a tries to post order, doesn't have enough tokens, gets more tokens and posts order again, this time succesfully httpContext.Request.Headers["token"] = a.token; var ao1 = (await ordersController.PostOrder(10, "SELL", 10000)).Result as BadRequestObjectResult; Assert.AreEqual(400, ao1.StatusCode); Assert.AreEqual("Not enough assets on account for this order", ao1.Value); await usersController.PutBalance("BTC", 10); StandingOrder ao2 = (await ordersController.PostOrder(10, "SELL", 10000)).Value; // B makes a standing order too httpContext.Request.Headers["token"] = b.token; var bo1 = (await ordersController.PostOrder(10, "SELL", 20000)).Value; // C makes a market order - check average price, balance httpContext.Request.Headers["token"] = c.token; MarketOrderDTO cm1 = (await ordersController.PostMarketOrder(15, "BUY")).Value; Assert.AreEqual(13333.33, (double)cm1.avg_price, 0.004); // there is no AreEqual for decimal UserBalanceDTO cb1 = (await usersController.GetBalance()).Value; Assert.AreEqual(15, cb1.bitcoins); Assert.AreEqual(50000, cb1.dollars); //check existing standing orders httpContext.Request.Headers["token"] = a.token; ao2 = (await ordersController.GetOrder(ao2.id)).Value; Assert.AreEqual("FULFILLED", ao2.status); Assert.AreEqual(0, ao2.remainingBitcoinAmount); httpContext.Request.Headers["token"] = b.token; bo1 = (await ordersController.GetOrder(bo1.id)).Value; Assert.AreEqual("LIVE", bo1.status); Assert.AreEqual(5, bo1.remainingBitcoinAmount); // D makes a standing buy order thats cheaper than what B is selling for, and then makes another one but doesn't have enough money remaining httpContext.Request.Headers["token"] = d.token; StandingOrder do1 = (await ordersController.PostOrder(20, "BUY", 10000)).Value; var do2 = (await ordersController.PostOrder(10, "BUY", 25000)).Result as BadRequestObjectResult; Assert.AreEqual(400, do2.StatusCode); Assert.AreEqual("Not enough assets on account for this order", do2.Value); // D then deletes the first order and remakes the second order, which is partially fulfilled do1 = (await ordersController.DeleteOrder(do1.id)).Value; Assert.AreEqual("CANCELLED", do1.status); StandingOrder do3 = (await ordersController.PostOrder(10, "BUY", 25000)).Value; Assert.AreEqual("LIVE", do3.status); Assert.AreEqual(5, do3.remainingBitcoinAmount); // since D bought 5 bitcoin at 20k (older standing order), they should have 100k less than at beginning, and onother 125k reserved their standing order UserBalanceDTO db = (await usersController.GetBalance()).Value; Assert.AreEqual(300000 - 100000 - 125000, db.dollars); Assert.AreEqual(5, db.bitcoins); // now checking balance and standing order status of B httpContext.Request.Headers["token"] = b.token; bo1 = (await ordersController.GetOrder(bo1.id)).Value; Assert.AreEqual("FULFILLED", bo1.status); UserBalanceDTO bb = (await usersController.GetBalance()).Value; Assert.AreEqual(0, bb.bitcoins); Assert.AreEqual(200000, bb.dollars); }