StudentsViewModel.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using Avalonia.Controls;
  6. using AvaloniaApplication4.AdditionalModels;
  7. using AvaloniaApplication4.Models;
  8. using AvaloniaApplication4.Views.ActionPages;
  9. using Microsoft.EntityFrameworkCore;
  10. using ReactiveUI;
  11. namespace AvaloniaApplication4.ViewModels
  12. {
  13. public class StudentsViewModel : ViewModelBase
  14. {
  15. User? user;
  16. List<StudentsTemplate> all = new List<StudentsTemplate>();
  17. List<StudentsTemplate> staticAll = new List<StudentsTemplate>();
  18. string searchQueryPrograms = string.Empty;
  19. public string SearchQueryPrograms
  20. {
  21. get
  22. {
  23. return searchQueryPrograms;
  24. }
  25. set
  26. {
  27. searchQueryPrograms = value;
  28. if (searchQueryPrograms != null && searchQueryPrograms != string.Empty && searchQueryPrograms != "")
  29. {
  30. All = staticAll.Where(x=>x.Program.IdProgramTemplateNavigation.NameProgram.ToLower().StartsWith(searchQueryPrograms.ToLower())).ToList();
  31. }
  32. else All = staticAll;
  33. }
  34. }
  35. GlobalsData globalsData;
  36. public StudentsViewModel(User? user, ref GlobalsData globalsData)
  37. {
  38. this.user = user;
  39. List<RunningProgram> programs = dataBase.RunningPrograms
  40. .Include(x=>x.IdProgramTemplateNavigation)
  41. .ToList();
  42. List<ProgramsWithStudent> programsWithStudents = dataBase.ProgramsWithStudents
  43. .Include(x=>x.IdRunningProgramNavigation)
  44. .Include(x=>x.IdStudentNavigation)
  45. .ToList();
  46. foreach (var program in programs)
  47. {
  48. All.Add
  49. (
  50. new StudentsTemplate(program, programsWithStudents.Where(x=>x.IdRunningProgram == program.IdRunningPrograms).ToList())
  51. );
  52. }
  53. staticAll = All;
  54. this.globalsData = globalsData;
  55. }
  56. public void EditStudent(int id)
  57. {
  58. Student? student = dataBase.Students.Include(x=>x.IdEducationNavigation).FirstOrDefault(x=>x.IdStudent == id);
  59. if (student != null)
  60. {
  61. globalsData.GlobalUserControl = new EditAndAddStudent(user, student, ref globalsData);
  62. }
  63. }
  64. public void AddNewStudent()
  65. {
  66. globalsData.GlobalUserControl = new EditAndAddStudent(user, ref globalsData);
  67. }
  68. internal List<StudentsTemplate> All
  69. {
  70. get { return all; }
  71. set
  72. {
  73. if (all == value)
  74. return;
  75. all = value;
  76. OnPropertyChanged("All");
  77. }
  78. }
  79. }
  80. }