2024-11-12 22:10:44 +04:00
|
|
|
|
using EmployeeManagmentContracts.BindingModels;
|
|
|
|
|
using EmployeeManagmentContracts.BusinessLogicContracts;
|
|
|
|
|
using EmployeeManagmentContracts.SearchModels;
|
|
|
|
|
using EmployeeManagmentContracts.ViewModels;
|
2024-11-13 14:41:01 +04:00
|
|
|
|
using EmployeeManagmentDataBaseImplement.Models;
|
|
|
|
|
using EmployeeManagmentDataBaseImplement;
|
2024-11-12 22:10:44 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace EmployeeManagmentBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class PhisicalPersonLogic : IPhisicalPersonLogic
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
private readonly EmployeeManagementDbContext _context;
|
|
|
|
|
|
|
|
|
|
public PhisicalPersonLogic(EmployeeManagementDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 22:10:44 +04:00
|
|
|
|
public void CreateOrUpdate(PhisicalPersonBindingModel model)
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
PhisicalPerson? person = _context.PhysicalPersons
|
|
|
|
|
.FirstOrDefault(p => p.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (person == null)
|
|
|
|
|
{
|
|
|
|
|
// Создание нового
|
|
|
|
|
person = new PhisicalPerson
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Surname = model.Surname,
|
|
|
|
|
Patronymic = model.Patronomic,
|
|
|
|
|
Birthday = model.Birthday,
|
|
|
|
|
Gender = model.Gender,
|
|
|
|
|
Address = model.Address,
|
|
|
|
|
Telephone = model.Telephone
|
|
|
|
|
};
|
|
|
|
|
_context.PhysicalPersons.Add(person);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Обновление существующего
|
|
|
|
|
person.Name = model.Name;
|
|
|
|
|
person.Surname = model.Surname;
|
|
|
|
|
person.Patronymic = model.Patronomic;
|
|
|
|
|
person.Birthday = model.Birthday;
|
|
|
|
|
person.Gender = model.Gender;
|
|
|
|
|
person.Address = model.Address;
|
|
|
|
|
person.Telephone = model.Telephone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.SaveChanges();
|
2024-11-12 22:10:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
var person = _context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
|
|
|
|
|
if (person != null)
|
|
|
|
|
{
|
|
|
|
|
_context.PhysicalPersons.Remove(person);
|
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
}
|
2024-11-12 22:10:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PhisicalPersonViewModel? GetPhisicalPersonById(int id)
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
var person = _context.PhysicalPersons
|
|
|
|
|
.Where(p => p.Id == id)
|
|
|
|
|
.Select(p => new PhisicalPersonViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
Name = p.Name,
|
|
|
|
|
Surname = p.Surname,
|
|
|
|
|
Patronomic = p.Patronymic,
|
|
|
|
|
Birthday = p.Birthday,
|
|
|
|
|
Gender = p.Gender,
|
|
|
|
|
Address = p.Address,
|
|
|
|
|
Telephone = p.Telephone
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
return person;
|
2024-11-12 22:10:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PhisicalPersonViewModel> GetPhisicalPersons(PhisicalPersonSearchModel model)
|
|
|
|
|
{
|
2024-11-13 14:41:01 +04:00
|
|
|
|
var query = _context.PhysicalPersons.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(p => p.Name.Contains(model.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Surname))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(p => p.Surname.Contains(model.Surname));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Patronomic))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(p => p.Patronymic.Contains(model.Patronomic));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
.Select(p => new PhisicalPersonViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = p.Id,
|
|
|
|
|
Name = p.Name,
|
|
|
|
|
Surname = p.Surname,
|
|
|
|
|
Patronomic = p.Patronymic,
|
|
|
|
|
Birthday = p.Birthday,
|
|
|
|
|
Gender = p.Gender,
|
|
|
|
|
Address = p.Address,
|
|
|
|
|
Telephone = p.Telephone
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2024-11-12 22:10:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|