using Avalonia; using Avalonia.Controls; <<<<<<< HEAD using Avalonia.Threading; ======= >>>>>>> cb3f32a974d3f3dd8a95538b44505c7803821611 using Microsoft.EntityFrameworkCore; using MsBox.Avalonia; using MsBox.Avalonia.Enums; using ReactiveUI; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using Teachers.Models; using Teachers.Views; namespace Teachers.ViewModels { internal class AddTeacherViewModel : ViewModelBase { //создаем нового учителя для дальнейшего добавления в бд Teacher? _newTeacher; public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); } string _buttonText; public string ButtonText { get => _buttonText; set => this.RaiseAndSetIfChanged(ref _buttonText, value); } public AddTeacherViewModel() { _newTeacher = new Teacher() { GenderNavigation = new Gender() }; _newCourse = new Course(); <<<<<<< HEAD ======= _courseWindow = new Window(); >>>>>>> cb3f32a974d3f3dd8a95538b44505c7803821611 _buttonText = "Добавить"; } public AddTeacherViewModel(int idTeacher) { _newTeacher = MainWindowViewModel.myConnection.Teachers .Include(x => x.GenderNavigation) .Include(x => x.TeacherCourses).ThenInclude(x => x.IdCourseNavigation) .Include(x => x.SubjectsTaughts).ThenInclude(x => x.IdSubjectNavigation).FirstOrDefault(x => x.Id == idTeacher); _newCourse = new Course(); <<<<<<< HEAD ======= _courseWindow = new Window(); >>>>>>> cb3f32a974d3f3dd8a95538b44505c7803821611 CoursesTable = NewTeacher.TeacherCourses.ToList(); SubjectsTable = NewTeacher.SubjectsTaughts.ToList(); _buttonText = "Сохранить изменения"; } #region Comboboxes //поля и свойства для работы со связью М:М List _coursesTable; List _subjectsTable; public List CoursesTable { get => _coursesTable; set => this.RaiseAndSetIfChanged(ref _coursesTable, value); } public List SubjectsTable { get => _subjectsTable; set => this.RaiseAndSetIfChanged(ref _subjectsTable, value); } //листы для отображения в комбобоксах (там где М:М в листы входят только те элементы, которых нету в листах-выбранных-значений //List _gendersL = [new Gender() { Id = 0, Gender1 = "Выберите пол" }, .. MainWindowViewModel.myConnection.Genders.ToList()]; List _coursesL = [new Course() { Id = 0, Name = "Выберите курс" }, .. MainWindowViewModel.myConnection.Courses.ToList()]; List _subjectL = [new Subject() { Id = 0, Name = "Выберите предмет" }, .. MainWindowViewModel.myConnection.Subjects.ToList()]; //public List GendersL => _gendersL; public List GendersL => MainWindowViewModel.myConnection.Genders.ToList(); public List CoursesL { get => _coursesL .Except(_newTeacher.TeacherCourses.Select(x => x.IdCourseNavigation)).ToList(); set { _coursesL = value; this.RaisePropertyChanged(nameof(CoursesL)); } } public List SubjectsL => _subjectL .Except(_newTeacher.SubjectsTaughts.Select(x => x.IdSubjectNavigation)).ToList(); //свойства для добавления выбранных элементов в другой лист и удаления из исходного //Gender? selectedGender; //public Gender? SelectedGender //{ // get // { // if (selectedGender == null) // { // return _gendersL[0]; // } // else // { // return selectedGender; // } // //return selectedGender; // } // set // { // if (value != null) // { // NewTeacher.Gender = value.Id; // } // } //} Course? selectedCourse; public Course? SelectedCourse { get { if (selectedCourse == null) { return _coursesL[0]; } else { return selectedCourse; } } set { if (value != null) { NewTeacher.TeacherCourses.Add (new TeacherCourse() { IdTeacherNavigation = NewTeacher, IdCourseNavigation = value }); CoursesTable = NewTeacher.TeacherCourses.ToList(); this.RaisePropertyChanged(nameof(CoursesL)); } } } Subject? selectedSubject; public Subject? SelectedSubject { get { if (selectedSubject == null) { return _subjectL[0]; } else { return selectedSubject; } } set { if (value != null) { NewTeacher.SubjectsTaughts.Add (new SubjectsTaught() { IdTeacherNavigation = NewTeacher, IdSubjectNavigation = value }); SubjectsTable = NewTeacher.SubjectsTaughts.ToList(); this.RaisePropertyChanged(nameof(SubjectsL)); } } } //методы для удаления выбранных курсов и предметов (М:М) public void DeleteCourse(TeacherCourse courseForDelete) { NewTeacher.TeacherCourses.Remove(courseForDelete); CoursesTable = NewTeacher.TeacherCourses.ToList(); //обновляем лист с выбранными значениями this.RaisePropertyChanged(nameof(CoursesL)); //обновляем лист с исходными значениями } public void DeleteSubject(SubjectsTaught subjectForDelete) { NewTeacher.SubjectsTaughts.Remove(subjectForDelete); SubjectsTable = NewTeacher.SubjectsTaughts.ToList(); //обновляем лист с выбранными значениями this.RaisePropertyChanged(nameof(SubjectsL)); //обновляем лист с исходными значениями } #endregion public void AddTeacher() { try { if (NewTeacher.Id == 0) //если текущего учителя не существует, т.е. в объекте newTeacher его Id равен 0, мы добавляем нового { MainWindowViewModel.myConnection.Teachers.Add(NewTeacher); } MainWindowViewModel.myConnection.SaveChanges(); MessageBoxManager.GetMessageBoxStandard("Успех", "Учитель успешно добавлен!", MsBox.Avalonia.Enums.ButtonEnum.Ok); MainWindowViewModel.Instance.Us = new TeachersView(); } catch (Exception ex) { MessageBoxManager.GetMessageBoxStandard("Внимание", ex.Message + "\n" + ex.StackTrace, MsBox.Avalonia.Enums.ButtonEnum.Ok).ShowAsync(); } } public void ToTeachersView() { MainWindowViewModel.Instance.Us = new TeachersView(); } Course? _newCourse; public Course? NewCourse { get => _newCourse; set => this.RaiseAndSetIfChanged(ref _newCourse, value); } <<<<<<< HEAD ======= Window _courseWindow; public Window CourseWindow { get => _courseWindow; set => this.RaiseAndSetIfChanged(ref _courseWindow, value); } bool rubilnik = true; >>>>>>> cb3f32a974d3f3dd8a95538b44505c7803821611 public async Task ShowAddCourse() { var window = new Window(); window.Width = 400; window.Height = 225; <<<<<<< HEAD window.Content = new AddCourseView(); window.Show(); } public void UpdateList() { //_coursesL = [new Course() { Id = 0, Name = "Выберите курс" }, .. MainWindowViewModel.myConnection.Courses.ToList()]; CoursesL = _coursesL .Except(_newTeacher.TeacherCourses.Select(x => x.IdCourseNavigation)).ToList(); this.RaisePropertyChanged(nameof(CoursesL)); Dispatcher.UIThread.Post(() => { this.RaisePropertyChanged(nameof(CoursesL)); }); AddTeacherView atv = new(); atv.UpdatePage(); //this.RaisePropertyChanged(nameof(_coursesL)); //Teacher? buf = _newTeacher; //MainWindowViewModel.Instance.Us = new AddTeacherView(); //_newTeacher = buf; //NewTeacher = buf; ======= //CourseWindow.Content = new AddCourseView(); window.Content = new AddCourseView(); window.Show(); //if (result) //{ //} //CourseWindow.Show(); //window.DataContext = new AddTeacherViewModel { CourseWindow = window }; //_courseWindow = window; //if (rubilnik) //{ // window.Show(); // window.Content = new AddCourseView(); //} //else window.Close(); } public void CloseWindow() { CourseWindow.Close(); >>>>>>> cb3f32a974d3f3dd8a95538b44505c7803821611 } } }