AddCourseViewModel.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using DatabaseMVVMApp.Models;
  5. using Microsoft.EntityFrameworkCore;
  6. using ReactiveUI;
  7. using Tmds.DBus.Protocol;
  8. namespace DatabaseMVVMApp.ViewModels
  9. {
  10. public class AddCourseViewModel : ReactiveObject
  11. {
  12. Course? _newCourse;
  13. string _errorText;
  14. bool _errorVisibility;
  15. UserControl _teacherControl;
  16. int? _duration;
  17. public int? Duration
  18. {
  19. get => _duration;
  20. set => this.RaiseAndSetIfChanged(ref _duration, value);
  21. }
  22. public UserControl TeacherControl
  23. {
  24. get => _teacherControl;
  25. set => this.RaiseAndSetIfChanged(ref _teacherControl, value);
  26. }
  27. public Course? NewCourse
  28. {
  29. get => _newCourse;
  30. set => this.RaiseAndSetIfChanged(ref _newCourse, value);
  31. }
  32. public AddCourseViewModel()
  33. {
  34. _newCourse = new Course();
  35. TeacherControl = new AddTeacher();
  36. }
  37. public AddCourseViewModel(UserControl control)
  38. {
  39. _newCourse = new Course();
  40. TeacherControl = control;
  41. }
  42. public string ErrorText
  43. {
  44. get => _errorText;
  45. set => this.RaiseAndSetIfChanged(ref _errorText, value);
  46. }
  47. public bool ErrorVisibility
  48. {
  49. get => _errorVisibility;
  50. set => this.RaiseAndSetIfChanged(ref _errorVisibility, value);
  51. }
  52. public void ToAddTeacherPage()
  53. {
  54. MainWindowViewModel.Instance.Control = TeacherControl;
  55. }
  56. public void AddCourse()
  57. {
  58. if (NewCourse.Title == null || NewCourse.Title == "")
  59. {
  60. ErrorText = "Ó íîâîãî êóðñà äîëæíî áûòü íàçâàíèå";
  61. ErrorVisibility = true;
  62. }
  63. else if (Duration == null)
  64. {
  65. ErrorText = "Ââåäåíà íåêîððåêòíàÿ ïðîäîëæèòåëüíîñòü";
  66. ErrorVisibility = true;
  67. }
  68. else
  69. {
  70. ErrorVisibility = false;
  71. MainWindowViewModel.connection.Courses.Add(NewCourse);
  72. string message = "Íîâûé êóðñ óñïåøíî äîáàâëåí";
  73. MainWindowViewModel.connection.SaveChanges();
  74. MainWindowViewModel.Instance.AddTeacherVM.AddCourse(NewCourse, Duration);
  75. MainWindowViewModel.Instance.Control = TeacherControl;
  76. MainWindowViewModel.MakeNotification(message);
  77. }
  78. }
  79. }
  80. }