using System.Collections.Generic; using System.Linq; using EmployeeManagmentContracts.BindingModels; using EmployeeManagmentContracts.BusinessLogicContracts; using EmployeeManagmentContracts.SearchModels; using EmployeeManagmentContracts.ViewModels; using EmployeeManagmentDataBaseImplement; using EmployeeManagmentDataBaseImplement.Models; namespace EmployeeManagmentBusinessLogic.BusinessLogic { public class EmployeeLogic : IEmployeeLogic { private readonly EmployeeManagementDbContext _context; public EmployeeLogic(EmployeeManagementDbContext context) { _context = context; } public void CreateOrUpdate(EmployeeBindingModel model) { // Проверка на обязательные поля if (string.IsNullOrWhiteSpace(model.NameJob)) { throw new ArgumentException("Job name cannot be empty."); } // Ищем сотрудника по ID, если он уже существует var employee = _context.Employees.FirstOrDefault(e => e.Id == model.Id); if (employee == null) { // Создаем нового сотрудника, если не найден employee = new Employee { NameJob = model.NameJob, StartJob = model.StartJob, EndJob = model.EndJob, PartTimeJob = model.PartTimeJob, Bid = model.Bid, PhisicalPersonsId = model.PhisicalPersonsId }; try { _context.Employees.Add(employee); _context.SaveChanges(); } catch (Exception ex) { // Логирование ошибки и обработка throw new InvalidOperationException("Error while adding new employee: " + ex.Message); } } else { // Обновляем данные существующего сотрудника employee.NameJob = model.NameJob; employee.StartJob = model.StartJob; employee.EndJob = model.EndJob; employee.PartTimeJob = model.PartTimeJob; employee.Bid = model.Bid; employee.PhisicalPersonsId = model.PhisicalPersonsId; try { _context.SaveChanges(); } catch (Exception ex) { // Логирование ошибки и обработка throw new InvalidOperationException("Error while updating employee: " + ex.Message); } } } public void Delete(int id) { var employee = _context.Employees.FirstOrDefault(e => e.Id == id); if (employee != null) { // Дополнительная проверка на связи с другими таблицами, если необходимо if (employee.Salaries.Any() || employee.Vacations.Any()) { throw new InvalidOperationException("Employee cannot be deleted because they have related data (e.g., salaries or vacations)."); } _context.Employees.Remove(employee); _context.SaveChanges(); } else { throw new KeyNotFoundException($"Employee with ID {id} not found."); } } public EmployeeViewModel? GetEmployeeById(int id) { var employee = _context.Employees .Where(e => e.Id == id) .Select(e => new EmployeeViewModel { Id = e.Id, NameJob = e.NameJob, StartJob = e.StartJob, EndJob = e.EndJob, PartTimeJob = e.PartTimeJob, Bid = e.Bid, PhysicalPersonsId = e.PhisicalPersonsId, PhysicalPersonName = e.PhisicalPerson != null ? e.PhisicalPerson.Name : null }) .FirstOrDefault(); return employee; } public List GetEmployees(EmployeeSearchModel model) { var query = _context.Employees.AsQueryable(); if (model.Id.HasValue) { query = query.Where(e => e.Id == model.Id.Value); } if (!string.IsNullOrWhiteSpace(model.NameJob)) { query = query.Where(e => e.NameJob.Contains(model.NameJob)); } if (model.StartDateFrom.HasValue) { query = query.Where(e => e.StartJob >= model.StartDateFrom.Value); } if (model.StartDateTo.HasValue) { query = query.Where(e => e.StartJob <= model.StartDateTo.Value); } if (model.Bid.HasValue) { query = query.Where(e => e.Bid == model.Bid.Value); } return query .Select(e => new EmployeeViewModel { Id = e.Id, NameJob = e.NameJob, StartJob = e.StartJob, EndJob = e.EndJob, PartTimeJob = e.PartTimeJob, Bid = e.Bid, PhysicalPersonsId = e.PhisicalPersonsId, PhysicalPersonName = e.PhisicalPerson != null ? e.PhisicalPerson.Name : null }) .ToList(); } } }