/// <summary> /// Adds a collection of companies to the database /// </summary> /// <param name="items">Collection of companies to add</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException" /> internal IEnumerable<int> AddCompanies(IEnumerable<ICompany> items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } List<int> ids = new List<int>(); using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { IEnumerable<Company> companies = items.Select(input => new Company(input)); IEnumerable<Company> enumerable = companies as Company[] ?? companies.ToArray(); foreach (Company company in enumerable) { if (ContainsCompany(company)) { throw new ArgumentException(); } dbContext.Companies.Add(company); } dbContext.SaveChanges(); ids.AddRange(enumerable.Select(company => company.Id)); } return ids; }
/// <summary> /// Adds a company to the database /// </summary> /// <param name="item">Company to add</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException" /> internal int AddCompany(ICompany item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (ContainsCompany(item)) { throw new ArgumentException(); } Company company = new Company(item); using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { dbContext.Companies.Add(company); dbContext.SaveChanges(); // save autogenerated id } return company.Id; }
/// <summary> /// Removes a Person from the database /// </summary> /// <param name="item">Person to add</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException"></exception> /// <exception cref="InvalidOperationException"></exception> internal bool RemovePerson(IPerson item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { Person person = dbContext.Persons.FirstOrDefault(x => x.Id == item.Id); if (person == null) { return false; } dbContext.Persons.Remove(person); dbContext.SaveChanges(); } return true; }
/// <summary> /// Removes a Company from the database /// </summary> /// <param name="item">Company to remove</param> /// <exception cref="ArgumentNullException"/> internal bool RemoveCompany(ICompany item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { Company company = dbContext.Companies.FirstOrDefault(x => x.Id == item.Id); if (company == null) { return false; } dbContext.Companies.Remove(company); dbContext.SaveChanges(); } return true; }
/// <summary> /// Removes a collection of companies to the database /// </summary> /// <param name="items">Collection of Companies to remove</param> /// <exception cref="ArgumentNullException"/> internal bool RemoveCompanies(IEnumerable<ICompany> items) { bool res; if (items == null) { throw new ArgumentNullException(nameof(items)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { res = true; foreach (ICompany company in items) { Company comp = dbContext.Companies.FirstOrDefault(x => x.Id == company.Id); if (comp == null) { res = false; } else { dbContext.Companies.Remove(comp); } } dbContext.SaveChanges(); } return res; }
/// <summary> /// Removes all companies specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Companies fullfilling the function, null if no company found</returns> /// <exception cref="ArgumentNullException"/> internal bool RemoveCompanies(Func<ICompany, bool> func) { bool res; if (func == null) { throw new ArgumentNullException(nameof(func)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { res = false; IEnumerable<ICompany> companies = GetCompanies(func); foreach (ICompany company in companies) { res = true; Company comp = dbContext.Companies.FirstOrDefault(x => x.Id == company.Id); dbContext.Companies.Remove(comp); } dbContext.SaveChanges(); } return res; }
/// <summary> /// Updates the given person /// </summary> /// <param name="item">Person to update</param> /// <returns>True, if person could be updated succesful, false otherwise</returns> internal bool UpdatePerson(IPerson item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (!ContainsPerson(item)) { AddPerson(item); return true; } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { Person nation = dbContext.Persons.FirstOrDefault(x => x.Id == item.Id); dbContext.Entry(nation).CurrentValues.SetValues(item); dbContext.SaveChanges(); } return true; }
/// <summary> /// Updates the given company /// </summary> /// <param name="item">Company to update</param> /// <returns>True, if company could be updated succesful, false otherwise</returns> internal bool UpdateCompany(ICompany item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (!ContainsCompany(item)) { AddCompany(item); return true; } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { Company company = dbContext.Companies.FirstOrDefault(x => x.Id == item.Id); dbContext.Entry(company).CurrentValues.SetValues(item); dbContext.SaveChanges(); } return true; }
/// <summary> /// Gets a person specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Person fullfilling the function, null if no nation found</returns> internal IPerson GetPerson(Func<IPerson, bool> func) { if (func == null) { throw new ArgumentNullException(); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Persons.FirstOrDefault(func); } }
/// <summary> /// Executes the given DDL/DML command against the database. /// </summary> /// <param name="sql">The command string.</param> /// <param name="parameters">The parameters to apply to the command string.</param> /// <returns>The object returned by the database after executing the command.</returns> internal ICompany GetCompany(string sql, params object[] parameters) { using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Companies.SqlQuery(sql, parameters).SingleOrDefault(); } }
/// <summary> /// Gets a company specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Company fullfilling the function, null if no company found</returns> internal ICompany GetCompany(Func<ICompany, bool> func) { if (func == null) { throw new ArgumentNullException(); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Companies.FirstOrDefault(func); } }
/// <summary> /// Executes the given DDL/DML command against the database. /// </summary> /// <param name="sql">The command string.</param> /// <param name="parameters">The parameters to apply to the command string.</param> /// <returns>The objects returned by the database after executing the command.</returns> internal IEnumerable<ICompany> GetCompanies(string sql, params object[] parameters) { using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Companies.SqlQuery(sql, parameters).ToList(); } }
/// <summary> /// Gets all companies specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Companies fullfilling the function, null if no company found</returns> internal IEnumerable<ICompany> GetCompanies(Func<ICompany, bool> func) { if (func == null) { throw new ArgumentNullException(); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Companies.Where(func).ToList(); } }
/// <summary> /// Executes the given DDL/DML command against the database. /// </summary> /// <param name="sql">The command string.</param> /// <param name="parameters">The parameters to apply to the command string.</param> /// <returns>The result returned by the database after executing the command.</returns> internal int ExecuteSqlCommand(string sql, params object[] parameters) { using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Database.ExecuteSqlCommand(sql, parameters); } }
/// <summary> /// Removes all persons specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Persons fullfilling the function, null if no person found</returns> /// <exception cref="ArgumentNullException"/> internal bool RemovePersons(Func<IPerson, bool> func) { bool res; if (func == null) { throw new ArgumentNullException(nameof(func)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { res = false; IEnumerable<IPerson> persons = GetPersons(func); foreach (IPerson person in persons) { res = true; Person per = dbContext.Persons.FirstOrDefault(x => x.Id == person.Id); dbContext.Persons.Remove(per); } dbContext.SaveChanges(); } return res; }
/// <summary> /// Removes a collection of persons to the database /// </summary> /// <param name="items">Collection of persons to remove</param> /// <exception cref="ArgumentNullException"/> internal bool RemovePersons(IEnumerable<IPerson> items) { bool res; if (items == null) { throw new ArgumentNullException(nameof(items)); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { res = true; foreach (IPerson person in items) { Person per = dbContext.Persons.FirstOrDefault(x => x.Id == person.Id); if (per == null) { res = false; } else { dbContext.Persons.Remove(per); } } dbContext.SaveChanges(); } return res; }
/// <summary> /// Executes the given DDL/DML command against the database. /// </summary> /// <param name="sql">The command string.</param> /// <param name="parameters">The parameters to apply to the command string.</param> /// <returns>The object returned by the database after executing the command.</returns> internal IPerson GetPerson(string sql, params object[] parameters) { using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Persons.SqlQuery(sql, parameters).SingleOrDefault(); } }
/// <summary> /// Gets all persons specified by the function (Linq-Expression) /// </summary> /// <param name="func"></param> /// <returns>Persons fullfilling the function, null if no person found</returns> internal IEnumerable<IPerson> GetPersons(Func<IPerson, bool> func) { if (func == null) { throw new ArgumentNullException(); } using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Persons.Where(func).ToList(); } }
/// <summary> /// Executes the given DDL/DML command against the database. /// </summary> /// <param name="sql">The command string.</param> /// <param name="parameters">The parameters to apply to the command string.</param> /// <returns>The objects returned by the database after executing the command.</returns> internal IEnumerable<IPerson> GetPersons(string sql, params object[] parameters) { using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { return dbContext.Persons.SqlQuery(sql, parameters).ToList(); } }
/// <summary> /// Adds a person to the database /// </summary> /// <param name="item">Person to add</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException" /> internal int AddPerson(IPerson item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (ContainsPerson(item)) { throw new ArgumentException(); } Person person = new Person(item); using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { dbContext.Persons.Add(person); dbContext.SaveChanges(); // save autogenerated id } return person.Id; }
/// <summary> /// Adds a collection of persons to the database /// </summary> /// <param name="items">Collection of persons to add</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException" /> internal IEnumerable<int> AddPersons(IEnumerable<IPerson> items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } List<int> ids = new List<int>(); using (OwlCalcDatabase dbContext = new OwlCalcDatabase()) { IEnumerable<Person> persons = items.Select(input => new Person(input)); IEnumerable<Person> enumerable = persons as Person[] ?? persons.ToArray(); foreach (Person person in enumerable) { if (ContainsPerson(person)) { throw new ArgumentException(); } dbContext.Persons.Add(person); } dbContext.SaveChanges(); ids.AddRange(enumerable.Select(person => person.Id)); } return ids; }