2024-11-13 14:41:01 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using EmployeeManagmentContracts.BindingModels;
|
2024-11-12 21:25:53 +04:00
|
|
|
|
using EmployeeManagmentContracts.BusinessLogicContracts;
|
|
|
|
|
using EmployeeManagmentContracts.SearchModels;
|
|
|
|
|
using EmployeeManagmentContracts.ViewModels;
|
2024-11-13 14:41:01 +04:00
|
|
|
|
using EmployeeManagmentDataBaseImplement;
|
|
|
|
|
using EmployeeManagmentDataBaseImplement.Models;
|
2024-11-12 13:07:37 +04:00
|
|
|
|
|
|
|
|
|
namespace EmployeeManagmentBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class EmployeeLogic : IEmployeeLogic
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
private readonly EmployeeManagementDbContext _context;
|
|
|
|
|
|
|
|
|
|
public EmployeeLogic(EmployeeManagementDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public void CreateOrUpdate(EmployeeBindingModel model)
|
2024-11-12 13:07:37 +04:00
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
// Проверка на обязательные поля
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-12 13:07:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 14:41:01 +04:00
|
|
|
|
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public void Delete(int id)
|
2024-11-12 13:07:37 +04:00
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
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.");
|
|
|
|
|
}
|
2024-11-12 13:07:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 14:41:01 +04:00
|
|
|
|
|
2024-11-12 13:07:37 +04:00
|
|
|
|
public EmployeeViewModel? GetEmployeeById(int id)
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
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;
|
2024-11-12 13:07:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public List<EmployeeViewModel> GetEmployees(EmployeeSearchModel model)
|
2024-11-12 13:07:37 +04:00
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
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();
|
2024-11-12 13:07:37 +04:00
|
|
|
|
}
|
2024-11-13 14:41:01 +04:00
|
|
|
|
|
2024-11-12 13:07:37 +04:00
|
|
|
|
}
|
|
|
|
|
}
|