PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentBusinessLogic/BusinessLogic/EmployeeLogic.cs

89 lines
3.2 KiB
C#

using EmployeeManagmentContracts.BindingModels;
using EmployeeManagmentContracts.BusinessLogicContracts;
using EmployeeManagmentContracts.SearchModels;
using EmployeeManagmentContracts.StoragesContracts;
using EmployeeManagmentContracts.ViewModels;
namespace EmployeeManagmentBusinessLogic.BusinessLogic
{
public class EmployeeLogic : IEmployeeLogic
{
private readonly IEmployeeStorage _employeeStorage;
public EmployeeLogic(IEmployeeStorage employeeStorage)
{
_employeeStorage = employeeStorage;
}
public List<EmployeeViewModel> GetEmployees(EmployeeSearchModel model)
{
var employees = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model);
return employees.Select(e => new EmployeeViewModel
{
Id = e.Id,
NameJob = e.NameJob,
StartJob = e.StartJob,
EndJob = e.EndJob,
PartTimeJob = e.PartTimeJob,
Bid = e.Bid,
PhysicalPersonName = e.PhysicalPerson.Name
}).ToList();
}
public EmployeeViewModel? GetEmployeeById(int id)
{
var employee = _employeeStorage.GetElement(id);
return employee != null ? new EmployeeViewModel
{
Id = employee.Id,
NameJob = employee.NameJob,
StartJob = employee.StartJob,
EndJob = employee.EndJob,
PartTimeJob = employee.PartTimeJob,
Bid = employee.Bid,
PhysicalPersonName = employee.PhysicalPerson.Name
} : null;
}
public void CreateOrUpdate(EmployeeBindingModel model)
{
if (model.Id.HasValue)
{
var existingEmployee = _employeeStorage.GetElement(model.Id.Value);
if (existingEmployee == null)
throw new Exception("Сотрудник не найден");
existingEmployee.NameJob = model.NameJob;
existingEmployee.StartJob = model.StartJob;
existingEmployee.EndJob = model.EndJob;
existingEmployee.PartTimeJob = model.PartTimeJob;
existingEmployee.Bid = model.Bid;
existingEmployee.PhysicalPersonId = model.PhysicalPersonId;
_employeeStorage.Update(existingEmployee);
}
else
{
var newEmployee = new Employee
{
NameJob = model.NameJob,
StartJob = model.StartJob,
EndJob = model.EndJob,
PartTimeJob = model.PartTimeJob,
Bid = model.Bid,
PhysicalPersonId = model.PhysicalPersonId
};
_employeeStorage.Insert(newEmployee);
}
}
public void Delete(int id)
{
var employee = _employeeStorage.GetElement(id);
if (employee == null)
throw new Exception("Сотрудник не найден");
_employeeStorage.Delete(id);
}
}
}