using EmployeeManager.Model.Data; using System.Data.Common; using System.Linq; namespace EmployeeManager.Model { public static class DataWorker { public static string CreatePhysicalPerson(string namePhysicalPersons, string surnamePhysicalPersons, string patronomicPhysicalPersons, DateTime birthday, string gender, string address, string telephone) { string result = "Возникла ошибка. Обратитесь в ЛАОД."; using(ApplicationConext db = new ApplicationConext()) { bool checkIsExist = db.PhysicalPersons.Any( e => e.NamePhysicalPersons == namePhysicalPersons && e.SurnamePhysicalPersons == surnamePhysicalPersons && e.PatronomicPhysicalPersons == patronomicPhysicalPersons && e.Birthday == birthday ); if (!checkIsExist) { PhysicalPerson newPhysicalPerson = new PhysicalPerson { NamePhysicalPersons = namePhysicalPersons, SurnamePhysicalPersons = surnamePhysicalPersons, PatronomicPhysicalPersons = patronomicPhysicalPersons, Birthday = birthday, Gender = gender, Address = address, Telephone = telephone }; db.PhysicalPersons.Add(newPhysicalPerson); db.SaveChanges(); result = "Физическое лицо добавлено."; } return result; } } public static string CreateEmployee(string nameJob, DateTime startJob, DateTime? endJob, string? partTimeJob, float bid, PhysicalPerson? physicalPersons) { string result = "Возникла ошибка. Обратитесь в ЛАОД."; using (ApplicationConext db = new ApplicationConext()) { Employee newEmployee = new Employee { NameJob = nameJob, StartJob = startJob, EndJob = endJob, PartTimeJob = partTimeJob, Bid = bid, PhysicalPersonsId = physicalPersons.Id }; db.Employees.Add(newEmployee); db.SaveChanges(); result = "Работник добавлен."; return result; } } public static string DeletePhysicalPerson(PhysicalPerson physicalPerson) { string result = "Возникла ошибка. Обратитесь в ЛАОД."; using(ApplicationConext db = new ApplicationConext()) { db.PhysicalPersons.Remove(physicalPerson); db.SaveChanges(); result = "Физическое лицо удалено."; } return result; } public static string EditPhysicalPerson(PhysicalPerson oldPhysicalPerson, string newNamePhysicalPersons, string newSurnamePhysicalPersons, string newPatronomicPhysicalPersons, DateTime newBirthday, string newGender, string newAddress, string newTelephone) { string result = "Возникла ошибка. Обратитесь в ЛАОД."; using (ApplicationConext db = new ApplicationConext()) { PhysicalPerson physicalPerson = db.PhysicalPersons.FirstOrDefault(e => e.Id == oldPhysicalPerson.Id); physicalPerson.SurnamePhysicalPersons = newSurnamePhysicalPersons; physicalPerson.NamePhysicalPersons = newNamePhysicalPersons; physicalPerson.PatronomicPhysicalPersons = newPatronomicPhysicalPersons; physicalPerson.Birthday = newBirthday; physicalPerson.Gender = newGender; physicalPerson.Address = newAddress; physicalPerson.Telephone = newTelephone; } return result; } public static List GetAllPhysicalPerson() { using (ApplicationConext db = new ApplicationConext()) { var result = db.PhysicalPersons.ToList(); return result; } } } }