public HttpResponseMessage MakeTransfer(InputTransactionLogDto transaction)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
            () =>
            {
                var db = new BankContext();
                var user = this.ValidateAndGetLoggedUser(db);

                var fromAccount = db.Accounts.FirstOrDefault(a => a.Iban == transaction.FromAccountIban);
                if (fromAccount == null)
                {
                    throw new ArgumentException(
                        string.Format("Account with id = {0} doesn't exist.", transaction.FromAccountIban));
                }

                if (fromAccount.User.Id != user.Id)
                {
                    throw new InvalidOperationException(
                        string.Format(
                        "Current user has id = {0} but the account belongs to user with id = {1}.",
                        user.Id,
                        fromAccount.User.Id));
                }

                Currency currency = null;
                if (transaction.CurrencyId.HasValue)
                {
                    currency = db.Currencies.FirstOrDefault(c => c.Id == transaction.CurrencyId.Value);
                    if (currency == null)
                    {
                        throw new ArgumentException("No such currency.");
                    }
                }
                else
                {
                    currency = fromAccount.Currency;
                }

                Account toAccount = null;
                string toIban = null;
                string description = null;
                if (transaction.ToAccountIban != "")
                {
                    toAccount = db.Accounts.FirstOrDefault(a => a.Iban == transaction.ToAccountIban);
                    if (toAccount == null)
                    {
                        throw new ArgumentException("No such destination account.");
                    }

                    if (toAccount.User.Id != user.Id)
                    {
                        throw new InvalidOperationException(
                            string.Format(
                            "Current user has id = {0} but the destination account belongs to user with id = {1}.",
                            user.Id,
                            toAccount.User.Id));
                    }
                    description = "Transfer between user's accounts.";
                    toIban = toAccount.Iban;

                    toAccount.Balance += transaction.Amount;
                    db.Entry(toAccount).State = EntityState.Modified;
                }

                fromAccount.Balance -= transaction.Amount;
                db.Entry(fromAccount).State = EntityState.Modified;

                db.SaveChanges();

                if (toAccount == null)
                {
                    description = "Transfer to an external account.";
                    toIban = transaction.toIban;
                }

                db.TransactionLogs.Add(new TransactionLog
                {
                    Amount = transaction.Amount,
                    Currency = currency,
                    Timestamp = DateTime.Now,
                    FromAccount = fromAccount,
                    ToIban = toIban,
                    Description = description
                });

                db.SaveChanges();

                var response = new HttpResponseMessage(HttpStatusCode.NoContent);
                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage UpdateUser(InputUserDto value)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
            () =>
            {
                var db = new BankContext();
                var user = this.ValidateAndGetLoggedUser(db);

                if (!ModelState.IsValid)
                {
                    throw new InvalidOperationException("Invalid model state.");
                }

                Role role = null;
                if (value.RoleId.HasValue)
                {
                    role = db.Roles.FirstOrDefault(r => r.Id == value.RoleId);
                    if (role == null)
                    {
                        throw new ArgumentException("No such role.");
                    }
                }

                user.UpdateWith(new User
                {
                    FirstName = value.FirstName,
                    LastName = value.LastName,
                    Role = role
                });

                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                var response = new HttpResponseMessage(HttpStatusCode.NoContent);
                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage UpdateAccount(int id, InputAccountDto value)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
            () =>
            {
                var db = new BankContext();
                var user = this.ValidateAndGetLoggedUser(db);

                if (!ModelState.IsValid)
                {
                    throw new InvalidOperationException("Invalid model state.");
                }

                var accountToUpdate = db.Accounts.FirstOrDefault(a => a.Id == id);
                if (accountToUpdate == null)
                {
                    throw new ArgumentException(
                        string.Format("Account with id = {0} doesn't exist.", id));
                }

                //if (accountToUpdate.User.Id != user.Id)
                //{
                //    throw new InvalidOperationException(
                //        string.Format(
                //        "Current user has id = {0} but the account belongs to user with id = {1}.",
                //        user.Id,
                //        accountToUpdate.User.Id));
                //}

                decimal interestRate = value.InterestRate ?? 0.0M;

                Currency currency = null;
                if (value.CurrencyId.HasValue)
                {
                    currency = db.Currencies.FirstOrDefault(c => c.Id == value.CurrencyId);
                    if (currency == null)
                    {
                        throw new ArgumentException("No such currency.");
                    }
                }

                AccountType type = null;
                if (value.TypeId.HasValue)
                {
                    type = db.AccountTypes.FirstOrDefault(at => at.Id == value.TypeId);
                    if (type == null)
                    {
                        throw new ArgumentException("No such account type.");
                    }
                }

                accountToUpdate.UpdateWith(new Account
                {
                    InterestRate = interestRate,
                    Description = value.Description,
                    Currency = currency,
                    Type = type
                });

                db.Entry(accountToUpdate).State = EntityState.Modified;
                db.SaveChanges();

                var response = new HttpResponseMessage(HttpStatusCode.NoContent);
                return response;
            });

            return responseMsg;
        }