AddPageViewModel.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using AvaloniaApplication5.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. using ReactiveUI;
  8. namespace AvaloniaApplication5.ViewModels
  9. {
  10. internal class AddPageViewModel : ViewModelBase
  11. {
  12. Teacher? _newTeacher;
  13. public List<TeacherCourse> TeacherCourses { get => _teacherCourses; set => this.RaiseAndSetIfChanged(ref _teacherCourses, value); }
  14. public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); }
  15. public AddPageViewModel()
  16. {
  17. _newTeacher = new Teacher()
  18. {
  19. Gender = new Gender()
  20. };
  21. }
  22. public AddPageViewModel(int id)
  23. {
  24. _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);
  25. TeacherCourses = NewTeacher.TeacherCourses.ToList();
  26. }
  27. public List<Gender> Genders => MainWindowViewModel.myConnection.Genders.ToList();
  28. public List<Discipline> Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList();
  29. public List<Course> Courses => MainWindowViewModel.myConnection.Courses.ToList();
  30. List<TeacherCourse>? _teacherCourses;
  31. List<TeacherDiscipline>? _teacherDisciplines;
  32. public List<TeacherDiscipline> TeacherDisciplines { get => _teacherDisciplines; set => this.RaiseAndSetIfChanged(ref _teacherDisciplines, value); }
  33. Course? selectedCourse;
  34. Discipline? selectedDiscipline;
  35. public Course? SelectedCourse
  36. {
  37. get => null;
  38. set
  39. {
  40. if(value != null)
  41. {
  42. NewTeacher.TeacherCourses.Add(new TeacherCourse()
  43. {
  44. Teacher = NewTeacher, Course = value
  45. });
  46. TeacherCourses = NewTeacher.TeacherCourses.ToList();
  47. this.RaisePropertyChanged(nameof(Courses));
  48. }
  49. }
  50. }
  51. public Discipline? SelectedDiscipline
  52. {
  53. get => null;
  54. set
  55. {
  56. if (value != null)
  57. {
  58. NewTeacher.TeacherDisciplines.Add(new TeacherDiscipline()
  59. {
  60. Teacher = NewTeacher, Discipline = value
  61. });
  62. TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
  63. this.RaisePropertyChanged(nameof(Disciplines));
  64. }
  65. }
  66. }
  67. public void DeleteDiscipline(int id)
  68. {
  69. NewTeacher.TeacherDisciplines.Remove(NewTeacher.TeacherDisciplines.FirstOrDefault(x => x.Id == id));
  70. TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
  71. this.RaisePropertyChanged(nameof(Disciplines));
  72. }
  73. public void AddTeacher()
  74. {
  75. if(NewTeacher.Id == 0)
  76. {
  77. MainWindowViewModel.myConnection.Teachers.Add(NewTeacher);
  78. }
  79. MainWindowViewModel.myConnection.SaveChanges();
  80. }
  81. }
  82. }