/// <summary> /// This method adds employee to DbSet and then save changes to database. /// </summary> /// <param name="employeeToAdd">Employee.</param> public void AddEmployee(vwEmployee employeeToAdd) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblEmployee employee = new tblEmployee { Name = employeeToAdd.Name, Surname = employeeToAdd.Surname, DateOfBirth = employeeToAdd.DateOfBirth, NumberOfIdentityCard = employeeToAdd.NumberOfIdentityCard, JMBG = employeeToAdd.JMBG, Gender = employeeToAdd.Gender, PhoneNumber = employeeToAdd.PhoneNumber, Sector = employeeToAdd.Sector, LocationID = employeeToAdd.LocationID, Manager = employeeToAdd.Manager }; context.tblEmployees.Add(employee); context.SaveChanges(); employeeToAdd.EmployeeID = employee.EmployeeID; LogAction("Employee " + employeeToAdd.Employee + " created. ID: " + employeeToAdd.EmployeeID + " JMBG: " + employeeToAdd.JMBG); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// This method edits employee in DbSet and then saves changes to database. /// </summary> /// <param name="employee">Employee to edit.</param> /// <returns>Edited employee.</returns> public vwEmployee EditEmployee(vwEmployee employee) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblEmployee employeeToEdit = context.tblEmployees.Where(x => x.EmployeeID == employee.EmployeeID).FirstOrDefault(); employeeToEdit.Name = employee.Name; employeeToEdit.Surname = employee.Surname; employeeToEdit.DateOfBirth = employee.DateOfBirth; employeeToEdit.NumberOfIdentityCard = employee.NumberOfIdentityCard; employeeToEdit.Gender = employee.Gender; employeeToEdit.PhoneNumber = employee.PhoneNumber; employeeToEdit.Sector = employee.Sector; employeeToEdit.LocationID = employee.LocationID; employeeToEdit.Manager = employee.Manager; context.SaveChanges(); LogAction("Employee with ID " + employeeToEdit.EmployeeID + " updated."); return(employee); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method edits employee in DbSet and then saves changes to database. /// </summary> /// <param name="employee"></param> /// <returns>True if edited, false if not.</returns> public bool EditEmployee(vwEmployee employee) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblEmployee employeeToEdit = context.tblEmployees.Where(x => x.EmployeeId == employee.EmployeeId).FirstOrDefault(); employeeToEdit.NameAndSurname = employee.NameAndSurname; employeeToEdit.DateOfBirth = CalculateDateOfBirth.Calculate(employee.JMBG); employeeToEdit.JMBG = employee.JMBG; employeeToEdit.NumberOfIdentityCard = employee.NumberOfIdentityCard; employeeToEdit.Gender = employee.Gender; employeeToEdit.PhoneNumber = employee.PhoneNumber; employeeToEdit.Sector = employee.Sector; employeeToEdit.LocationId = employee.LocationId; employeeToEdit.Manager = employee.Manager; context.SaveChanges(); logger.LogAction("Employee with JMBG " + employeeToEdit.JMBG + " updated."); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method adds employee to DbSet and then save changes to database. /// </summary> /// <param name="employeeToAdd">Employee to be added.</param> /// <returns>True if added, false if not.</returns> public bool AddEmployee(vwEmployee employeeToAdd) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblEmployee employee = new tblEmployee { NameAndSurname = employeeToAdd.NameAndSurname, DateOfBirth = CalculateDateOfBirth.Calculate(employeeToAdd.JMBG), NumberOfIdentityCard = employeeToAdd.NumberOfIdentityCard, JMBG = employeeToAdd.JMBG, Gender = employeeToAdd.Gender, PhoneNumber = employeeToAdd.PhoneNumber, Sector = employeeToAdd.Sector, LocationId = employeeToAdd.LocationId, Manager = employeeToAdd.Manager }; context.tblEmployees.Add(employee); context.SaveChanges(); employeeToAdd.EmployeeId = employee.EmployeeId; logger.LogAction("Employee " + employeeToAdd.NameAndSurname + " created. ID: " + employeeToAdd.EmployeeId + " JMBG: " + employeeToAdd.JMBG); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method reads locations from a file, adds them to DbSet, and then saves changes to the database. /// </summary> public void AddLocations() { try { string[] lines = File.ReadAllLines(@"../../Lokacije.txt"); List <string> list = new List <string>(); for (int i = 0; i < lines.Length; i++) { tblLocation location = new tblLocation(); list = lines[i].Split(',').ToList(); location.Address = list[0]; location.City = list[1]; location.State = list[2]; using (Employee_DataEntities context = new Employee_DataEntities()) { //checking if location already exists if (context.tblLocations.Where(x => x.Address == location.Address && x.City == location.City && x.State == location.State).ToList().Count == 0) { //if not exists, adding location to DbSet and saving changes to database context.tblLocations.Add(location); context.SaveChanges(); logger.LogAction("Location " + location.Address + ", " + location.City + ", " + location.State + " created."); } } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// This method finds sector in DbSet based on a forwared name. /// </summary> /// <param name="sectorName"></param> /// <returns>Id of sector.</returns> public int FindSector(string sectorName) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { return(context.vwSectors.Where(x => x.SectorName == sectorName).Select(x => x.SectorID).FirstOrDefault()); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(0); } }
/// <summary> /// This method creates a list of data from view of employees. /// </summary> /// <returns>List of employees.</returns> public List <vwEmployee> GetAllEmployees() { try { using (Employee_DataEntities context = new Employee_DataEntities()) { List <vwEmployee> employees = new List <vwEmployee>(); employees = (from x in context.vwEmployees select x).ToList(); return(employees); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method creates a list of data from view of locations. /// </summary> /// <returns>List of locations.</returns> public List <vwLocation> GetAllLocations() { try { using (Employee_DataEntities context = new Employee_DataEntities()) { List <vwLocation> locations = new List <vwLocation>(); locations = (from x in context.vwLocations select x).OrderBy(x => x.Address).ToList(); return(locations); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method creates a list of possible managers of employee. /// </summary> /// <param name="employee">Employee.</param> /// <returns>List of possible managers.</returns> public List <vwEmployee> GetAllManagers(vwEmployee employee) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { List <vwEmployee> employees = new List <vwEmployee>(); //inserting into list all employees except forwarded employee (finding him based on jmbg) employees = context.vwEmployees.Where(x => x.JMBG != employee.JMBG).ToList(); return(employees); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// This method adds sectors to DbSet and then save changes to database. /// </summary> /// <param name="sectorToAdd">Sector name.</param> public void AddSector(string sectorToAdd) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblSector sector = new tblSector { SectorName = sectorToAdd }; context.tblSectors.Add(sector); context.SaveChanges(); LogAction("Sector " + sector.SectorName + " with ID " + sector.SectorID + " created."); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// This method deletes employee from DbSet and then saves changes to database. /// </summary> /// <param name="employeeID">Employee id.</param> /// <returns>True if deleted, false if not.</returns> public bool DeleteEmployee(int employeeID) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { //creating a list of employees for this manager var employeeOfThisManager = context.tblEmployees.Where(x => x.Manager == employeeID).ToList(); //if the list is not empty, setting manager id to null for every employee in that list if (employeeOfThisManager.Count() > 0) { foreach (var employee in employeeOfThisManager) { employee.Manager = null; logger.LogAction("Employee with JMBG " + employee.JMBG + " updated so he has no manager."); } } //finding employee with forwarded id tblEmployee employeeToDelete = context.tblEmployees.Where(x => x.EmployeeId == employeeID).FirstOrDefault(); //if that employee was the only in the sector, deleting sector var peopleInSector = context.tblEmployees.Where(x => x.Sector == employeeToDelete.Sector).ToList(); //removing employee from DbSet and saving changes to database context.tblEmployees.Remove(employeeToDelete); context.SaveChanges(); logger.LogAction("Employee with JMBG " + employeeToDelete.JMBG + " deleted."); if (peopleInSector.Count() == 1) { var sector = context.tblSectors.Where(x => x.SectorId == employeeToDelete.Sector).FirstOrDefault(); context.tblSectors.Remove(sector); context.SaveChanges(); logger.LogAction("Sector " + sector.SectorName + " with ID: " + sector.SectorId + " deleted."); } return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <summary> /// This method checks if sector has employees, and if there are no employees deletes sector. /// </summary> /// <param name="sectorName">Sector name.</param> public void DeleteSector(string sectorName) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblSector sectorToDelete = context.tblSectors.Where(x => x.SectorName == sectorName).FirstOrDefault(); var peopleInSector = context.tblEmployees.Where(x => x.Sector == sectorToDelete.SectorId).ToList(); if (peopleInSector.Count == 0) { context.tblSectors.Remove(sectorToDelete); context.SaveChanges(); logger.LogAction("Sector " + sectorToDelete.SectorName + " with ID " + sectorToDelete.SectorId + " deleted."); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// This method checks if sector already exists in database. /// </summary> /// <param name="sectorName">Sector name.</param> /// <returns>True if sector exists, false if not.</returns> public bool IsSectorExists(string sectorName) { try { using (Employee_DataEntities context = new Employee_DataEntities()) { tblSector sector = context.tblSectors.Where(x => x.SectorName == sectorName).FirstOrDefault(); if (sector != null) { return(true); } else { return(false); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }