CoursesViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using pr13.Models;
  5. using Microsoft.EntityFrameworkCore;
  6. using ReactiveUI;
  7. namespace pr13.ViewModels
  8. {
  9. public class CoursesViewModel : ReactiveObject
  10. {
  11. public static PostgresContext DB = new PostgresContext();
  12. private List<Course>? courses;
  13. public List<Course>? Courses { get => courses; set => this.RaiseAndSetIfChanged(ref courses, value); }
  14. public CoursesViewModel()
  15. {
  16. Courses = DB.Courses.Include(x => x.TeachersCourses).ToList();
  17. }
  18. public static List<string> OrderNameOrHours => new List<string>() { "Áåç ñîðòèðîâêè", "Ïî èìåíè À-ß", "Ïî èìåíè ß-À", "Ïî êîëè÷åñòâó ÷àñîâ (ïî âîçðàñòàíèþ)", "Ïî êîëè÷åñòâó ÷àñîâ (ïî óáûâàíèþ)" };
  19. private string selectedNameOrHour = OrderNameOrHours[0];
  20. public string SelectedNameOrHour
  21. {
  22. get => selectedNameOrHour;
  23. set
  24. {
  25. selectedNameOrHour = value;
  26. Filter();
  27. }
  28. }
  29. public static List<string> FilterByHours => new List<string>() { "Áåç ôèëüòðà", "50 ÷àñîâ èëè ìåíüøå", "îò 50 äî 100 ÷àñîâ", "áîëåå 100 ÷àñîâ" };
  30. private string selectedFilter = FilterByHours[0];
  31. public string SelectedFilter
  32. {
  33. get => selectedFilter;
  34. set
  35. {
  36. selectedFilter = value;
  37. Filter();
  38. }
  39. }
  40. private string nameOrHoursFind;
  41. public string NameOrHoursFind
  42. {
  43. get => nameOrHoursFind;
  44. set
  45. {
  46. nameOrHoursFind = value;
  47. Filter();
  48. }
  49. }
  50. public void Filter()
  51. {
  52. Courses = DB.Courses.Include(x => x.TeachersCourses).ToList();
  53. if (SelectedNameOrHour != "Áåç ñîðòèðîâêè")
  54. {
  55. switch (SelectedNameOrHour)
  56. {
  57. case "Ïî èìåíè À-ß":
  58. Courses = Courses.OrderBy(x => x.Course1).ToList();
  59. break;
  60. case "Ïî èìåíè ß-À":
  61. Courses = Courses.OrderByDescending(x => x.Course1).ToList();
  62. break;
  63. case "Ïî êîëè÷åñòâó ÷àñîâ (ïî âîçðàñòàíèþ)":
  64. Courses = Courses.OrderBy(x => x.HoursCount).ToList();
  65. break;
  66. case "Ïî êîëè÷åñòâó ÷àñîâ (ïî óáûâàíèþ)":
  67. Courses = Courses.OrderByDescending(x => x.HoursCount).ToList();
  68. break;
  69. }
  70. }
  71. if (SelectedFilter != "Áåç ôèëüòðà")
  72. {
  73. switch (SelectedNameOrHour)
  74. {
  75. case "50 ÷àñîâ èëè ìåíüøå":
  76. Courses = Courses.Where(x => x.HoursCount <= 50).ToList();
  77. break;
  78. case "îò 50 äî 100 ÷àñîâ":
  79. Courses = Courses.Where(x => x.HoursCount > 50 && x.HoursCount <= 100).ToList();
  80. break;
  81. case "áîëåå 100 ÷àñîâ":
  82. Courses = Courses.Where(x => x.HoursCount > 100).ToList();
  83. break;
  84. }
  85. }
  86. if (NameOrHoursFind != null && NameOrHoursFind != "")
  87. {
  88. Courses = Courses.Where(x => x.Course1.ToLower().Contains(NameOrHoursFind.ToLower()) || NameOrHoursFind == Convert.ToString(x.HoursCount)).ToList();
  89. }
  90. }
  91. }
  92. }