public async Task Add(EmployeeBll employee, int departamentId, int jobId, DateTime?hireDate) { var model = _mapper.Map <EmployeeDal>(employee); var dept = _db.Departaments.FirstOrDefault(x => x.Id == departamentId); if (dept == null) { throw new KeyNotFoundException($"Не найден отдел с идентификационным номером {departamentId}"); } var job = _db.Jobs.FirstOrDefault(x => x.Id == jobId); if (job == null) { throw new KeyNotFoundException($"Не найдена должность с идентификационным номером {jobId}"); } var jobHistory = new JobHistoryDal { DepartamentId = dept.Id, JobId = job.Id, StartDate = hireDate, EmployeeId = model.Id, Salary = model.Salary }; model.JobHistory.Add(jobHistory); _db.Employees.Add(model); await _db.SaveChangesAsync(); }
public async Task ChangeJob(int id, int newJobId, DateTime changeDate, int deptId, decimal salary) { var emp = _db.Employees.FirstOrDefault(x => x.Id == id); if (emp == null) { throw new KeyNotFoundException($"Не найден клиент с идентификационным номером {id}"); } var newJob = _db.Jobs.FirstOrDefault(x => x.Id == newJobId); if (newJob == null) { throw new KeyNotFoundException($"Не найдена должность с идентификационным номером {newJobId}"); } var lastJob = emp.JobHistory.FirstOrDefault(x => x.EndDate == null && x.StartDate != DateTime.MinValue); if (lastJob == null) { throw new KeyNotFoundException($"Не найдена последняя должность сотрудника"); } var dept = _db.Departaments.FirstOrDefault(x => x.Id == deptId); if (dept == null) { throw new KeyNotFoundException($"Не найден отдел с идентификационным номером {deptId}"); } lastJob.EndDate = changeDate; var newJobHistoryInfo = new JobHistoryDal() { Job = newJob, JobId = newJob.Id, Departament = dept, DepartamentId = dept.Id, StartDate = changeDate, Employee = emp, EmployeeId = emp.Id, Salary = salary }; emp.Salary = salary; emp.JobHistory.Add(newJobHistoryInfo); await _db.SaveChangesAsync(); }
protected override void Seed(MyDbContext context) { base.Seed(context); var dept_hr = new DepartamentDal { Title = "Отдел по работе с физическими лицами" }; var dept_dev = new DepartamentDal { Title = "Отдел по работе с юридическими лицами" }; var dept_marketing = new DepartamentDal { Title = "Отдел по представлению ИМНС в судах" }; var job_jun = new JobDal { Title = "Специалист 1 категории", MinSalary = 600, MaxSalary = 900 }; var job_mid = new JobDal { Title = "Специалист 2 категории", MinSalary = 900, MaxSalary = 1700 }; var job_sen = new JobDal { Title = "Руководитель отдела", MinSalary = 1700, MaxSalary = 5000 }; context.Departaments.AddRange(new[] { dept_hr, dept_dev, dept_marketing }); context.Jobs.AddRange(new[] { job_jun, job_mid, job_sen }); List <EmployeeDal> emplTest = new List <EmployeeDal>(); for (int i = 0; i < 10; i++) { var empl1 = new EmployeeDal { Birthday = DateTime.Now.AddYears(-24 + i), Email = $"test{i}@gmail.com", Fired = false, FireDate = null, FirstName = $"Тестер{i}", HireDate = DateTime.Now.AddYears(-1 - i), PhoneNumber = "+375297768576", Salary = 1500, SecondName = $"Тестовский{i}", ThirdName = $"Тестовов{i}", JobHistory = new List <JobHistoryDal> { } }; var hist = new JobHistoryDal { Departament = dept_dev, Employee = empl1, Salary = 1500, Job = job_mid, StartDate = DateTime.Now.AddYears(-1) }; empl1.JobHistory.Add(hist); context.Employees.Add(empl1); context.JobHistories.Add(hist); } context.SaveChanges(); }