ResultMainViewModel.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Avalonia.Threading;
  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 ResultMainViewModel : ObservableObject
  13. {
  14. ObservableCollection<UserSession> answers = new ObservableCollection<UserSession>();
  15. string group = "";
  16. string fio = "";
  17. [ObservableProperty]DateTime endDate = DateTime.Today;
  18. [ObservableProperty]DateTime startDate = DateTime.Today;
  19. DateTime selectedEndDate = DateTime.Today;
  20. DateTime selectedStartDate = DateTime.Today;
  21. [ObservableProperty] DispatcherTimer timer = new DispatcherTimer();
  22. public ResultMainViewModel()
  23. {
  24. EntranceTestingContext connection = new EntranceTestingContext();
  25. StartDate = connection.UserSessions.OrderBy(tb => tb.Date).ToList().First().Date;
  26. SelectedStartDate = StartDate;
  27. filter();
  28. timer.Interval = new TimeSpan(0, 1, 30);
  29. timer.Tick += restart;
  30. timer.Start();
  31. }
  32. private void restart(object? sender, EventArgs e)
  33. {
  34. filter();
  35. }
  36. public ObservableCollection<UserSession> Answers { get => answers; set => answers = value; }
  37. public string Group { get => group; set { group = value; filter(); } }
  38. public string Fio { get => fio; set { fio = value; filter(); } }
  39. public DateTime SelectedEndDate { get => selectedEndDate; set { selectedEndDate = value; filter(); } }
  40. public DateTime SelectedStartDate { get => selectedStartDate; set { selectedStartDate = value; filter(); } }
  41. private void filter()
  42. {
  43. EntranceTestingContext connection = new EntranceTestingContext();
  44. List<UserSession> list = connection.UserSessions
  45. .Include(tb=>tb.IdAppSettingsNavigation)
  46. .Include(tb => tb.UserResponses)
  47. .OrderByDescending(tb => tb.Date).ToList();
  48. if(!string.IsNullOrWhiteSpace(Group))
  49. list = list.Where(tb => tb.UserGroup.ToLower().Contains(Group.ToLower())).ToList();
  50. if (!string.IsNullOrWhiteSpace(Fio))
  51. list = list.Where(tb => tb.UserName.ToLower().Contains(Fio.ToLower())).ToList();
  52. list = list.Where(tb => tb.Date >= SelectedStartDate && tb.Date.Date <= SelectedEndDate).ToList();
  53. Answers.Clear();
  54. foreach (var item in list)
  55. Answers.Add(item);
  56. }
  57. }
  58. }