123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- 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<TeacherCourse> _coursesTable;
- List<SubjectsTaught> _subjectsTable;
- public List<TeacherCourse> CoursesTable { get => _coursesTable; set => this.RaiseAndSetIfChanged(ref _coursesTable, value); }
- public List<SubjectsTaught> SubjectsTable { get => _subjectsTable; set => this.RaiseAndSetIfChanged(ref _subjectsTable, value); }
- //листы для отображения в комбобоксах (там где М:М в листы входят только те элементы, которых нету в листах-выбранных-значений
- //List<Gender> _gendersL = [new Gender() { Id = 0, Gender1 = "Выберите пол" }, .. MainWindowViewModel.myConnection.Genders.ToList()];
- List<Course> _coursesL = [new Course() { Id = 0, Name = "Выберите курс" }, .. MainWindowViewModel.myConnection.Courses.ToList()];
- List<Subject> _subjectL = [new Subject() { Id = 0, Name = "Выберите предмет" }, .. MainWindowViewModel.myConnection.Subjects.ToList()];
- //public List<Gender> GendersL => _gendersL;
- public List<Gender> GendersL => MainWindowViewModel.myConnection.Genders.ToList();
- public List<Course> CoursesL
- {
- get => _coursesL
- .Except(_newTeacher.TeacherCourses.Select(x => x.IdCourseNavigation)).ToList();
- set
- {
- _coursesL = value;
- this.RaisePropertyChanged(nameof(CoursesL));
- }
- }
- public List<Subject> 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
- }
- }
- }
|