using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using WpfAppUI.Data; namespace WpfAppUI.Model { /// /// Класс реализует функционал запросов для бд /// public static class DBRequests { //Все рабочие public static ICollection AllDataRequest() { ApplicationContextUI db = new ApplicationContextUI(); return db.Emploees.ToList(); } //Рабочий по ID public static Emploee EmploeeById(int id) { using (ApplicationContextUI db = new ApplicationContextUI()) { return db.Emploees.Where(i => i.id == id).FirstOrDefault(); } } //Все должности public static ICollection PostRequest() { using (ApplicationContextUI db = new ApplicationContextUI()) { return db.Posts.ToList(); } } //Удалить сотрудника public static bool DeleteEmploeeById(int id) { using (ApplicationContextUI db = new ApplicationContextUI()) { var emploee = db.Emploees.Where(i => i.id == id).FirstOrDefault(); if (emploee != null) { db.Remove(emploee); db.SaveChanges(); return true; } return false; } } //Изменить данные о сотруднике public static void ChangeEmploeeData(int id, string surname, string name, Post post, DateTime birthday, string phone, string mail, string patronymic = null) { ApplicationContextUI db = new ApplicationContextUI(); var emploee = db.Emploees.Where(i => i.id == id).FirstOrDefault(); emploee.Surname = surname; emploee.Name = name; emploee.Post = post; emploee.Birthday = birthday; emploee.Phone = phone; emploee.Mail = mail; emploee.Patronymic = patronymic; db.SaveChanges(); db.Dispose(); } //Новый сотрудник public static bool AddNewEmploee(string surname, string name, Post post, DateTime birthday, string phone, string mail, string patronymic = null) { ApplicationContextUI db = new ApplicationContextUI(); bool chek = false; if (!db.Emploees.Any(el => el.Name == name && el.Surname == surname && el.Mail == mail || el.Birthday == birthday)) { Emploee emploee = new Emploee { Surname = surname, Name = name, Mail = mail, Birthday = birthday, Phone = phone, Post = post, Patronymic = patronymic, }; db.Emploees.Add(emploee); db.SaveChanges(); db.Dispose(); chek = true; } return chek; } } }