using System; using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using MsBox.Avalonia; using MsBox.Avalonia.Enums; using ReactiveUI; using TeacheresAndCourses.Models; namespace TeacheresAndCourses.ViewModels { public class AddTeacherViewModel : ViewModelBase { string buttonText; public string ButtonText { get => buttonText; set => this.RaiseAndSetIfChanged(ref buttonText, value); } Teacher? newTeacher; public Teacher? NewTeacher { get => newTeacher; set => this.RaiseAndSetIfChanged(ref newTeacher, value); } public List? teacSubjectsList; public List? TeacSubjectsList { get => teacSubjectsList; set => this.RaiseAndSetIfChanged(ref teacSubjectsList, value); } public List? teacCoursesList; public List? TeacCoursesList { get => teacCoursesList; set => this.RaiseAndSetIfChanged(ref teacCoursesList, value); } public AddTeacherViewModel() { newTeacher = new Teacher() { GenderNavigation = new Gender() }; ButtonText = "Добавить"; } public AddTeacherViewModel(int teacherId) { newTeacher = MainWindowViewModel.connection.Teachers.Include(x=>x.GenderNavigation).Include(x=>x.TeachersAndCourses).ThenInclude(x=>x.Course). Include(x=>x.TeachersAndSubjects).ThenInclude(x=>x.Subject).FirstOrDefault(x=>x.Teacherid== teacherId); TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList(); TeacCoursesList = NewTeacher.TeachersAndCourses.ToList(); ButtonText = "Сохранить изменения"; } public List courseList => MainWindowViewModel.connection.Courses.ToList().Except(newTeacher.TeachersAndCourses.Select(x => x.Course)).ToList(); public List subjectList => MainWindowViewModel.connection.Subjects.ToList().Except(newTeacher.TeachersAndSubjects.Select(x=>x.Subject)).ToList(); public List genderList => MainWindowViewModel.connection.Genders.ToList(); // Добавление выбранного предмета в список Subject? newSub; public Subject? NewSub { get => null; set { if (value!=null) { NewTeacher.TeachersAndSubjects.Add(new TeachersAndSubject() { Teacher = NewTeacher, Subject = value }); TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList(); this.RaisePropertyChanged(nameof(subjectList)); } } } // Добавление выбранного курса в список Course? newCou; public Course? NewCou { get => null; set { if (value != null) { NewTeacher.TeachersAndCourses.Add(new TeachersAndCourse() { Teacher = NewTeacher, Course = value }); TeacCoursesList = NewTeacher.TeachersAndCourses.ToList(); this.RaisePropertyChanged(nameof(courseList)); } } } // Удаление предмета из списка с добавленными предметами public void DeleteSub(TeachersAndSubject deleteSub) { NewTeacher.TeachersAndSubjects.Remove(deleteSub); TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList(); this.RaisePropertyChanged(nameof(subjectList)); } // Удаление курса из списка с добавленными курсами public void DeleteCou(TeachersAndCourse deleteCou) { NewTeacher.TeachersAndCourses.Remove(deleteCou); TeacCoursesList = NewTeacher.TeachersAndCourses.ToList(); this.RaisePropertyChanged(nameof(courseList)); } public void ToMainPage() { MainWindowViewModel.Instance.Uc = new TeachersPage(); } public void ToAddCoursePage() { MainWindowViewModel.Instance.Uc = new AddCourse(); } public async void AddTeacher() { ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Проверка действия", "Вы хотите сохранить данные?", ButtonEnum.YesNo).ShowAsync(); switch (checking) { case ButtonResult.Yes: { if (NewTeacher.Teacherid == 0) { MainWindowViewModel.connection.Teachers.Add(NewTeacher); } MainWindowViewModel.connection.SaveChanges(); MainWindowViewModel.Instance.Uc = new TeachersPage(); break; } case ButtonResult.No: { break; } } } } }