AddPageViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. using System.Text;
  9. using System.Threading.Tasks;
  10. using MsBox.Avalonia.Enums;
  11. using MsBox.Avalonia;
  12. namespace AvaloniaApplication5.ViewModels
  13. {
  14. internal class AddPageViewModel : ViewModelBase
  15. {
  16. Teacher? _newTeacher;
  17. public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); }
  18. public List<TeacherCourse> TeacherCourses { get => _teacherCourses; set => this.RaiseAndSetIfChanged(ref _teacherCourses, value); }
  19. public AddPageViewModel()
  20. {
  21. _newTeacher = new Teacher()
  22. {
  23. Gender = new Gender()
  24. };
  25. TextButton = "Äîáàâèòü";
  26. }
  27. string text_button = "";
  28. public string TextButton
  29. {
  30. get { return text_button; }
  31. set { this.RaiseAndSetIfChanged(ref text_button, value); }
  32. }
  33. public AddPageViewModel(int idt)
  34. {
  35. _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 == idt);
  36. TeacherCourses = NewTeacher.TeacherCourses.ToList();
  37. TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
  38. TextButton = "Ñîõðàíèòü èçìåíåíèÿ";
  39. }
  40. public List<Gender> Genders => MainWindowViewModel.myConnection.Genders.ToList();
  41. public List<Discipline> Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList().Except(_newTeacher.TeacherDisciplines.Select(x =>x.Discipline)).ToList();
  42. public List<Course> Courses => MainWindowViewModel.myConnection.Courses.ToList().Except(_newTeacher.TeacherCourses.Select(x => x.Course)).ToList();
  43. public List<TeacherDiscipline> TeacherDisciplines { get => _teacherDisciplines; set => this.RaiseAndSetIfChanged(ref _teacherDisciplines, value); }
  44. List<TeacherCourse>? _teacherCourses;
  45. List<TeacherDiscipline>? _teacherDisciplines;
  46. Course? selectedCourse;
  47. Discipline? selectedDiscipline;
  48. public Course? SelectedCourse
  49. {
  50. get => null;
  51. set
  52. {
  53. if(value != null)
  54. {
  55. NewTeacher.TeacherCourses.Add(new TeacherCourse()
  56. {
  57. Teacher = NewTeacher, Course = value
  58. });
  59. TeacherCourses = NewTeacher.TeacherCourses.ToList();
  60. this.RaisePropertyChanged(nameof(Courses));
  61. }
  62. }
  63. }
  64. public Discipline? SelectedDiscipline
  65. {
  66. get => null;
  67. set
  68. {
  69. if (value != null)
  70. {
  71. NewTeacher.TeacherDisciplines.Add(new TeacherDiscipline()
  72. {
  73. Teacher = NewTeacher, Discipline = value
  74. });
  75. TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
  76. this.RaisePropertyChanged(nameof(Disciplines));
  77. }
  78. }
  79. }
  80. public void DeleteDiscipline(TeacherDiscipline teacherDiscipline)
  81. {
  82. NewTeacher.TeacherDisciplines.Remove(teacherDiscipline);
  83. TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
  84. this.RaisePropertyChanged(nameof(Disciplines));
  85. }
  86. public void DeleteCourse(TeacherCourse teacherCourse)
  87. {
  88. NewTeacher.TeacherCourses.Remove(teacherCourse);
  89. TeacherCourses = NewTeacher.TeacherCourses.ToList();
  90. this.RaisePropertyChanged(nameof(Courses));
  91. }
  92. public async void AddTeacher()
  93. {
  94. if (NewTeacher.Id == 0 && TextButton == "Äîáàâèòü")
  95. {
  96. ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Ïîäòâåðæäåíèå äîáàâëåíèÿ",
  97. "Ïîäòâåðäèòü äîáàâëåíèå ó÷èòåëÿ?", ButtonEnum.YesNo).ShowAsync();
  98. if (result == ButtonResult.Yes)
  99. {
  100. MainWindowViewModel.myConnection.Teachers.Add(NewTeacher);
  101. await MessageBoxManager.GetMessageBoxStandard("Ñîîáùåíèå", "Ó÷èòåëü äîáàâëåí", ButtonEnum.Ok).ShowAsync();
  102. }
  103. else
  104. {
  105. await MessageBoxManager.GetMessageBoxStandard("Îòìåíà", "Äîáàâëåíèå îòìåíåî", ButtonEnum.Ok).ShowAsync();
  106. }
  107. }
  108. else {
  109. ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Ïîäòâåðæäåíèå ðåäàêòèðîâàíèÿ",
  110. "Ïîäòâåðäèòü ðåäàêòèðîâàíèå ó÷èòåëÿ?", ButtonEnum.YesNo).ShowAsync();
  111. if (result == ButtonResult.Yes)
  112. {
  113. await MessageBoxManager.GetMessageBoxStandard("Ñîîáùåíèå", "Ó÷èòåëü îòðåäàêòèðîâàí", ButtonEnum.Ok).ShowAsync();
  114. }
  115. else
  116. {
  117. await MessageBoxManager.GetMessageBoxStandard("Îòìåíà", "Ðåäàêòèðîâàíèå îòìåíåíî", ButtonEnum.Ok).ShowAsync();
  118. }
  119. }
  120. MainWindowViewModel.myConnection.SaveChanges();
  121. MainWindowViewModel.Instance.PageContent = new Show();
  122. }
  123. public void ToAddDisCurs()
  124. {
  125. MainWindowViewModel.Instance.PageContent = new AddDisCurs();
  126. }
  127. string _message = "";
  128. public string Message
  129. {
  130. get => _message;
  131. set => this.RaiseAndSetIfChanged(ref _message, value);
  132. }
  133. }
  134. }