AddDisCursViewModel.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using ReactiveUI;
  4. using System.Linq;
  5. using System.Data;
  6. using AvaloniaApplication5.Models;
  7. using Microsoft.EntityFrameworkCore;
  8. using MsBox.Avalonia;
  9. using MsBox.Avalonia.Enums;
  10. namespace AvaloniaApplication5.ViewModels
  11. {
  12. public class AddDisCursViewModel : ViewModelBase
  13. {
  14. Course? _newcourse;
  15. public Course? NewCourse { get => _newcourse; set => this.RaiseAndSetIfChanged(ref _newcourse, value); }
  16. public List<Course> CoursesList => MainWindowViewModel.myConnection.Courses.ToList();
  17. public AddDisCursViewModel()
  18. {
  19. _newcourse = new Course();
  20. }
  21. string _message = "";
  22. public string Message
  23. {
  24. get => _message;
  25. set => this.RaiseAndSetIfChanged(ref _message, value);
  26. }
  27. public async void AddCourse()
  28. {
  29. if(string.IsNullOrEmpty(NewCourse.Time) || !IsNumeric(NewCourse.Time))
  30. {
  31. await MessageBoxManager.GetMessageBoxStandard("Ñîîáùåíèå", " ïîëå êîëè÷åñòâà ÷àñîâ äîëæíû áûòü òîëüêî öèôðû").ShowAsync();
  32. return;
  33. }
  34. else
  35. {
  36. if (NewCourse.Id == 0)
  37. {
  38. MainWindowViewModel.myConnection.Courses.Add(NewCourse);
  39. }
  40. MainWindowViewModel.myConnection.SaveChanges();
  41. MainWindowViewModel.Instance.PageContent = new AddPage();
  42. }
  43. }
  44. private bool IsNumeric(string value)
  45. {
  46. return int.TryParse(value, out _); // Èëè double.TryParse, åñëè íóæíî ïîääåðæèâàòü äðîáíûå ÷èñëà
  47. }
  48. }
  49. }