using TeacheresAndCourses.Models; using System; using System.Collections.Generic; using ReactiveUI; using System.Linq; using Microsoft.EntityFrameworkCore; using System.Diagnostics; using System.Data; using MsBox.Avalonia.Enums; using MsBox.Avalonia; namespace TeacheresAndCourses.ViewModels { public class TeachersPageViewModel : ViewModelBase { List teachersList; public List TeachersList { get => teachersList; set => this.RaiseAndSetIfChanged(ref teachersList, value); } public TeachersPageViewModel() { teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation). Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course). Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList(); } public void ToAddingPage() { MainWindowViewModel.Instance.Uc = new AddTeacher(); } //Свойства для выборки по ФИО string searchLine; public string SearchLine { get => searchLine; set { searchLine = value; AllSort(); } } // Свойства для выборки по предмету List subjectsList = [new Subject() { Subjectid = 0, Title = "Все предметы" }, ..MainWindowViewModel.connection.Subjects.ToList()]; public List SubjectsList { get => subjectsList; } Subject selectedSubject = null; public Subject SelectedSubject { get { if (selectedSubject == null) return subjectsList[0]; else return selectedSubject; } set { selectedSubject = value; } } // Свойства для выборки по полу int genderSort = 0; public int GenderSort { get => genderSort; set { genderSort = value; AllSort(); } } // Свойства для выборки по полу int phoneNumSort = 0; public int PhoneNumSort { get => phoneNumSort; set { phoneNumSort = value; } } // Свойства для выборки по полу int generalSort = 0; public int GeneralSort { get => generalSort; set { generalSort = value; } } void AllSort() { teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation). Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course). Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList(); //поисковая строка if (!string.IsNullOrWhiteSpace(searchLine)) { TeachersList = TeachersList.Where(x => x.Lastname.ToLower().Contains(searchLine.ToLower()) || x.Firstname.ToLower().Contains(searchLine.ToLower()) || x.Patronymic.ToLower().Contains(searchLine.ToLower())).ToList(); } // выборка по полу if (genderSort==1) { TeachersList = TeachersList.Where(x=>x.Gender==1).ToList(); } else if(genderSort == 2) { TeachersList = TeachersList.Where(x => x.Gender == 2).ToList(); } // выборка по наличию номера телефона if (phoneNumSort == 1) { TeachersList = TeachersList.Where(x => x.PhoneNumber!=null).ToList(); } else if (phoneNumSort == 2) { TeachersList = TeachersList.Where(x => x.PhoneNumber == null).ToList(); } } public async void DeleteTeacher(int teacherId) { ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Проверка", "Вы хотите удалить пользователя?", ButtonEnum.YesNo).ShowAsync(); switch (checking) { case ButtonResult.Yes: { Teacher deleteTeacher = MainWindowViewModel.connection.Teachers.First(x => x.Teacherid == teacherId); MainWindowViewModel.connection.Teachers.Remove(deleteTeacher); MainWindowViewModel.connection.SaveChanges(); MainWindowViewModel.Instance.Uc = new TeachersPage(); break; } case ButtonResult.No: { break; } } } public void EditTeacher(int teacherId) { MainWindowViewModel.Instance.Uc = new AddTeacher(teacherId); } } }