TaskMainViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using CommunityToolkit.Mvvm.ComponentModel;
  8. using DynamicData;
  9. using EntranseTesting.Models;
  10. using Microsoft.EntityFrameworkCore;
  11. using MsBox.Avalonia.Enums;
  12. using MsBox.Avalonia;
  13. using ReactiveUI;
  14. namespace EntranseTesting.ViewModels
  15. {
  16. public partial class TaskMainViewModel : ObservableObject, INotifyPropertyChanged
  17. {
  18. public event PropertyChangedEventHandler PropertyChanged;
  19. protected virtual void OnPropertyChanged(string propertyName)
  20. {
  21. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  22. }
  23. string? searchLine = "";
  24. bool selectedInTest = true;
  25. string selectedCategory = "Âñå òèïû çàäàíèé";
  26. string selectedSort = "Áåç ñîðòèðîâêè";
  27. [ObservableProperty] List<String> categoryList = ["Âñå òèïû çàäàíèé", "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà", "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà", "Óïîðÿäî÷èâàíèå ýëåìåíòîâ", "Ñîîòíîøåíèå âåëè÷èí", "Ñîîòíîøåíèå ýëåìåíòîâ", "Ïîäñòàíîâêà îòâåòîâ"];
  28. [ObservableProperty] List<String> sortList = ["Áåç ñîðòèðîâêè", "×àñòî âñòðå÷àåìûå âîïðîñû", "Ìàëî âñòðå÷àåìûå âîïðîñû", "Ïî êîëè÷åñòâó + îòâåòîâ", "Ïî êîëè÷åñòâó - îòâåòîâ"];
  29. ObservableCollection<Question> questions = new ObservableCollection<Question>();
  30. public ObservableCollection<Question> Questions { get => questions; set { questions = value; OnPropertyChanged("Questions"); } }
  31. public string SelectedCategory { get => selectedCategory; set { selectedCategory = value; OnPropertyChanged("SelectedCategory"); filter(); } }
  32. public string SelectedSort { get => selectedSort; set { selectedSort = value; OnPropertyChanged("SelectedSort"); filter(); } }
  33. public string? SearchLine { get => searchLine; set { searchLine = value; OnPropertyChanged("SearchLine"); filter(); } }
  34. public bool SelectedInTest { get => selectedInTest; set { selectedInTest = value; OnPropertyChanged("SelectedInTest"); filter(); } }
  35. public TaskMainViewModel()
  36. {
  37. filter();
  38. }
  39. public async void DeleteQuestion(int idQuestion)
  40. {
  41. try
  42. {
  43. var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå âîïðîñà", "Ìû íå ðåêîìåíäóåì âàì óäàëÿòü âîïðîñûþ Âû äåéñòâèòåëüíî õîòèòå óäàëèòü ýòîò âîïðîñ?", ButtonEnum.YesNo).ShowAsync();
  44. switch (result)
  45. {
  46. case ButtonResult.Yes:
  47. {
  48. EntranceTestingContext connection = new EntranceTestingContext();
  49. Question q = connection.Questions.FirstOrDefault(tb => tb.Id == idQuestion);
  50. if (q != null)
  51. connection.Questions.Remove(q);
  52. connection.SaveChanges();
  53. filter();
  54. await MessageBoxManager.GetMessageBoxStandard("Óñïåøíîå óäàëåíèå", "Âîïðîñ óñïåøíî óäàëåí", ButtonEnum.Ok).ShowAsync();
  55. break;
  56. }
  57. default:
  58. break;
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  64. #if DEBUG
  65. Debug.WriteLine(ex.Message);
  66. #endif
  67. }
  68. }
  69. private void filter()
  70. {
  71. try
  72. {
  73. Questions.Clear();
  74. List<Question> _list = new EntranceTestingContext().Questions
  75. .Include(tb => tb.IdCategoryNavigation).ThenInclude(tb => tb.IdOrientationNavigation)
  76. .Include(tb => tb.ElementOfChooses)
  77. .Include(tb => tb.ElementOfArrangements)
  78. .Include(tb => tb.TextOfPuttings).ThenInclude(tb => tb.ElementOfPuttings)
  79. .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement1Navigations)
  80. .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement2Navigations)
  81. .Include(tb => tb.Groups).ThenInclude(tb => tb.ElementOfGroups)
  82. .Include(tb => tb.UserResponses).ToList();
  83. //comboBox wiht typing Question (selected type)
  84. if (SelectedCategory != "Âñå òèïû çàäàíèé" && SelectedCategory != null)
  85. _list = _list.Where(tb => tb.IdCategoryNavigation.Name.ToLower().Contains(SelectedCategory.ToLower())).ToList();
  86. //search
  87. if (!string.IsNullOrWhiteSpace(SearchLine))
  88. _list = _list.Where(tb => tb.FillName.ToLower().Contains(SearchLine.ToLower())).ToList();
  89. if (SelectedInTest)
  90. _list = _list.Where(tb => tb.InTest == true).ToList();
  91. //sort
  92. switch (SelectedSort)
  93. {
  94. case "×àñòî âñòðå÷àåìûå âîïðîñû":
  95. {
  96. _list = _list.OrderByDescending(tb => tb.CountInResponse).ToList();
  97. break;
  98. }
  99. case "Ìàëî âñòðå÷àåìûå âîïðîñû":
  100. {
  101. _list = _list.OrderBy(tb => tb.CountInResponse).ToList();
  102. break;
  103. }
  104. case "Ïî êîëè÷åñòâó + îòâåòîâ":
  105. {
  106. _list = _list.OrderByDescending(tb => tb.CountCorrectly).ToList();
  107. break;
  108. }
  109. case "Ïî êîëè÷åñòâó - îòâåòîâ":
  110. {
  111. _list = _list.OrderByDescending(tb => tb.UnCountCorrectly).ToList();
  112. break;
  113. }
  114. default:
  115. break;
  116. }
  117. if (_list.Count > 0)
  118. foreach (Question q in _list)
  119. Questions.Add(q);
  120. else
  121. Questions = new ObservableCollection<Question>();
  122. }
  123. catch (Exception ex)
  124. {
  125. #if DEBUG
  126. Debug.WriteLine(ex.Message);
  127. #endif
  128. }
  129. }
  130. }
  131. }