12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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<TeacherCourse> 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<Gender> Genders => MainWindowViewModel.myConnection.Genders.ToList();
- public List<Discipline> Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList();
- public List<Course> Courses => MainWindowViewModel.myConnection.Courses.ToList();
- List<TeacherCourse>? _teacherCourses;
- List<TeacherDiscipline>? _teacherDisciplines;
- public List<TeacherDiscipline> 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();
- }
- }
- }
|