AddTeacherViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.EntityFrameworkCore;
  5. using MsBox.Avalonia;
  6. using MsBox.Avalonia.Enums;
  7. using ReactiveUI;
  8. using TeacheresAndCourses.Models;
  9. namespace TeacheresAndCourses.ViewModels
  10. {
  11. public class AddTeacherViewModel : ViewModelBase
  12. {
  13. string buttonText;
  14. public string ButtonText { get => buttonText; set => this.RaiseAndSetIfChanged(ref buttonText, value); }
  15. Teacher? newTeacher;
  16. public Teacher? NewTeacher { get => newTeacher; set => this.RaiseAndSetIfChanged(ref newTeacher, value); }
  17. public List<TeachersAndSubject>? teacSubjectsList;
  18. public List<TeachersAndSubject>? TeacSubjectsList { get => teacSubjectsList; set => this.RaiseAndSetIfChanged(ref teacSubjectsList, value); }
  19. public List<TeachersAndCourse>? teacCoursesList;
  20. public List<TeachersAndCourse>? TeacCoursesList { get => teacCoursesList; set => this.RaiseAndSetIfChanged(ref teacCoursesList, value); }
  21. public AddTeacherViewModel() {
  22. newTeacher = new Teacher() { GenderNavigation = new Gender() };
  23. ButtonText = "Äîáàâèòü";
  24. }
  25. public AddTeacherViewModel(int teacherId)
  26. {
  27. newTeacher = MainWindowViewModel.connection.Teachers.Include(x=>x.GenderNavigation).Include(x=>x.TeachersAndCourses).ThenInclude(x=>x.Course).
  28. Include(x=>x.TeachersAndSubjects).ThenInclude(x=>x.Subject).FirstOrDefault(x=>x.Teacherid== teacherId);
  29. TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
  30. TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
  31. ButtonText = "Ñîõðàíèòü èçìåíåíèÿ";
  32. }
  33. public List<Course> courseList => MainWindowViewModel.connection.Courses.ToList().Except(newTeacher.TeachersAndCourses.Select(x => x.Course)).ToList();
  34. public List<Subject> subjectList => MainWindowViewModel.connection.Subjects.ToList().Except(newTeacher.TeachersAndSubjects.Select(x=>x.Subject)).ToList();
  35. public List<Gender> genderList => MainWindowViewModel.connection.Genders.ToList();
  36. // Äîáàâëåíèå âûáðàííîãî ïðåäìåòà â ñïèñîê
  37. Subject? newSub;
  38. public Subject? NewSub { get => null;
  39. set {
  40. if (value!=null)
  41. {
  42. NewTeacher.TeachersAndSubjects.Add(new TeachersAndSubject() { Teacher = NewTeacher, Subject = value });
  43. TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
  44. this.RaisePropertyChanged(nameof(subjectList));
  45. }
  46. } }
  47. // Äîáàâëåíèå âûáðàííîãî êóðñà â ñïèñîê
  48. Course? newCou;
  49. public Course? NewCou
  50. {
  51. get => null;
  52. set
  53. {
  54. if (value != null)
  55. {
  56. NewTeacher.TeachersAndCourses.Add(new TeachersAndCourse() { Teacher = NewTeacher, Course = value });
  57. TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
  58. this.RaisePropertyChanged(nameof(courseList));
  59. }
  60. }
  61. }
  62. // Óäàëåíèå ïðåäìåòà èç ñïèñêà ñ äîáàâëåííûìè ïðåäìåòàìè
  63. public void DeleteSub(TeachersAndSubject deleteSub)
  64. {
  65. NewTeacher.TeachersAndSubjects.Remove(deleteSub);
  66. TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
  67. this.RaisePropertyChanged(nameof(subjectList));
  68. }
  69. // Óäàëåíèå êóðñà èç ñïèñêà ñ äîáàâëåííûìè êóðñàìè
  70. public void DeleteCou(TeachersAndCourse deleteCou)
  71. {
  72. NewTeacher.TeachersAndCourses.Remove(deleteCou);
  73. TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
  74. this.RaisePropertyChanged(nameof(courseList));
  75. }
  76. public void ToMainPage()
  77. {
  78. MainWindowViewModel.Instance.Uc = new TeachersPage();
  79. }
  80. public void ToAddCoursePage()
  81. {
  82. MainWindowViewModel.Instance.Uc = new AddCourse();
  83. }
  84. public async void AddTeacher()
  85. {
  86. ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Ïðîâåðêà äåéñòâèÿ", "Âû õîòèòå ñîõðàíèòü äàííûå?", ButtonEnum.YesNo).ShowAsync();
  87. switch (checking)
  88. {
  89. case ButtonResult.Yes:
  90. {
  91. if (NewTeacher.Teacherid == 0)
  92. {
  93. MainWindowViewModel.connection.Teachers.Add(NewTeacher);
  94. }
  95. MainWindowViewModel.connection.SaveChanges();
  96. MainWindowViewModel.Instance.Uc = new TeachersPage();
  97. break;
  98. }
  99. case ButtonResult.No:
  100. {
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. }