Exemplo n.º 1
0
        /// <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;
        }
Exemplo n.º 2
0
 /// <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;
 }
Exemplo n.º 3
0
 /// <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;
 }
Exemplo n.º 4
0
 /// <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;
 }
Exemplo n.º 5
0
 /// <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;
 }
Exemplo n.º 6
0
 /// <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;
 }
Exemplo n.º 7
0
 /// <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;
 }
Exemplo n.º 8
0
 /// <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;
 }
Exemplo n.º 9
0
 /// <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);
     }
 }
Exemplo n.º 10
0
 /// <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();
     }
 }
Exemplo n.º 11
0
 /// <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);
     }
 }
Exemplo n.º 12
0
 /// <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();
     }
 }
Exemplo n.º 13
0
 /// <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();
     }
 }
Exemplo n.º 14
0
 /// <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);
     }
 }
Exemplo n.º 15
0
 /// <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;
 }
Exemplo n.º 16
0
 /// <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;
 }
Exemplo n.º 17
0
 /// <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();
     }
 }
Exemplo n.º 18
0
 /// <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();
     }
 }
Exemplo n.º 19
0
 /// <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();
     }
 }
Exemplo n.º 20
0
 /// <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;
 }
Exemplo n.º 21
0
        /// <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;
        }