Esempio n. 1
0
        public bool Add(string connectionString, ApiModels.Account account)
        {
            if (account != null)
            {
                var transactionsRepo = new TransactionsRepository();
                var transactions     = transactionsRepo.GetEntities(connectionString, account.Id);

                using (var context = new AccountsDatabase(connectionString))
                {
                    if (account.Id == null)
                    {
                        context.Accounts.Add(new Entities.Account()
                        {
                            Amount       = account.Amount,
                            Name         = account.Name,
                            Number       = account.Number,
                            Id           = Guid.NewGuid(),
                            Transactions = transactions
                        });
                    }
                    else
                    {
                        context.Accounts.Single(a => a.Id == account.Id).Name = account.Name;
                    }

                    context.SaveChanges();
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
 public Entities.Account GetEntity(string connectionString, Guid id)
 {
     using (var context = new AccountsDatabase(connectionString))
     {
         return(context.Accounts.AsNoTracking().Single(a => a.Id == id));
     }
 }
Esempio n. 3
0
 public List <Entities.Transaction> GetEntities(string connectionString, Guid id)
 {
     using (var context = new AccountsDatabase(connectionString))
     {
         return(context.Transactions.AsNoTracking().Where(t => t.AccountId == id).ToList());
     }
 }
Esempio n. 4
0
        public bool Add(string connectionString, ApiModels.Transaction transaction, Guid id)
        {
            if (transaction != null)
            {
                using (var context = new AccountsDatabase(connectionString))
                {
                    var accountsRepo = new AccountsRepository();
                    var account      = accountsRepo.GetAccount(connectionString, id);
                    account.Amount += transaction.Amount;
                    if (!accountsRepo.Update(connectionString, account))
                    {
                        return(false);
                    }

                    context.Transactions.Add(new Entities.Transaction()
                    {
                        Amount    = transaction.Amount,
                        Date      = transaction.Date,
                        AccountId = account.Id,
                        Id        = Guid.NewGuid()
                    });

                    context.SaveChanges();

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
 public virtual IList <T> GetEntities(string connectionString, Guid id)
 {
     using (var context = new AccountsDatabase(connectionString))
     {
         //TODO: how to do this
         return(context.Transactions.AsNoTracking().Where(t => t.AccountId == id).ToList());
     }
 }
Esempio n. 6
0
 public bool Delete(string connectionString, Guid id)
 {
     using (var context = new AccountsDatabase(connectionString))
     {
         var entity = context.Accounts.Single(a => a.Id == id);
         context.Accounts.Remove(entity);
         context.SaveChanges();
         return(true);
     }
 }
Esempio n. 7
0
        public bool Update(string connectionString, ApiModels.Account account)
        {
            if (account != null)
            {
                using (var context = new AccountsDatabase(connectionString))
                {
                    var entity = context.Accounts.Single(a => a.Id == account.Id);
                    entity.Name = account.Name;

                    context.SaveChanges();
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 8
0
        public List <ApiModels.Account> GetAccounts(string connectionString)
        {
            using (var context = new AccountsDatabase(connectionString))
            {
                var accounts = new List <Entities.Account>();
                accounts = context.Accounts.AsNoTracking().ToList();

                if (accounts != null)
                {
                    var accountsDisplay = new List <ApiModels.Account>();
                    accounts.ForEach(a => {
                        accountsDisplay.Add(new ApiModels.Account()
                        {
                            Amount = (float)a.Amount,
                            Name   = a.Name,
                            Number = a.Number,
                            Id     = a.Id
                        });
                    });
                    return(accountsDisplay);
                }
                return(null);
            }
        }