public void SectorsToDB()
        {
            List <Sector> existingSectors = GetAllSectors();
            List <Sector> newSectors      = new List <Sector>();
            StreamReader  sr = new StreamReader(@".../.../Sectors.txt");

            if (!File.Exists(@".../.../Sectors.txt"))
            {
                File.Create(@".../.../Sectors.txt").Close();
            }
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                Sector s = new Sector();
                s.SectorName = line;
                newSectors.Add(s);
            }
            var sectorsToAdd = newSectors.Where(s => existingSectors.All(s2 => s2.SectorName != s.SectorName));

            using (Task_1Entities context = new Task_1Entities())
            {
                foreach (var sec in sectorsToAdd)
                {
                    context.Sectors.Add(sec);
                    string input = (DateTime.Now + " / Added new location with address " + sec.SectorName + " directly to database");
                    archive.WriteToFile(input);
                    context.SaveChanges();
                }
            }
        }
 /// <summary>
 /// Adds and employee to the database
 /// </summary>
 /// <param name="employee">Employee to be added to the database</param>
 /// <returns>An employee that was added to the database</returns>
 public Employee AddEmployee(Employee employee)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             Employee newEmployee = new Employee();
             newEmployee.FirstName   = employee.FirstName;
             newEmployee.LastName    = employee.LastName;
             newEmployee.DateOfBirth = employee.DateOfBirth;
             newEmployee.IDNumber    = employee.IDNumber;
             newEmployee.JMBG        = employee.JMBG;
             newEmployee.Gender      = employee.Gender;
             newEmployee.PhoneNumber = employee.PhoneNumber;
             newEmployee.SectorID    = employee.SectorID;
             newEmployee.Location    = employee.Location;
             newEmployee.Manager     = employee.Manager;
             context.Employees.Add(newEmployee);
             context.SaveChanges();
             string input = (DateTime.Now + " / Added new Employee named " + newEmployee.FirstName + " " + newEmployee.LastName + " to sector " + newEmployee.Sector.SectorName);
             archive.WriteToFile(input);
             return(newEmployee);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         return(null);
     }
 }
        public void LocationsToDB()
        {
            List <Location> existingLocations = GetAllLocations();
            List <Location> newLocations      = new List <Location>();
            StreamReader    sr = new StreamReader(@".../.../Locations.txt");

            if (!File.Exists(@".../.../Locations.txt"))
            {
                File.Create(@".../.../Locations.txt").Close();
            }
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                Location l = new Location();
                l.Address = line;
                newLocations.Add(l);
            }
            var locationsToAdd = newLocations.Where(l => existingLocations.All(l2 => l2.Address != l.Address));

            locationsToAdd = locationsToAdd.OrderBy(l => l.Address).ToList();
            using (Task_1Entities context = new Task_1Entities())
            {
                foreach (var loc in locationsToAdd)
                {
                    context.Locations.Add(loc);
                    string input = (DateTime.Now + " / Added new location with address " + loc.Address + " directly to database");
                    archive.WriteToFile(input);
                    context.SaveChanges();
                }
            }
        }
 /// <summary>
 /// This method updates an employee from the database
 /// </summary>
 /// <param name="employee">Employee that is to be updated</param>
 /// <returns>A updated employee</returns>
 public Employee UpdateEmployee(Employee employee)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             Employee EmployeeToUpdate = (from e in context.Employees where e.EmployeeID == employee.EmployeeID select e).First();
             EmployeeToUpdate.FirstName   = employee.FirstName;
             EmployeeToUpdate.LastName    = employee.LastName;
             EmployeeToUpdate.DateOfBirth = employee.DateOfBirth;
             EmployeeToUpdate.IDNumber    = employee.IDNumber;
             EmployeeToUpdate.JMBG        = employee.JMBG;
             EmployeeToUpdate.Gender      = employee.Gender;
             EmployeeToUpdate.PhoneNumber = employee.PhoneNumber;
             EmployeeToUpdate.SectorID    = employee.SectorID;
             EmployeeToUpdate.Location    = employee.Location;
             EmployeeToUpdate.Manager     = employee.Manager;
             context.SaveChanges();
             string input = (DateTime.Now + " / Updated Employee named " + EmployeeToUpdate.FirstName + " " + EmployeeToUpdate.LastName + " in sector " + EmployeeToUpdate.Sector.SectorName);
             archive.WriteToFile(input);
             return(employee);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         return(null);
     }
 }
 /// <summary>
 /// Adds new location to database
 /// </summary>
 /// <param name="L">A location that is to be added</param>
 public void AddLocation(Location L)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             context.Locations.Add(L);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 public List <Employee> GetAllManagers(Employee updateEmployee)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             return((from x in context.Employees where x.EmployeeID != updateEmployee.EmployeeID select x).ToList());
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         return(null);
     }
 }
 /// <summary>
 /// This method gets all the sectors from database and adds them to a list
 /// </summary>
 /// <returns>A list of all sectors in DataBase</returns>
 public List <Sector> GetAllSectors()
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             List <Sector> list = new List <Sector>();
             list = (from x in context.Sectors select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception" + ex.Message.ToString());
         return(null);
     }
 }
 /// <summary>
 /// This method gets all the employees from view and adds them to a list
 /// </summary>
 /// <returns>A list of all employees in view</returns>
 public List <EmployeeView> GetEmployeeViews()
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             List <EmployeeView> list = new List <EmployeeView>();
             list = (from x in context.EmployeeViews select x).ToList();
             return(list);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
         return(null);
     }
 }
 /// <summary>
 /// This method deletes an employee from the database
 /// </summary>
 /// <param name="deleteID">ID of employee that is to be deleted</param>
 public void DeleteEmplyee(int deleteID)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             Employee employee = (from e in context.Employees where e.EmployeeID == deleteID select e).FirstOrDefault();
             context.Employees.Remove(employee);
             context.SaveChanges();
             string input = (DateTime.Now + " / Deleted Employee named " + employee.FirstName + " " + employee.LastName + " in sector " + employee.Sector.SectorName);
             archive.WriteToFile(input);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 public Sector AddSector(Sector sector)
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             Sector newSector = new Sector();
             newSector.SectorID   = sector.SectorID;
             newSector.SectorName = sector.SectorName;
             context.Sectors.Add(newSector);
             context.SaveChanges();
             return(newSector);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception " + ex.Message.ToString() + "/" + ex.Source + "/" + ex.InnerException);
         return(null);
     }
 }
        public void EmployeesToDB()
        {
            List <Employee> existingEmployees = GetAllEmployees();
            List <Employee> newEmployees      = new List <Employee>();
            StreamReader    sr = new StreamReader(@".../.../Employees.txt");

            if (!File.Exists(@".../.../Employees.txt"))
            {
                File.Create(@".../.../Employees.txt").Close();
            }
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] employeeData = line.Split(',');
                Employee e            = new Employee();
                e.FirstName   = employeeData[0];
                e.LastName    = employeeData[1];
                e.DateOfBirth = Convert.ToDateTime(employeeData[2]);
                e.IDNumber    = employeeData[3];
                e.JMBG        = employeeData[4];
                e.Gender      = employeeData[5];
                e.PhoneNumber = employeeData[6];
                e.SectorID    = Convert.ToInt32(employeeData[7]);
                e.LocationID  = Convert.ToInt32(employeeData[8]);
                e.Manager     = employeeData[9];
                newEmployees.Add(e);
            }
            var employeesToAdd = newEmployees.Where(e => existingEmployees.All(e2 => e2.JMBG != e.JMBG));

            using (Task_1Entities context = new Task_1Entities())
            {
                foreach (var emp in employeesToAdd)
                {
                    context.Employees.Add(emp);
                    string input = (DateTime.Now + " / Added new Employee with name " + emp.FirstName + " " + emp.LastName + " directly to database");
                    archive.WriteToFile(input);
                    context.SaveChanges();
                }
            }
        }
Exemplo n.º 12
0
 private void UpdateEmployeeExecute()
 {
     try
     {
         using (Task_1Entities context = new Task_1Entities())
         {
             int                userID        = Employee.EmployeeID;
             Employee           e             = (from x in context.Employees where x.EmployeeID == userID select x).First();
             UpdateEmployeeView updateEmplyee = new UpdateEmployeeView(e);
             updateEmplyee.ShowDialog();
             if ((updateEmplyee.DataContext as UpdateEmployeeViewModel).IsUpdateEmployee == true)
             {
                 EmpViews = empModel.GetEmployeeViews();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Exception" + ex.Message.ToString());
     }
 }