public Employee UpdateEmployeeAreas(Employee employee, List <Area> areas) { using (var dbContext = new CallSystemDbContext()) { employee = (from x in dbContext.Employees.Include(e => e.EmployeeAreas.Select(ea => ea.Area)) .Include(e => e.EmployeeAvailability) where x.Name == employee.Name select x).FirstOrDefault(); employee.IsActive = true; employee.EmployeeAvailability.IsAvailable = true; //Add new areas to existing employee foreach (var area in areas) { if (!employee.Areas.Any(a => a.Name.Equals(area.Name))) { employee.EmployeeAreas.Add(new EmployeeArea() { AreaName = area.Name, EmployeeGuid = employee.EmployeeGuid }); } } //employee areas to unassign var employeeAreasToUnassign = employee.EmployeeAreas.Where(ea => areas.All(a => a.Name != ea.AreaName)) .ToList(); dbContext.EmployeesAreas.RemoveRange(employeeAreasToUnassign); dbContext.SaveChanges(); return(employee); } }
/// <summary> /// Add new area to system /// </summary> /// <returns>Wrapper where you will see success or no and error</returns> public ResponseWrapper <Area> AddArea(string areaName) { var result = new ResponseWrapper <Area>(); try { if (GetAreasByName(new List <string> { areaName }).Count > 0) { result.ErrorMessage = "Area already exists!"; return(result); } using (var dbContext = new CallSystemDbContext()) { var newArea = new Area() { Name = areaName }; dbContext.Areas.Add(newArea); dbContext.SaveChanges(); result.IsSuccess = true; result.ResponseData = newArea; } } catch (Exception e) { result.ErrorMessage = e.Message; } return(result); }
public Employee SaveNewEmployee(string employeeName, List <Area> areas) { using (var dbContext = new CallSystemDbContext()) { var employeeGuid = Guid.NewGuid(); var newEmployee = new Employee() { EmployeeGuid = employeeGuid, Name = employeeName, IsActive = true, }; //new employee is always available after registration newEmployee.EmployeeAvailability = new EmployeeAvailability() { Employee = newEmployee, EmployeeGuid = employeeGuid, IsAvailable = true }; var employeeAreas = areas.Select(area => new EmployeeArea() { Area = dbContext.Areas.Find(area.Name), AreaName = area.Name, EmployeeGuid = employeeGuid, Employee = newEmployee }) .ToList(); dbContext.Employees.Add(newEmployee); dbContext.EmployeesAreas.AddRange(employeeAreas); dbContext.SaveChanges(); return(newEmployee); } }
/// <summary> /// Save calls and assigned employees to database, /// employees become not available /// </summary> private void SaveCallsAndAssignedEmployees(List <Call> calls, List <Employee> assignedEmployees) { using (var dbContext = new CallSystemDbContext()) { dbContext.Calls.AddRange(calls); foreach (var assignedEmployee in assignedEmployees) { // need more time (( //var employee = (from x in dbContext.Employees.Include(e => e.EmployeeAvailability) // where x.EmployeeGuid == assignedEmployee.EmployeeGuid // select x).FirstOrDefault(); ////var availability = dbContext.EmployeeAvailabilities.FirstOrDefault(ea => ea.EmployeeGuid.Equals(assignedEmployee.EmployeeGuid)); //if (employee== null) //{ // continue; //} //employee.EmployeeAvailability.IsAvailable = false; } try { dbContext.SaveChanges(); } catch (Exception e) { } } }
/// <summary> /// Get all areas. TODO: add cache support /// </summary> public List <Area> GetAll() { using (var dbContext = new CallSystemDbContext()) { return((from x in dbContext.Areas select x).ToList()); } }
public Employee GetEmployee(string employeeName) { using (var dbContext = new CallSystemDbContext()) { return((from x in AddDefaultPrefetchPath(dbContext.Employees) where x.Name == employeeName select x).FirstOrDefault()); } }
/// <summary> /// Get areas by name. TODO: add cache support /// </summary> public List <Area> GetAreasByName(List <string> areaNames) { if (areaNames.IsNullOrEmpty()) { return(new List <Area>()); } using (var dbContext = new CallSystemDbContext()) { return((from x in dbContext.Areas where areaNames.Contains(x.Name) select x).ToList()); } }
public List <Employee> GetActiveAvailableEmployees(List <string> area) { using (var dbContext = new CallSystemDbContext()) { var employeesQuery = from x in dbContext.Employees join ea in dbContext.EmployeesAreas on x.EmployeeGuid equals ea.EmployeeGuid where x.IsActive && x.EmployeeAvailability.IsAvailable && area.Contains(ea.AreaName) select x; employeesQuery.Include(p => p.EmployeeAreas.Select(ea => ea.Area)) .Include(p => p.EmployeeAvailability).Load(); return(employeesQuery.ToList()); } }
/// <summary> /// Reset all employees. This happens at the end of working day /// </summary> /// <returns>true/false which indicates success</returns> public bool Reset() { try { using (var dbContext = new CallSystemDbContext()) { var employees = (from e in dbContext.Employees where e.IsActive select e).ToList(); employees.ForEach(e => e.IsActive = false); dbContext.SaveChanges(); } return(true); } catch (Exception e) { return(false); } }