public bool SetSalary(vwEmployee employee) { try { using (HotelEntities context = new HotelEntities()) { tblEmployee employeeToEdit = context.tblEmployees.Where(x => x.EmployeeId == employee.EmployeeId).FirstOrDefault(); employeeToEdit.Salary = employee.Salary; context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }
/// <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 adds employees to DbSet and then save changes to database. /// </summary> /// <param name="employeeToAdd">Employee.</param> /// <returns>True if added, false if not.</returns> public bool AddEmployee(vwEmployee employeeToAdd) { try { using (HotelEntities context = new HotelEntities()) { tblUser user = new tblUser { DateOfBirth = employeeToAdd.DateOfBirth, Email = employeeToAdd.Email, Name = employeeToAdd.Name, Password = employeeToAdd.Password, Surname = employeeToAdd.Surname, Username = employeeToAdd.Username }; context.tblUsers.Add(user); context.SaveChanges(); employeeToAdd.UserId = user.UserId; tblEmployee employee = new tblEmployee { UserId = user.UserId, Citizenship = employeeToAdd.Citizenship, Engagement = employeeToAdd.Engagement, Gender = employeeToAdd.Gender, HotelFloor = employeeToAdd.HotelFloor, Salary = null }; context.tblEmployees.Add(employee); context.SaveChanges(); employeeToAdd.EmployeeId = employee.EmployeeId; return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }