TeachersViewModel.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Microsoft.EntityFrameworkCore;
  2. using MsBox.Avalonia;
  3. using MsBox.Avalonia.Dto;
  4. using MsBox.Avalonia.Enums;
  5. using ReactiveUI;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Teachers.Models;
  12. using Teachers.ViewModels;
  13. namespace Teachers.ViewModels
  14. {
  15. internal class TeachersViewModel : ViewModelBase
  16. {
  17. List<Teacher> _teachersList = MainWindowViewModel.myConnection.Teachers
  18. .Include(x => x.GenderNavigation)
  19. .Include(x => x.SubjectsTaughts).ThenInclude(x => x.IdSubjectNavigation)
  20. .Include(x => x.TeacherCourses).ThenInclude(x => x.IdCourseNavigation)
  21. .ToList();
  22. public List<Teacher> TeachersList { get => _teachersList; set => this.RaiseAndSetIfChanged(ref _teachersList, value); }
  23. public async void DeleteTeacher(int id)
  24. {
  25. Teacher teacherForDelete = MainWindowViewModel.myConnection.Teachers.First(x => x.Id == id);
  26. ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Внимание", "Вы уверены, что хотите удалить " + teacherForDelete.FIO + "?", MsBox.Avalonia.Enums.ButtonEnum.YesNo).ShowAsync();
  27. switch (result)
  28. {
  29. case ButtonResult.Yes:
  30. {
  31. MainWindowViewModel.myConnection.Teachers.Remove(teacherForDelete);
  32. MainWindowViewModel.myConnection.SaveChanges();
  33. MainWindowViewModel.Instance.Us = new TeachersView();
  34. break;
  35. }
  36. case ButtonResult.No:
  37. {
  38. break;
  39. }
  40. }
  41. }
  42. public void ToAdd()
  43. {
  44. MainWindowViewModel.Instance.Us = new AddTeacherView();
  45. }
  46. public void ToUpdate(int idTeacher)
  47. {
  48. MainWindowViewModel.Instance.Us = new AddTeacherView(idTeacher);
  49. }
  50. //public void DeleteTeacher(int id)
  51. //{
  52. // Teacher teacherForDelete = MainWindowViewModel.myConnection.Teachers.First(x => x.Id == id);
  53. // MainWindowViewModel.myConnection.Teachers.Remove(teacherForDelete);
  54. // MainWindowViewModel.myConnection.SaveChanges();
  55. // MainWindowViewModel.Instance.Us = new TeachersView();
  56. //}
  57. }
  58. }