UserResultViewModel.cs 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using CommunityToolkit.Mvvm.ComponentModel;
  7. using EntranseTesting.Models;
  8. using Microsoft.EntityFrameworkCore;
  9. using ReactiveUI;
  10. namespace EntranseTesting.ViewModels
  11. {
  12. public partial class UserResultViewModel : ObservableObject, INotifyPropertyChanged
  13. {
  14. public event PropertyChangedEventHandler PropertyChanged;
  15. protected virtual void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  16. [ObservableProperty] UserSession session = new UserSession();
  17. [ObservableProperty] List<UserResponse> responseList = new List<UserResponse>();
  18. bool? hintVisible = null;
  19. bool? correctlyVisible = null;
  20. bool? responseVisible = null;
  21. string searchLine = "";
  22. public UserResultViewModel(int idSession)
  23. {
  24. EntranceTestingContext connection = new EntranceTestingContext();
  25. Session = connection.UserSessions
  26. .Include(tb => tb.IdAppSettingsNavigation)
  27. .Include(tb => tb.UserResponses)
  28. .FirstOrDefault(tb => tb.Id == idSession);
  29. filter();
  30. }
  31. public bool? HintVisible { get => hintVisible; set { hintVisible = value; filter(); OnPropertyChanged("HintVisible"); OnPropertyChanged("ResponseList"); } }
  32. public bool? CorrectlyVisible { get => correctlyVisible; set { correctlyVisible = value; filter(); OnPropertyChanged("CorrectlyVisible"); OnPropertyChanged("ResponseList"); } }
  33. public bool? ResponseVisible { get => responseVisible; set{ responseVisible = value; filter(); OnPropertyChanged("ResponseVisible"); OnPropertyChanged("ResponseList"); } }
  34. public string SearchLine { get => searchLine; set { searchLine = value; filter(); OnPropertyChanged("SearchLine"); OnPropertyChanged("ResponseList"); } }
  35. private void filter()
  36. {
  37. EntranceTestingContext connection = new EntranceTestingContext();
  38. List<UserResponse> _list = connection.UserResponses
  39. .Include(tb => tb.UserResponseArrangements)
  40. .Include(tb =>tb.UserResponseChooseAnswers)
  41. .Include(tb =>tb.UserResponseMatchTheElements)
  42. .Include(tb =>tb.UserResponseMatchTheValues)
  43. .Include(tb =>tb.UserResponseMultiplyAnswers)
  44. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.ElementOfArrangements)
  45. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.ElementOfChooses)
  46. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement1Navigations)
  47. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement2Navigations)
  48. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.Groups).ThenInclude(tb =>tb.ElementOfGroups)
  49. .Include(tb => tb.IdQuestionNavigation).ThenInclude(tb => tb.TextOfPuttings).ThenInclude(tb =>tb.ElementOfPuttings)
  50. .Where(tb => tb.IdSession == Session.Id).ToList();
  51. switch (HintVisible)
  52. {
  53. case true:
  54. _list = _list.Where(tb => tb.HintApply == true).ToList();
  55. break;
  56. case false:
  57. _list = _list.Where(tb => tb.HintApply == false).ToList();
  58. break;
  59. default: break;
  60. }
  61. switch (CorrectlyVisible)
  62. {
  63. case true:
  64. _list = _list.Where(tb => tb.Correctly == true).ToList();
  65. break;
  66. case false:
  67. _list = _list.Where(tb => tb.Correctly == false).ToList();
  68. break;
  69. default: break;
  70. }
  71. switch (ResponseVisible)
  72. {
  73. case true:
  74. _list = _list.Where(tb => tb.UserResponseChooseAnswers.Count != 0 || tb.UserResponseArrangements.Count != 0 || tb.UserResponseMatchTheElements.Count != 0 || tb.UserResponseMatchTheValues.Count != 0 || tb.UserResponseMultiplyAnswers.Count != 0).ToList();
  75. break;
  76. case false:
  77. _list = _list.Where(tb => tb.UserResponseChooseAnswers.Count == 0 && tb.UserResponseArrangements.Count == 0 && tb.UserResponseMatchTheElements.Count == 0 && tb.UserResponseMatchTheValues.Count == 0 && tb.UserResponseMultiplyAnswers.Count == 0).ToList();
  78. break;
  79. default: break;
  80. }
  81. if(!string.IsNullOrWhiteSpace(SearchLine))
  82. {
  83. _list = _list.Where(tb => tb.IdQuestionNavigation.Name.ToLower().Contains(SearchLine.ToLower())).ToList();
  84. }
  85. ResponseList = _list;
  86. }
  87. }
  88. }