AllTeachersViewModel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using Avalonia.Controls;
  3. using System.Collections.Generic;
  4. using ReactiveUI;
  5. using DatabaseMVVMApp.Models;
  6. using System.Linq;
  7. using Microsoft.EntityFrameworkCore;
  8. using System.Threading;
  9. using MsBox.Avalonia;
  10. using MsBox.Avalonia.Models;
  11. using Avalonia.Notification;
  12. namespace DatabaseMVVMApp.ViewModels
  13. {
  14. public class AllTeachersViewModel : ReactiveObject
  15. {
  16. public List<Teacher> Teachers => MainWindowViewModel.connection.Teachers.Include(x => x.Gender).Include(x => x.TeachersCourses).ThenInclude(x => x.Course).Include(x => x.TeachersSubjects).ThenInclude(x => x.Subject).ToList();
  17. public string Count => Teachers.Count().ToString();
  18. public INotificationMessageManager Manager { get; } = new NotificationMessageManager();
  19. public void ToAddPage()
  20. {
  21. MainWindowViewModel.Instance.Control = new AddTeacher();
  22. }
  23. public async void DeleteTeacher(int id)
  24. {
  25. var box = MessageBoxManager.GetMessageBoxCustom(
  26. new MsBox.Avalonia.Dto.MessageBoxCustomParams {
  27. ButtonDefinitions = new List<ButtonDefinition> {
  28. new ButtonDefinition { Name = "Äà",},
  29. new ButtonDefinition { Name = "Îòìåíà",},
  30. },
  31. ContentTitle = "Ïðåäóïðåæäåíèå",
  32. ContentMessage = "Âû óâåðåííû ÷òî õîòèòå óäàëèòü äàííûå ó÷èòåëÿ?",
  33. Icon = MsBox.Avalonia.Enums.Icon.Warning,
  34. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  35. CanResize = false,
  36. MaxWidth = 500,
  37. MaxHeight = 800,
  38. SizeToContent = SizeToContent.WidthAndHeight,
  39. ShowInCenter = true,
  40. Topmost = false
  41. });
  42. var result = await box.ShowAsync();
  43. if (result == "Äà")
  44. {
  45. Teacher deleteTeacher = MainWindowViewModel.connection.Teachers.First(x => x.Id == id);
  46. MainWindowViewModel.connection.Teachers.Remove(deleteTeacher);
  47. MainWindowViewModel.connection.SaveChanges();
  48. MainWindowViewModel.Instance.Control = new AllTeachers();
  49. var box1 = MessageBoxManager.GetMessageBoxCustom(
  50. new MsBox.Avalonia.Dto.MessageBoxCustomParams
  51. {
  52. ButtonDefinitions = new List<ButtonDefinition> {
  53. new ButtonDefinition { Name = "Îê",},
  54. },
  55. ContentTitle = "Óâåäîìëåíèå",
  56. ContentMessage = "Ó÷èòåëü áûë óñïåøíî óäàëåí",
  57. Icon = MsBox.Avalonia.Enums.Icon.Success,
  58. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  59. CanResize = false,
  60. MaxWidth = 500,
  61. MaxHeight = 800,
  62. SizeToContent = SizeToContent.WidthAndHeight,
  63. ShowInCenter = true,
  64. Topmost = false
  65. });
  66. await box1.ShowAsync();
  67. }
  68. }
  69. public void UpdateTeacher(int id)
  70. {
  71. MainWindowViewModel.Instance.Control = new AddTeacher(id);
  72. }
  73. }
  74. }