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());
        }
Exemplo n.º 2
0
        public IHttpActionResult Create(CreateTransactionBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId = User.Identity.GetUserId();

            var bankAccount = Context
                              .BankAccounts
                              .FirstOrDefault(p => p.Id == model.BankAccountId &&
                                              (p.Household.OwnerId == userId ||
                                               p.Household.Members.Any(t => t.Id == userId)));

            if (bankAccount == null)
            {
                ModelState.AddModelError("",
                                         "Bank account doesn't exist or you don't belong to this household");
                return(BadRequest(ModelState));
            }

            var category = Context
                           .Categories
                           .FirstOrDefault(p => p.Id == model.CategoryId &&
                                           p.HouseholdId == bankAccount.HouseholdId);

            if (category == null)
            {
                ModelState.AddModelError("", "Category doesn't exist in this household");
                return(BadRequest(ModelState));
            }

            var transaction = Mapper.Map <Transaction>(model);

            transaction.OwnerId = userId;

            bankAccount.Balance += transaction.Amount;

            Context.Transactions.Add(transaction);
            Context.SaveChanges();

            var result = Mapper.Map <TransactionViewModel>(transaction);

            return(Ok(result));
        }
Exemplo n.º 3
0
        public IHttpActionResult Create(CreateTransactionBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId = User.Identity.GetUserId();

            var account = _db.Accounts
                          .FirstOrDefault(p => p.Id == model.AccountId);

            if (account == null)
            {
                return(BadRequest("Account doesn't exist"));
            }

            var houseHold = account.HouseHold;

            if (houseHold.CreatorId == userId ||
                houseHold.Members.Any(p => p.Id == userId))
            {
                var transaction = new Transactions();
                transaction.AccountId   = model.AccountId;
                transaction.Description = model.Description;
                transaction.Date        = model.Date;
                transaction.Amount      = model.Amount;
                transaction.CategoryId  = model.CategoryId;
                transaction.IsVoided    = false;
                transaction.EnteredById = userId;

                account.Balance += transaction.Amount;


                _db.Transactions.Add(transaction);
                _db.SaveChanges();

                return(Ok());
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                return(BadRequest("Not authorized"));
            }
        }
Exemplo n.º 4
0
        public IHttpActionResult PostTransactions(CreateTransactionBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var EnteredById = User.Identity.GetUserId();
            //var transaction = db.Transactions.Where(p => p.Id == transactions.Id).FirstOrDefault();
            var account = db.Accounts.Where(p => p.Id == model.AccountId).FirstOrDefault();

            if (account == null)
            {
                return(BadRequest("Account not found"));
            }
            var houseHold = account.HouseHold;

            //if (houseHold.CreatorId == EnteredById ||
            //    houseHold.HouseHoldUser.Any(p => p.Id == EnteredById))
            //{
            var transaction = new Transactions();

            transaction.AccountId   = model.AccountId;
            transaction.Description = model.Description;
            transaction.Date        = model.Date;
            transaction.Amount      = model.Amount;
            transaction.CategoryId  = model.CategoryId;
            transaction.IsVoided    = false;
            transaction.EnteredById = EnteredById;
            account.Balance        += transaction.Amount;
            db.Transactions.Add(transaction);
            db.SaveChanges();
            return(Ok("Success"));
            //}
            //else
            //{
            //    return BadRequest("Not authorized");
            //}
        }