//Add Shift : public void Add(EmployeesData data) { shift shift = new shift(); shift.date = data.Date; shift.start_time = data.StartTime; shift.end_time = data.EndTime; db.shifts.Add(shift); db.SaveChanges(); }
public bool Add(EmployeesData employeeData) { // Check if employee exists int count = db.employees.Where(x => x.ID == employeeData.ID).Count(); if (count == 0) { return(false); } // Check if Shift exists List <EmployeesData> data = this.GetAllEmployeesData(); bool isShiftAlreadyExists = false; int shiftID = 0; foreach (shift element in db.shifts) { if (element.date.Year == employeeData.Date.Year && element.date.Month == employeeData.Date.Month && element.date.Day == employeeData.Date.Day && element.start_time == employeeData.StartTime && element.end_time == employeeData.EndTime) { isShiftAlreadyExists = true; shiftID = element.ID; break; } } if (isShiftAlreadyExists == false) { return(false); } // Check if Employee's shift is duplicated List <EmployeesData> specificEmployeeData = data.Where(x => x.ID == employeeData.ID).ToList(); foreach (EmployeesData element in specificEmployeeData) { if (element.Date.Year == employeeData.Date.Year && element.Date.Month == employeeData.Date.Month && element.Date.Day == employeeData.Date.Day && element.StartTime == employeeData.StartTime && element.EndTime == employeeData.EndTime) { return(false); } } // Add Shift to Employee employeeshift empShift = db.employeeshifts.Where(x => x.employeeID == employeeData.ID).First(); empShift.shiftID = shiftID; db.employeeshifts.Add(empShift); db.SaveChanges(); return(true); }
//Edit Employee : public void Edit(EmployeesData employeesData) { employee emp = db.employees.Where(x => x.ID == employeesData.ID).First(); emp.first_name = employeesData.FirstName; emp.last_name = employeesData.LastName; emp.start_work_year = employeesData.StartWorkYear; emp.departmentID = employeesData.DepartmentID; db.SaveChanges(); }
public bool Delete(EmployeesData employeeData) { // Check if employee exists int count = db.employees.Where(x => x.ID == employeeData.ID).Count(); if (count == 0) { return(false); } // Check if employee is a manager, if yes, don't delete him count = db.departments.Where(x => x.manager == employeeData.ID).Count(); if (count != 0) { return(false); } // Remove Employee's Shifts var employeeShift = db.employeeshifts.Where(x => x.employeeID == employeeData.ID); foreach (var element in employeeShift) { db.employeeshifts.Remove(element); } db.SaveChanges(); // Remove Employee var employee = db.employees.Where(x => x.ID == employeeData.ID).First(); db.employees.Remove(employee); db.SaveChanges(); return(true); }