134 lines
4.8 KiB
C#
134 lines
4.8 KiB
C#
using EmployeeManagmentContracts.SearchModels;
|
||
using EmployeeManagmentContracts.StoragesContracts;
|
||
using EmployeeManagmentContracts.ViewModels;
|
||
using EmployeeManagmentDataBaseImplement.Models;
|
||
|
||
namespace EmployeeManagmentDataBaseImplement.Implements
|
||
{
|
||
public class EmployeeStorage : IEmployeeStorage
|
||
{
|
||
// Получить полный список сотрудников
|
||
public List<EmployeeViewModel> GetFullList()
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
return context.Employees
|
||
.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.Surname} {e.PhisicalPerson.Name}"
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
// Получить отфильтрованный список сотрудников
|
||
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
if (model == null) return new List<EmployeeViewModel>();
|
||
|
||
return context.Employees
|
||
.Where(e =>
|
||
(string.IsNullOrEmpty(model.NameJob) || e.NameJob.Contains(model.NameJob)) &&
|
||
(!model.StartDateFrom.HasValue || e.StartJob >= model.StartDateFrom) &&
|
||
(!model.StartDateTo.HasValue || e.EndJob <= model.StartDateTo))
|
||
.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.Surname} {e.PhisicalPerson.Name}"
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
// Получить сотрудника по ID
|
||
public EmployeeViewModel? GetElement(int id)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Employees
|
||
.FirstOrDefault(e => e.Id == id);
|
||
|
||
return entity == null ? null : new EmployeeViewModel
|
||
{
|
||
Id = entity.Id,
|
||
NameJob = entity.NameJob,
|
||
StartJob = entity.StartJob,
|
||
EndJob = entity.EndJob,
|
||
PartTimeJob = entity.PartTimeJob,
|
||
Bid = entity.Bid,
|
||
PhysicalPersonsId = entity.PhisicalPersonsId,
|
||
PhysicalPersonName = entity.PhisicalPerson != null
|
||
? $"{entity.PhisicalPerson.Surname} {entity.PhisicalPerson.Name}"
|
||
: "Не указано" // Обработка отсутствующего физического лица
|
||
};
|
||
}
|
||
|
||
|
||
// Добавить нового сотрудника
|
||
public void Insert(EmployeeViewModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var newEmployee = new Employee
|
||
{
|
||
NameJob = model.NameJob,
|
||
StartJob = model.StartJob,
|
||
EndJob = model.EndJob,
|
||
PartTimeJob = model.PartTimeJob,
|
||
Bid = model.Bid,
|
||
PhisicalPersonsId = model.PhysicalPersonsId
|
||
};
|
||
|
||
context.Employees.Add(newEmployee);
|
||
context.SaveChanges();
|
||
}
|
||
|
||
// Обновить сотрудника
|
||
public void Update(EmployeeViewModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Employees.FirstOrDefault(e => e.Id == model.Id);
|
||
|
||
if (entity != null)
|
||
{
|
||
entity.NameJob = model.NameJob;
|
||
entity.StartJob = model.StartJob;
|
||
entity.EndJob = model.EndJob;
|
||
entity.PartTimeJob = model.PartTimeJob;
|
||
entity.Bid = model.Bid;
|
||
entity.PhisicalPersonsId = model.PhysicalPersonsId;
|
||
|
||
context.SaveChanges();
|
||
}
|
||
}
|
||
|
||
// Удалить сотрудника
|
||
public void Delete(int id)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Employees.FirstOrDefault(e => e.Id == id);
|
||
|
||
if (entity != null)
|
||
{
|
||
context.Employees.Remove(entity);
|
||
context.SaveChanges();
|
||
}
|
||
}
|
||
}
|
||
}
|