124 lines
4.0 KiB
C#
Raw Normal View History

using EmployeeManagmentContracts.BindingModels;
using EmployeeManagmentContracts.BusinessLogicContracts;
using EmployeeManagmentContracts.SearchModels;
using EmployeeManagmentContracts.ViewModels;
using EmployeeManagmentDataBaseImplement.Models;
using EmployeeManagmentDataBaseImplement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeManagmentBusinessLogic.BusinessLogic
{
public class PhisicalPersonLogic : IPhisicalPersonLogic
{
private readonly EmployeeManagementDbContext _context;
public PhisicalPersonLogic(EmployeeManagementDbContext context)
{
_context = context;
}
public void CreateOrUpdate(PhisicalPersonBindingModel model)
{
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();
}
public void Delete(int id)
{
var person = _context.PhysicalPersons.FirstOrDefault(p => p.Id == id);
if (person != null)
{
_context.PhysicalPersons.Remove(person);
_context.SaveChanges();
}
}
public PhisicalPersonViewModel? GetPhisicalPersonById(int id)
{
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;
}
public List<PhisicalPersonViewModel> GetPhisicalPersons(PhisicalPersonSearchModel model)
{
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();
}
}
}