using AvaloniaBaseTeacher.Models; using Microsoft.EntityFrameworkCore; using MsBox.Avalonia; using MsBox.Avalonia.Enums; using ReactiveUI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AvaloniaBaseTeacher.ViewModels { internal class add : ViewModelBase { public string ButtonName; public string message; public string message1; public string Button { get => ButtonName; set => this.RaiseAndSetIfChanged(ref ButtonName, value); } public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } public string Message1 { get => message1; set => this.RaiseAndSetIfChanged(ref message1, value); } Teacher? _newTeacher; public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); } public add() { _newTeacher = new Teacher() { GenderNavigation = new Gender() }; ButtonName = "Добавить"; Message = "Вы точно хотите добавить пользователя?"; Message1 = "Пользователь добавлен"; } public add(int TeacherId) { _newTeacher = MainWindowViewModel.myConnection.Teachers.Include(x => x.GenderNavigation).Include(x => x.Course1Teachers).ThenInclude(x => x.IdCourseNavigation).Include(x => x.Discipline1Teachers).ThenInclude(x => x.IdDisciplineNavigation).FirstOrDefault(x => x.IdTeacher == TeacherId); Course1Teachers = NewTeacher.Course1Teachers.ToList(); Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList(); ButtonName = "Сохранить изменения"; Message = "Вы точно хотите изменить?"; Message1 = "Пользователь изменен"; } public List genders => MainWindowViewModel.myConnection.Genders.ToList(); public List courses => MainWindowViewModel.myConnection.Courses.ToList().Except(_newTeacher.Course1Teachers.Select(x => x.IdCourseNavigation)).ToList(); public List disciplines => MainWindowViewModel.myConnection.Disciplines.ToList().Except(_newTeacher.Discipline1Teachers.Select(x=>x.IdDisciplineNavigation)).ToList(); List? _Course1Teach; public List? Course1Teachers { get => _Course1Teach; set=>this.RaiseAndSetIfChanged(ref _Course1Teach, value); } List? _Discipline1Teac; public List? Discipline1Teachers { get => _Discipline1Teac; set => this.RaiseAndSetIfChanged(ref _Discipline1Teac, value); } public Course? SelectedCourse { get => null; set { if(value != null) { NewTeacher.Course1Teachers.Add(new Course1Teacher() {IdTeacherNavigation = NewTeacher, IdCourseNavigation = value}); Course1Teachers = NewTeacher.Course1Teachers.ToList(); this.RaisePropertyChanged(nameof(courses)); } } } public Discipline? SelectedDiscipline { get => null; set { if (value != null) { NewTeacher.Discipline1Teachers.Add(new Discipline1Teacher() { IdTeacherNavigation = NewTeacher, IdDisciplineNavigation = value }); Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList(); this.RaisePropertyChanged(nameof(disciplines)); } } } public void DeleteCourse(int id) { NewTeacher.Course1Teachers.Remove(NewTeacher.Course1Teachers.FirstOrDefault(x => x.Id == id)); Course1Teachers = NewTeacher.Course1Teachers.ToList(); this.RaisePropertyChanged(nameof(courses)); } public void DeleteDiscipline(int id) { NewTeacher.Discipline1Teachers.Remove(NewTeacher.Discipline1Teachers.FirstOrDefault(x => x.Id == id)); Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList(); this.RaisePropertyChanged(nameof(disciplines)); } public async Task AddTeacher() { ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Вы уверенны?", Message, ButtonEnum.YesNo).ShowAsync(); if (result == ButtonResult.Yes) { if (NewTeacher.IdTeacher == 0) { MainWindowViewModel.myConnection.Teachers.Add(NewTeacher); } MainWindowViewModel.myConnection.SaveChanges(); MainWindowViewModel.Instance.PageContent = new Show(); ButtonResult result1 = await MessageBoxManager.GetMessageBoxStandard("Успешно", Message1, ButtonEnum.Ok).ShowAsync(); } else { } } public void AddCoursePage() { MainWindowViewModel.Instance.PageContent = new AddCourse(); } } }