TeachersPageViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using TeacheresAndCourses.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using ReactiveUI;
  5. using System.Linq;
  6. using Microsoft.EntityFrameworkCore;
  7. using System.Diagnostics;
  8. using System.Data;
  9. using MsBox.Avalonia.Enums;
  10. using MsBox.Avalonia;
  11. namespace TeacheresAndCourses.ViewModels
  12. {
  13. public class TeachersPageViewModel : ViewModelBase
  14. {
  15. List<Teacher> teachersList;
  16. public List<Teacher> TeachersList { get => teachersList; set => this.RaiseAndSetIfChanged(ref teachersList, value); }
  17. public TeachersPageViewModel() {
  18. teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation).
  19. Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course).
  20. Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList();
  21. }
  22. public void ToAddingPage()
  23. {
  24. MainWindowViewModel.Instance.Uc = new AddTeacher();
  25. }
  26. //Ñâîéñòâà äëÿ âûáîðêè ïî ÔÈÎ
  27. string searchLine;
  28. public string SearchLine { get => searchLine; set { searchLine = value; AllSort(); } }
  29. // Ñâîéñòâà äëÿ âûáîðêè ïî ïðåäìåòó
  30. List<Subject> subjectsList = [new Subject() { Subjectid = 0, Title = "Âñå ïðåäìåòû" }, ..MainWindowViewModel.connection.Subjects.ToList()];
  31. public List<Subject> SubjectsList { get => subjectsList; }
  32. Subject selectedSubject = null;
  33. public Subject SelectedSubject {
  34. get {
  35. if (selectedSubject == null) return subjectsList[0];
  36. else return selectedSubject;
  37. }
  38. set {
  39. selectedSubject = value;
  40. }
  41. }
  42. // Ñâîéñòâà äëÿ âûáîðêè ïî ïîëó
  43. int genderSort = 0;
  44. public int GenderSort { get => genderSort; set { genderSort = value; AllSort(); } }
  45. // Ñâîéñòâà äëÿ âûáîðêè ïî ïîëó
  46. int phoneNumSort = 0;
  47. public int PhoneNumSort { get => phoneNumSort; set { phoneNumSort = value; } }
  48. // Ñâîéñòâà äëÿ âûáîðêè ïî ïîëó
  49. int generalSort = 0;
  50. public int GeneralSort { get => generalSort; set { generalSort = value; } }
  51. void AllSort()
  52. {
  53. teachersList = MainWindowViewModel.connection.Teachers.Include(x => x.GenderNavigation).
  54. Include(x => x.TeachersAndCourses).ThenInclude(x => x.Course).
  55. Include(x => x.TeachersAndSubjects).ThenInclude(x => x.Subject).ToList();
  56. //ïîèñêîâàÿ ñòðîêà
  57. if (!string.IsNullOrWhiteSpace(searchLine))
  58. {
  59. TeachersList = TeachersList.Where(x => x.Lastname.ToLower().Contains(searchLine.ToLower()) || x.Firstname.ToLower().Contains(searchLine.ToLower()) || x.Patronymic.ToLower().Contains(searchLine.ToLower())).ToList();
  60. }
  61. // âûáîðêà ïî ïîëó
  62. if (genderSort==1)
  63. {
  64. TeachersList = TeachersList.Where(x=>x.Gender==1).ToList();
  65. }
  66. else if(genderSort == 2)
  67. {
  68. TeachersList = TeachersList.Where(x => x.Gender == 2).ToList();
  69. }
  70. // âûáîðêà ïî íàëè÷èþ íîìåðà òåëåôîíà
  71. if (phoneNumSort == 1)
  72. {
  73. TeachersList = TeachersList.Where(x => x.PhoneNumber!=null).ToList();
  74. }
  75. else if (phoneNumSort == 2)
  76. {
  77. TeachersList = TeachersList.Where(x => x.PhoneNumber == null).ToList();
  78. }
  79. }
  80. public async void DeleteTeacher(int teacherId)
  81. {
  82. ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Ïðîâåðêà", "Âû õîòèòå óäàëèòü ïîëüçîâàòåëÿ?", ButtonEnum.YesNo).ShowAsync();
  83. switch (checking)
  84. {
  85. case ButtonResult.Yes:
  86. {
  87. Teacher deleteTeacher = MainWindowViewModel.connection.Teachers.First(x => x.Teacherid == teacherId);
  88. MainWindowViewModel.connection.Teachers.Remove(deleteTeacher);
  89. MainWindowViewModel.connection.SaveChanges();
  90. MainWindowViewModel.Instance.Uc = new TeachersPage();
  91. break;
  92. }
  93. case ButtonResult.No:
  94. {
  95. break;
  96. }
  97. }
  98. }
  99. public void EditTeacher(int teacherId)
  100. {
  101. MainWindowViewModel.Instance.Uc = new AddTeacher(teacherId);
  102. }
  103. }
  104. }