public static void AddAccount(AccountDTO dto) { using (var db = new WebDBEntities()) { var account = new Account { AccountCode = dto.AccountCode, CustomerId = dto.CustomerId }; db.Accounts.Add(account); db.SaveChanges(); } }
public static void AddCustomer(CustomerDTO dto) { using (var db = new WebDBEntities()) { var customer = new Customer { Name = dto.Name, Surname = dto.Surname, Phone = dto.Phone, Email = dto.Email }; db.Customers.Add(customer); db.SaveChanges(); } }
public static int DeleteAccount(int id) { int customerId; using (var db = new WebDBEntities()) { var account = db.Accounts.First(r => r.Id == id); customerId = account.CustomerId; db.Transactions.RemoveRange(db.Transactions.Where(r => r.AccountId == account.Id)); db.Accounts.Remove(account); db.SaveChanges(); } return customerId; }
public static int AddTransaction(TransactionDTO dto) { int customerId; using (var db = new WebDBEntities()) { var transaction = new Transaction() { CreateDate = dto.CreateDate, CallDuration = dto.CallDuration, AccountId = dto.AccountId }; db.Transactions.Add(transaction); db.SaveChanges(); customerId = db.Accounts.First(r => r.Id == dto.AccountId).CustomerId; } return customerId; }
public static void DeleteCustomer(int id) { using (var db = new WebDBEntities()) { var accounts = db.Accounts.Where(r => r.CustomerId == id); foreach (var acc in accounts) { db.Transactions.RemoveRange(db.Transactions.Where(r => r.AccountId == acc.Id)); } db.Accounts.RemoveRange(db.Accounts.Where(r => r.CustomerId == id)); db.Customers.RemoveRange(db.Customers.Where(r => r.Id == id)); db.SaveChanges(); } }
public static int UpdateTransfer(TransactionDTO dto) { using (var db = new WebDBEntities()) { var transaction = db.Transactions.FirstOrDefault(r => r.Id == dto.Id) ?? new Transaction { Id = dto.Id }; transaction.AccountId = dto.AccountId; transaction.CallDuration = dto.CallDuration; transaction.CreateDate = dto.CreateDate; db.Transactions.AddOrUpdate(transaction); db.SaveChanges(); return db.Accounts.First(r => r.Id == dto.AccountId).CustomerId; } }
public static void UpdateCustomer(CustomerDTO customerDto) { using (var db = new WebDBEntities()) { var customer = db.Customers.FirstOrDefault(r => r.Id == customerDto.Id) ?? new Customer { Id = customerDto.Id }; customer.Name = customerDto.Name; customer.Surname = customerDto.Surname; customer.Email = customerDto.Email; customer.Phone = customerDto.Phone; db.Customers.AddOrUpdate(customer); db.SaveChanges(); } }
public static List<TransactionDTO> GetTransactions(int accountId) { using (var db = new WebDBEntities()) { return db.Transactions.Where(r => r.AccountId == accountId).Select(t => new TransactionDTO { Id = t.Id, AccountId = t.AccountId, CreateDate = t.CreateDate, CallDuration = t.CallDuration }).ToList(); } }
public static TransactionDTO GetTransaction(int id) { using (var db = new WebDBEntities()) { var transaction = db.Transactions.First(r => r.Id == id); var dto = new TransactionDTO { Id = transaction.Id, CallDuration = transaction.CallDuration, CreateDate = transaction.CreateDate, AccountId = transaction.AccountId }; return dto; } }
public static List<CustomerDTO> GetCustomers() { using (var db = new WebDBEntities()) { return db.Customers.Select(customer => new CustomerDTO { Id = customer.Id, Name = customer.Name, Surname = customer.Surname, Phone = customer.Phone, Email = customer.Email }).ToList(); } }
public static CustomerDTO GetCustomer(int id) { using (var db = new WebDBEntities()) { var customer = db.Customers.FirstOrDefault(r => r.Id == id); if (customer == null) throw new ArgumentException("Неверное значение параметра", nameof(id)); var customerDTO = new CustomerDTO { Id = customer.Id, Name = customer.Name, Surname = customer.Surname, Phone = customer.Phone, Email = customer.Email }; return customerDTO; } }
public static List<AccountDTO> GetAllAccounts() { var result = new List<AccountDTO>(); using (var db = new WebDBEntities()) { result.AddRange(db.Accounts.Select(a => new AccountDTO { Id = a.Id, AccountCode = a.AccountCode, CustomerId = a.CustomerId, }).ToList()); } return result; }
public static List<AccountDTO> GetAccountsWithTransaction(int customerId) { using (var db = new WebDBEntities()) { return db.Accounts.Where(r => r.CustomerId == customerId).Select(a => new AccountDTO { Id = a.Id, AccountCode = a.AccountCode, CustomerId = a.CustomerId, Transactions = a.Transactions.Select(t => new TransactionDTO { Id = t.Id, AccountId = t.AccountId, CreateDate = t.CreateDate, CallDuration = t.CallDuration }).ToList() }).ToList(); } }
public static List<AccountDTO> GetAccounts(IEnumerable<int> accountIds) { var result = new List<AccountDTO>(); using (var db = new WebDBEntities()) { foreach (var aid in accountIds) { result.AddRange(db.Accounts.Where(r => r.Id == aid).Select(a => new AccountDTO { Id = a.Id, AccountCode = a.AccountCode, CustomerId = a.CustomerId, Transactions = a.Transactions.Select(t => new TransactionDTO { Id = t.Id, AccountId = t.AccountId, CreateDate = t.CreateDate, CallDuration = t.CallDuration }).ToList() }).ToList()); } } return result; }
public static List<AccountDTO> GetAccounts(int customerId) { using (var db = new WebDBEntities()) { return db.Accounts.Where(r => r.CustomerId == customerId).Select(a => new AccountDTO { Id = a.Id, AccountCode = a.AccountCode, CustomerId = a.CustomerId }).ToList(); } }
public static object DeleteTransfer(int id) { int customerId; using (var db = new WebDBEntities()) { var transaction = db.Transactions.First(r => r.Id == id); var account = db.Accounts.First(r => r.Id == transaction.AccountId); customerId = account.CustomerId; db.Transactions.Remove(transaction); db.SaveChanges(); } return customerId; }