示例#1
0
        public IHttpActionResult Post([FromBody] TransferRequest model)
        {
            GeneralResponse oResponse        = new GeneralResponse();
            AccountManager  oAccountManager  = new AccountManager();
            TransferManager oTransferManager = new TransferManager();
            FieldsService   oFieldsService   = new FieldsService();

            var claims = ClaimsPrincipal.Current.Identities.First().Claims.ToList();

            string UserId = claims?.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Sid, StringComparison.OrdinalIgnoreCase))?.Value;

            try
            {
                Dictionary <string, string> oErrors = oFieldsService.ValidateModel(ModelState);
                if (oErrors.Count != 0)
                {
                    oResponse.Success = 0;
                    oResponse.Message = "Error, campos invalidos";
                    oResponse.Data    = oErrors;

                    return(Content(HttpStatusCode.BadRequest, oResponse));
                }


                Account oDebitAccount = new Account();

                oDebitAccount = oAccountManager.GetAccountById(model.DebitAccountId);

                if (oDebitAccount.AccountId == null)
                {
                    throw new ArgumentException("Cuenta de debito no existente");
                }
                ;
                if (Convert.ToString(oDebitAccount.User.UserId) != UserId)
                {
                    throw new ArgumentException("No se puede transferir dinero desde una cuenta que no pertenece al usuario logeado");
                }
                ;
                if (oDebitAccount.Balance < model.Amount)
                {
                    throw new ArgumentException("No tiene el saldo suficiente para realizar esta transferencia");
                }
                ;

                Account oCreditAccount = new Account();
                oCreditAccount = oAccountManager.GetAccountById(model.CreditAccountId);

                if (oCreditAccount.AccountId == null)
                {
                    throw new ArgumentException("Cuenta de credito no existente");
                }
                ;

                oTransferManager.MakeTransfer(oDebitAccount, model.CreditAccountId, model.Amount, model.Concept);
                oAccountManager.UpdateAccountBalance((int)oDebitAccount.AccountId, -model.Amount);
                oAccountManager.UpdateAccountBalance(model.CreditAccountId, model.Amount);
                oResponse.Success = 1;
                oResponse.Message = "Exito - Transferencia realizada";

                return(Content(HttpStatusCode.OK, oResponse));
            } catch (ArgumentException ex)
            {
                oResponse.Success = 0;
                oResponse.Message = "Error - " + ex.Message;
                return(Content(HttpStatusCode.BadRequest, oResponse));
            } catch (Exception ex)
            {
                oResponse.Success = 0;
                oResponse.Message = ex.Message;
                return(Content(HttpStatusCode.BadRequest, oResponse));
            }
        }