public async Task<IHttpActionResult> Create(Transaction trans)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            trans.Created = DateTimeOffset.Now;

            var account = db.HouseHoldAccounts.Find(trans.HouseHoldAccountId);

            if (trans.isDebit)
            {
                account.Balance = account.Balance - trans.Amount;
            }

            else
            {
                account.Balance = account.Balance + trans.Amount;
            }

            db.Transactions.Add(trans);
            await db.SaveChangesAsync();

            return Ok(trans);
        }
        public async Task<IHttpActionResult> Create(HouseHoldAccount model)
        {
            var user = db.Users.Find(User.Identity.GetUserId());
            
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var accountNameExists = user.HouseHold.HouseHoldAccounts.Any(a => a.Name == model.Name);

            if (accountNameExists)
            {
                return Ok("You already have an account called: " + model.Name + " . Please chose another name.");
            }

            else
            {
                var account = new HouseHoldAccount()
                {
                    Name = model.Name,
                    Balance = model.Balance,
                    HouseHoldId = (int)user.HouseHoldId
                };

                db.HouseHoldAccounts.Add(account);

                var trans = new Transaction()
                {
                    Description = "New Account: " + model.Name + " created.",
                    Amount = model.Balance,
                    HouseHoldAccountId = model.HouseHoldId,
                    CategoryId = 1,
                    Created = DateTimeOffset.Now,

                };

                db.Transactions.Add(trans);

                await db.SaveChangesAsync();

                return Ok(account);
            }
            
        }
        public async Task<IHttpActionResult> Edit(Transaction model)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var oldTrans = db.Transactions.AsNoTracking().FirstOrDefault(t => t.id == model.id);

            if (model.isDebit)
            {
                if (model.Amount > 0)
                    model.Amount *= -1;
            }
            else
                if (model.Amount < 0)
                    model.Amount *= -1;

            //check if amount has changed
            if (oldTrans.Amount != model.Amount)
            {
                var account = db.HouseHoldAccounts.Find(model.HouseHoldAccountId);
                account.Balance -= oldTrans.Amount;
                account.Balance += model.Amount;
            }

            model.Updated = DateTimeOffset.Now;
            
            db.Update<Transaction>(model, "Amount", "Reconcile", "CategoryId", "Description", "isDebit");

            await db.SaveChangesAsync();

            return Ok(model);
        }