add.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using AvaloniaBaseTeacher.Models;
  2. using Microsoft.EntityFrameworkCore;
  3. using MsBox.Avalonia;
  4. using MsBox.Avalonia.Enums;
  5. using ReactiveUI;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace AvaloniaBaseTeacher.ViewModels
  12. {
  13. internal class add : ViewModelBase
  14. {
  15. public string ButtonName;
  16. public string message;
  17. public string message1;
  18. public string Button
  19. {
  20. get => ButtonName;
  21. set => this.RaiseAndSetIfChanged(ref ButtonName, value);
  22. }
  23. public string Message
  24. {
  25. get => message;
  26. set => this.RaiseAndSetIfChanged(ref message, value);
  27. }
  28. public string Message1
  29. {
  30. get => message1;
  31. set => this.RaiseAndSetIfChanged(ref message1, value);
  32. }
  33. Teacher? _newTeacher;
  34. public Teacher? NewTeacher
  35. {
  36. get => _newTeacher;
  37. set => this.RaiseAndSetIfChanged(ref _newTeacher, value);
  38. }
  39. public add()
  40. {
  41. _newTeacher = new Teacher() { GenderNavigation = new Gender() };
  42. ButtonName = "Добавить";
  43. Message = "Вы точно хотите добавить пользователя?";
  44. Message1 = "Пользователь добавлен";
  45. }
  46. public add(int TeacherId)
  47. {
  48. _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);
  49. Course1Teachers = NewTeacher.Course1Teachers.ToList();
  50. Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList();
  51. ButtonName = "Сохранить изменения";
  52. Message = "Вы точно хотите изменить?";
  53. Message1 = "Пользователь изменен";
  54. }
  55. public List<Gender> genders => MainWindowViewModel.myConnection.Genders.ToList();
  56. public List<Course> courses => MainWindowViewModel.myConnection.Courses.ToList().Except(_newTeacher.Course1Teachers.Select(x => x.IdCourseNavigation)).ToList();
  57. public List<Discipline> disciplines => MainWindowViewModel.myConnection.Disciplines.ToList().Except(_newTeacher.Discipline1Teachers.Select(x=>x.IdDisciplineNavigation)).ToList();
  58. List<Course1Teacher>? _Course1Teach;
  59. public List<Course1Teacher>? Course1Teachers
  60. {
  61. get => _Course1Teach;
  62. set=>this.RaiseAndSetIfChanged(ref _Course1Teach, value);
  63. }
  64. List<Discipline1Teacher>? _Discipline1Teac;
  65. public List<Discipline1Teacher>? Discipline1Teachers
  66. {
  67. get => _Discipline1Teac;
  68. set => this.RaiseAndSetIfChanged(ref _Discipline1Teac, value);
  69. }
  70. public Course? SelectedCourse
  71. {
  72. get => null;
  73. set
  74. {
  75. if(value != null)
  76. {
  77. NewTeacher.Course1Teachers.Add(new Course1Teacher() {IdTeacherNavigation = NewTeacher, IdCourseNavigation = value});
  78. Course1Teachers = NewTeacher.Course1Teachers.ToList();
  79. this.RaisePropertyChanged(nameof(courses));
  80. }
  81. }
  82. }
  83. public Discipline? SelectedDiscipline
  84. {
  85. get => null;
  86. set
  87. {
  88. if (value != null)
  89. {
  90. NewTeacher.Discipline1Teachers.Add(new Discipline1Teacher() { IdTeacherNavigation = NewTeacher, IdDisciplineNavigation = value });
  91. Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList();
  92. this.RaisePropertyChanged(nameof(disciplines));
  93. }
  94. }
  95. }
  96. public void DeleteCourse(int id)
  97. {
  98. NewTeacher.Course1Teachers.Remove(NewTeacher.Course1Teachers.FirstOrDefault(x => x.Id == id));
  99. Course1Teachers = NewTeacher.Course1Teachers.ToList();
  100. this.RaisePropertyChanged(nameof(courses));
  101. }
  102. public void DeleteDiscipline(int id)
  103. {
  104. NewTeacher.Discipline1Teachers.Remove(NewTeacher.Discipline1Teachers.FirstOrDefault(x => x.Id == id));
  105. Discipline1Teachers = NewTeacher.Discipline1Teachers.ToList();
  106. this.RaisePropertyChanged(nameof(disciplines));
  107. }
  108. public async Task AddTeacher()
  109. {
  110. ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Вы уверенны?", Message, ButtonEnum.YesNo).ShowAsync();
  111. if (result == ButtonResult.Yes)
  112. {
  113. if (NewTeacher.IdTeacher == 0)
  114. {
  115. MainWindowViewModel.myConnection.Teachers.Add(NewTeacher);
  116. }
  117. MainWindowViewModel.myConnection.SaveChanges();
  118. MainWindowViewModel.Instance.PageContent = new Show();
  119. ButtonResult result1 = await MessageBoxManager.GetMessageBoxStandard("Успешно", Message1, ButtonEnum.Ok).ShowAsync();
  120. }
  121. else { }
  122. }
  123. public void AddCoursePage()
  124. {
  125. MainWindowViewModel.Instance.PageContent = new AddCourse();
  126. }
  127. }
  128. }