public IEnumerable <Account> GetAccountsByPerson(Person person) { IEnumerable <EntityAccount> entityAccounts; var accounts = new List <Account>(); try { using (var accountContext = new AccountContext()) { entityAccounts = accountContext.Accounts.Where(a => a.Owner.Id == person.Id); } } catch { return(null); } foreach (var ea in entityAccounts) { accounts.Add(new Account(ea.Id, ea.CardType, ea.Owner, ea.Balance, ea.Bonuses)); } return(accounts); }
public void AddAccount(DalAccount account) { using (AccountContext db = new AccountContext()) { db.Accounts.Add(account); db.SaveChanges(); } }
public DalAccount GetAccount(int id) { using (AccountContext db = new AccountContext()) { DalAccount account = db.Accounts.Find(id); return(account); } }
public List <DalAccount> GetAllAccounts() { using (AccountContext db = new AccountContext()) { List <DalAccount> accounts = db.Accounts.ToList <DalAccount>(); return(accounts); } }
public void RemoveAccount(DalAccount account) { using (AccountContext db = new AccountContext()) { db.Accounts.Remove(account); db.SaveChanges(); } }
/// <inheritdoc/> public IAccountDto GetRecord(Guid id) { using (var db = new AccountContext()) { var record = db.AccountSet.Find(id); if (record is null) { throw new ArgumentException($"No such ID: '{id}'."); } return(record); } }
public void UpdateAccount(DalAccount account) { using (AccountContext db = new AccountContext()) { DalAccount dbAccount = db.Accounts.Find(account.Id); foreach (var prop in account.GetType().GetProperties()) { prop.SetValue(dbAccount, (prop.GetValue(account))); } db.SaveChanges(); } }
/// <inheritdoc/> public void DeleteRecord(Guid id) { using (var db = new AccountContext()) { var record = db.AccountSet.Find(id); if (record is null) { throw new ArgumentException($"No such ID: '{id}'."); } db.AccountSet.Remove(record); db.SaveChanges(); } }
/// <inheritdoc/> public IEnumerable <IAccountDto> GetAllRecords() { using (var db = new AccountContext()) { if (!db.AccountSet.Any()) { yield break; } foreach (var record in db.AccountSet) { yield return(record); } } }
public int GetAccountsCount() { int count; try { using (var accountContext = new AccountContext()) { count = accountContext.Accounts.Count(); } } catch { return(-1); } return(count); }
public bool RemoveById(int id) { try { using (var accountContext = new AccountContext()) { var entityAccount = accountContext.Accounts.First(a => a.Id == id); accountContext.Accounts.Remove(entityAccount); accountContext.SaveChanges(); } } catch { return(false); } return(true); }
public bool Update(Account account) { try { using (var accountContext = new AccountContext()) { var entityAccount = accountContext.Accounts.First(a => a.Id == account.Id); entityAccount.Balance = account.GetBalance(); entityAccount.Bonuses = account.GetBonuses(); accountContext.SaveChanges(); } } catch { return(false); } return(true); }
public Account GetAccountById(int id) { EntityAccount ea; try { using (var accountContext = new AccountContext()) { ea = accountContext.Accounts.First(a => a.Id == id); accountContext.Accounts.Remove(ea); accountContext.SaveChanges(); } } catch { return(null); } return(new Account(ea.Id, ea.CardType, ea.Owner, ea.Balance, ea.Bonuses)); }
/// <inheritdoc/> public void AddRecord(IAccountDto dto) { if (dto is null) { throw new ArgumentNullException(nameof(dto)); } var record = new AccountRecord() { Id = dto.Id, Type = dto.Type, Owner = dto.Owner, Balance = dto.Balance, Bonus = dto.Bonus, }; using (var db = new AccountContext()) { db.AccountSet.Add(record); db.SaveChanges(); } }
public int OpenAccount(AccountType accountType, Person owner) { int id; try { using (var accountContext = new AccountContext()) { id = accountContext.Accounts.Count(); accountContext.Accounts .Add(new EntityAccount { Id = id, CardType = accountType, Balance = 0, Bonuses = 0, Owner = owner }); accountContext.SaveChanges(); } } catch { return(-1); } return(id); }
/// <inheritdoc/> public void UpdateRecord(IAccountDto dto) { if (dto is null) { throw new ArgumentNullException(nameof(dto)); } using (var db = new AccountContext()) { var record = db.AccountSet.Find(dto.Id); if (record is null) { throw new ArgumentException($"No such ID: '{dto.Id}'."); } record.Type = dto.Type; record.Owner = dto.Owner; record.Balance = dto.Balance; record.Bonus = dto.Bonus; db.SaveChanges(); } }
/// <summary> /// Initializes a new instance of the <see cref="EFRepository"/> class. /// </summary> /// <param name="context"></param> public EFRepository(AccountContext context) { this.context = context ?? throw new ArgumentNullException(nameof(context)); }