예제 #1
0
        public HttpResponseMessage Post([FromBody] NewTransactionRequest transactionRequest, string walletId)
        {
            // Desde el Identity, recupero el Id del usuario
            int userId = TokenService.GetIdClient(User.Identity as ClaimsIdentity);
            // Obtengo el wallet por Id o por nombre de moneda (shortcut)
            var wallet = _walletService.GetWalletByCoinNameOrWalletIdAndUser(walletId, userId);

            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Campos Incorrectos"));
            }
            //Si no se encontro el wallet y el request usa el shortcut de nombre de moneda existente en CMC, creo un nuevo wallet
            if (wallet == null && CoinService.ExisteEnCoinMarketCap(walletId))
            {
                Coin moneda  = _coinService.GetCoinByName(walletId);
                User usuario = _userService.GetUserById(userId);
                wallet = _walletService.AddWallet(moneda, 0, usuario).Result;
            }

            if (wallet == null && walletId.All(c => Char.IsDigit(c)))
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Id de wallet inexistente"));
            }
            if (wallet == null && !walletId.All(c => Char.IsDigit(c)))
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Tipo de moneda inexistente"));
            }

            var type               = transactionRequest.Type;
            var amount             = transactionRequest.Amount;
            var transactionService = _transactionService;

            try
            {
                if (type.ToLower() == "compra")
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, new TransactionViewModel(transactionService.Buy(wallet.Id, amount))));
                }
                else if (type.ToLower() == "venta")
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, new TransactionViewModel(transactionService.Sell(wallet.Id, amount))));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "La transaccion debe ser del tipo \"compra\" o \"venta\""));
                }
            }

            catch (Exception exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, exception.Message));;
            }
        }
예제 #2
0
        public async Task <HttpResponseMessage> NewWallet([FromBody] NewWalletRequest newWalletRequest)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Campos Incorrectos"));
            }
            // Desde el Identity, recupero el Id del usuario
            int userId = TokenService.GetIdClient(User.Identity as ClaimsIdentity);

            if (!CoinService.ExisteEnCoinMarketCap(newWalletRequest.NombreMoneda))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, "La moneda debe existir en CoinMarketCap"));
            }
            if (newWalletRequest.Balance < 0)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, "El balance no puede ser negativo"));
            }
            Coin moneda  = _coinService.GetCoinByName(newWalletRequest.NombreMoneda);
            User usuario = _userService.GetUserById(userId);
            var  wallet  = await _walletService.AddWallet(moneda, newWalletRequest.Balance, usuario);

            return(Request.CreateResponse <WalletViewModel>(HttpStatusCode.Created, await _walletService.GetWalletInfo(wallet)));
        }