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

130 lines
4.6 KiB
C#

using EmployeeManagmentContracts.SearchModels;
using EmployeeManagmentContracts.StoragesContracts;
using EmployeeManagmentContracts.ViewModels;
using EmployeeManagmentDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeManagmentDataBaseImplement.Implements
{
public class PhisicalPersonStorage : IPhisicalPersonStorage
{
// Метод для получения полного списка
public List<PhisicalPersonViewModel> GetFullList()
{
using var context = new EmployeeManagementDbContext();
return context.PhysicalPersons
.Select(p => new PhisicalPersonViewModel
{
Id = p.Id,
Name = p.Name,
Surname = p.Surname,
Patronymic = p.Patronymic,
Birthday = p.Birthday,
Gender = p.Gender,
Address = p.Address,
Telephone = p.Telephone
}).ToList();
}
// Метод для получения отфильтрованного списка
public List<PhisicalPersonViewModel> GetFilteredList(PhisicalPersonSearchModel model)
{
using var context = new EmployeeManagementDbContext();
if (model == null) return new List<PhisicalPersonViewModel>();
return context.PhysicalPersons
.Where(p => p.Surname.Contains(model.Surname ?? string.Empty))
.Select(p => new PhisicalPersonViewModel
{
Id = p.Id,
Name = p.Name,
Surname = p.Surname,
Patronymic = p.Patronymic,
Birthday = p.Birthday,
Gender = p.Gender,
Address = p.Address,
Telephone = p.Telephone
}).ToList();
}
// Метод для получения одного элемента по ID
public PhisicalPersonViewModel? GetElement(int id)
{
using var context = new EmployeeManagementDbContext();
var entity = context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
return entity == null ? null : new PhisicalPersonViewModel
{
Id = entity.Id,
Name = entity.Name,
Surname = entity.Surname,
Patronymic = entity.Patronymic,
Birthday = entity.Birthday,
Gender = entity.Gender,
Address = entity.Address,
Telephone = entity.Telephone
};
}
// Метод для добавления нового физического лица
public void Insert(PhisicalPersonViewModel model)
{
using var context = new EmployeeManagementDbContext();
var newPerson = new PhisicalPerson
{
Name = model.Name,
Surname = model.Surname,
Patronymic = model.Patronymic,
Birthday = model.Birthday,
Gender = model.Gender,
Address = model.Address,
Telephone = model.Telephone
};
context.PhysicalPersons.Add(newPerson);
context.SaveChanges();
}
// Метод для обновления физического лица
public void Update(PhisicalPersonViewModel model)
{
using var context = new EmployeeManagementDbContext();
var entity = context.PhysicalPersons.FirstOrDefault(p => p.Id == model.Id);
if (entity != null)
{
entity.Name = model.Name;
entity.Surname = model.Surname;
entity.Patronymic = model.Patronymic;
entity.Birthday = model.Birthday;
entity.Gender = model.Gender;
entity.Address = model.Address;
entity.Telephone = model.Telephone;
context.SaveChanges();
}
}
// Метод для удаления физического лица
public void Delete(int id)
{
using var context = new EmployeeManagementDbContext();
var entity = context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
if (entity != null)
{
context.PhysicalPersons.Remove(entity);
context.SaveChanges();
}
}
}
}