PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentDataBaseImplement/Implements/EmployeeStorage.cs

137 lines
5.1 KiB
C#
Raw Normal View History

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} {e.PhisicalPerson.Patronymic} "
})
.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} {e.PhisicalPerson.Patronymic}",
})
.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,
2024-12-01 15:27:38 +04:00
PhysicalPersonName = entity.PhisicalPerson != null
? $"{entity.PhisicalPerson.Surname} {entity.PhisicalPerson.Name} {entity.PhisicalPerson.Patronymic} "
2024-12-01 15:27:38 +04:00
: "Не указано" // Обработка отсутствующего физического лица
};
}
2024-12-01 15:27:38 +04:00
// Добавить нового сотрудника
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)
{
2024-12-09 23:59:28 +04:00
// Убедимся, что связи остаются нетронутыми
entity.PhisicalPersonsId = null; // Удаляем связь, если это нужно
context.Employees.Remove(entity);
context.SaveChanges();
}
}
2024-12-09 23:59:28 +04:00
}
}