Esempio n. 1
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            People people = new People()
            {
                IdentityId = new Guid(user.Id),
                Name       = model.Name,
                LastName   = model.LasName,
                MiddleName = model.MiddleName,
                Birthday   = model.Birthday,
                Email      = model.Email
            };

            db.Balances.Add(InitBalance(people));
            db.Peoples.Add(people);
            db.SaveChanges();

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }
        public IHttpActionResult PostTransaction(CreateTransactionBindingModel transaction)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            People people = CurrentPeople();

            if (people.Balance < transaction.Amount)
            {
                return(InternalServerError(new Exception("You don't have enough PW")));
            }

            Transaction newTransaction = new Transaction()
            {
                Correspondent = db.Peoples.Find(transaction.RecepientID),
                People        = people,
                Amount        = transaction.Amount,
                DateTime      = DateTime.Now,
                Type          = TransactionType.Debet
            };

            //User balance
            db.Balances.Add(
                new Balance(newTransaction.People,
                            newTransaction.People.Balance - newTransaction.Amount,
                            "User transaction",
                            newTransaction));

            //Correspondent balance
            db.Balances.Add(
                new Balance(newTransaction.Correspondent,
                            newTransaction.Correspondent.Balance + newTransaction.Amount,
                            "Correspondent transaction",
                            newTransaction));

            db.Entry(newTransaction.Correspondent).State = EntityState.Unchanged;
            db.Entry(newTransaction.People).State        = EntityState.Unchanged;

            db.Transactions.Add(newTransaction);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                return(InternalServerError());
            }

            return(Ok());
        }
        public AccountTransaction Add(Account payee, Account recipient, decimal amount)
        {
            AccountTransaction accountTransaction = new AccountTransaction();

            accountTransaction.PayeeId           = payee.Id;
            accountTransaction.RecipientId       = recipient.Id;
            accountTransaction.PayeeBalance      = payee.Balance;
            accountTransaction.RecipientBalance  = recipient.Balance;
            accountTransaction.TimeOfTransaction = DateTime.Now;
            accountTransaction.Amount            = amount;

            db.AccountTransactions.Add(accountTransaction);
            db.SaveChanges();
            return(accountTransaction);
        }
Esempio n. 4
0
 public virtual void Commit() => _context.SaveChanges();
Esempio n. 5
0
        public (AccountTransaction, string) CreateTransaction(int payeeUserId, int recipientUserId, decimal amount)
        {
            if (amount <= 0)
            {
                return(null, "Transaction is not succeed: transaction amount must be greater than zero.");
            }
            Account recipient = GetAccountOfUser(recipientUserId);

            if (recipient == null)
            {
                return(null, "Transaction is not succeed: Recipient does not exist.");
            }
            Account payee = GetAccountOfUser(payeeUserId);

            if ((payee.Id != GetSystemAccount().Id) && (payee.Balance < amount))
            {
                return(null, "Transaction is not succeed: transaction amount is greater than the current balance.");
            }
            //1. вариант без транзакций---------------------------

            recipient.Balance = recipient.Balance + amount;
            payee.Balance     = payee.Balance - amount;
            db.SaveChanges();
            AccountTransaction accountTransaction = _accountTransactionService.Add(payee, recipient, amount);

            //-----------------------------------
            //2. вариант с хранимой процедурой----------------------

            /* using (SqlConnection connection = new SqlConnection(""))//ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString))
             * CREATE PROCEDURE AddTransaction
             * @payeeUserId int,
             * @recipientUserId int,
             * @amount float,
             * @TransactionId int out
             * AS
             * DECLARE  @payeeBalance int,
             * @recipientBalance int,
             * @payeeAccountId int,
             * @recipientAccountId int
             *
             * BEGIN TRANSACTION
             * select @payeeBalance=Balance-@amount,
             * @payeeAccountId=Id
             * from [dbo].[Accounts]
             * where  [dbo].[Accounts].UserId=@payeeUserId
             *
             * select @recipientBalance=Balance+@amount,
             * @recipientAccountId=Id
             * from [dbo].[Accounts]
             * where  [dbo].[Accounts].UserId=@recipientUserId
             *
             *
             * Update [dbo].[Accounts] Set Balance=Balance-@amount WHERE  [dbo].[Accounts].UserId=@payeeUserId
             * IF (@@error <> 0) ROLLBACK
             * Update [dbo].[Accounts] Set Balance=Balance+@amount WHERE  [dbo].[Accounts].UserId=@recipientUserId
             * IF (@@error <> 0) ROLLBACK
             * Insert INTO AccountTransactions
             * ([Amount]
             * ,[PayeeBalance]
             * ,[PayeeId]
             * ,[RecipientBalance]
             * ,[RecipientId]
             * ,[TimeOfTransaction])
             * Values (@amount, @payeeBalance, @payeeAccountId, @recipientBalance, @recipientAccountId, GETDATE())
             * IF (@@error <> 0) ROLLBACK
             * COMMIT
             *
             *
             * GO
             *
             */

            /* 3. вариант с CreateCommand()
             *
             * using (SqlConnection connection = new SqlConnection(""))//ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString))
             * {
             *    connection.Open();
             *    SqlTransaction transaction = connection.BeginTransaction();
             *
             *    SqlCommand command = connection.CreateCommand();
             *    command.Transaction = transaction;
             *    try
             *    {
             *        command.CommandText = $"Update Accounts Set Balance=Balance-{amount} where Accounts.UserId={payeeId}";
             *        command.ExecuteNonQuery();
             *        command.CommandText = $"Update Accounts Set Balance=Balance+{amount} where Accounts.UserId={recipientId}";
             *        command.ExecuteNonQuery();
             *        transaction.Commit();
             *    }
             *    catch (Exception ex)
             *    {
             *        transaction.Rollback();
             *        OnNewTransaction(new AccountEventArgs(recipientId, $"Your transaction is not complited. " + ex.Message));
             *        return;
             *    }
             * } */

            return(accountTransaction, "Transaction is complited.");
        }
Esempio n. 6
0
 public void SaveChanges()
 {
     _context.SaveChanges();
 }