using System; using System.Collections.Generic; using System.Linq; using System.Data; using AvaloniaApplication5.Models; using Microsoft.EntityFrameworkCore; using ReactiveUI; namespace AvaloniaApplication5.ViewModels { internal class AddPageViewModel : ViewModelBase { Teacher? _newTeacher; public List TeacherCourses { get => _teacherCourses; set => this.RaiseAndSetIfChanged(ref _teacherCourses, value); } public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); } public AddPageViewModel() { _newTeacher = new Teacher() { Gender = new Gender() }; } public AddPageViewModel(int id) { _newTeacher = MainWindowViewModel.myConnection.Teachers.Include(x=>x.Gender).Include(x=>x.TeacherCourses).ThenInclude(x=>x.Course).Include(x=>x.TeacherDisciplines).ThenInclude(x=>x.Discipline).FirstOrDefault(x=>x.Id == id); TeacherCourses = NewTeacher.TeacherCourses.ToList(); } public List Genders => MainWindowViewModel.myConnection.Genders.ToList(); public List Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList(); public List Courses => MainWindowViewModel.myConnection.Courses.ToList(); List? _teacherCourses; List? _teacherDisciplines; public List TeacherDisciplines { get => _teacherDisciplines; set => this.RaiseAndSetIfChanged(ref _teacherDisciplines, value); } Course? selectedCourse; Discipline? selectedDiscipline; public Course? SelectedCourse { get => null; set { if(value != null) { NewTeacher.TeacherCourses.Add(new TeacherCourse() { Teacher = NewTeacher, Course = value }); TeacherCourses = NewTeacher.TeacherCourses.ToList(); this.RaisePropertyChanged(nameof(Courses)); } } } public Discipline? SelectedDiscipline { get => null; set { if (value != null) { NewTeacher.TeacherDisciplines.Add(new TeacherDiscipline() { Teacher = NewTeacher, Discipline = value }); TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList(); this.RaisePropertyChanged(nameof(Disciplines)); } } } public void DeleteDiscipline(int id) { NewTeacher.TeacherDisciplines.Remove(NewTeacher.TeacherDisciplines.FirstOrDefault(x => x.Id == id)); TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList(); this.RaisePropertyChanged(nameof(Disciplines)); } public void AddTeacher() { if(NewTeacher.Id == 0) { MainWindowViewModel.myConnection.Teachers.Add(NewTeacher); } MainWindowViewModel.myConnection.SaveChanges(); } } }