AdminUsersViewModel.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Media.Imaging;
  4. using AvaloniaApplication1.Models;
  5. using Microsoft.EntityFrameworkCore;
  6. using System.Linq;
  7. using ReactiveUI;
  8. namespace AvaloniaApplication1.ViewModels
  9. {
  10. public class AdminUsersViewModel : ViewModelBase
  11. {
  12. List<User> usersMain;
  13. List<User> users;
  14. string search;
  15. List<Gender> genders;
  16. Gender selectedGender;
  17. int id;
  18. public AdminUsersViewModel()
  19. {
  20. this.usersMain = myConnection.Users.Include(x => x.GenderNavigation).ToList();
  21. this.users = this.usersMain;
  22. genders =
  23. [
  24. new Gender() { Title = "Âûáåðåòå ïîë", Id = 0 },
  25. .. users.Select(x => x.GenderNavigation).Distinct().OrderBy(x=>x.Id).ToList()
  26. ];
  27. selectedGender = genders.Where(x => x.Id == 0).First();
  28. }
  29. public string Search { get => search; set { search = value; AllFilters(); } }
  30. public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
  31. public Gender SelectedGender { get => selectedGender; set { selectedGender = value; AllFilters(); } }
  32. public List<User> Users { get => users; set => this.RaiseAndSetIfChanged(ref users, value); }
  33. public List<User> UsersMain { get => usersMain; set => this.RaiseAndSetIfChanged(ref usersMain, value); }
  34. public void OrderByAge(int id)
  35. {
  36. switch (id)
  37. {
  38. case 1:
  39. Users = Users.OrderBy(x => x.DateOfBirth).ToList();
  40. break;
  41. case 2:
  42. Users = Users.OrderByDescending(x => x.DateOfBirth).ToList();
  43. break;
  44. }
  45. }
  46. public void Drop(int id)
  47. {
  48. myConnection.Users.Remove(UsersMain.Where(x => x.Id == id).First());
  49. myConnection.SaveChanges();
  50. UsersMain = myConnection.Users.ToList();
  51. Users = UsersMain;
  52. }
  53. public void Update(int id)
  54. {
  55. MainWindowViewModel.Self.Uc = new UpdateUsers(id);
  56. }
  57. private void AllFilters()
  58. {
  59. Users = UsersMain;
  60. if (!string.IsNullOrEmpty(Search)) Users = Users.Where(x => x.LastName.ToLower().Contains(Search.ToLower())).ToList();
  61. if (selectedGender != null && selectedGender.Id != 0) { Users = Users.Where(x => x.GenderNavigation == selectedGender).ToList(); }
  62. }
  63. }
  64. }