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());
        }
Example #2
0
        public virtual void Update(T entity)
        {
            var local = _context.Set <T>()
                        .Local
                        .FirstOrDefault(entry => entry.Id.Equals(entity.Id));

            if (local != null)
            {
                _context.Entry(local).State = EntityState.Detached;
            }

            EntityEntry dbEntityEntry = _context.Entry <T>(entity);

            dbEntityEntry.State = EntityState.Modified;
        }