AddPageViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Avalonia.Controls.Platform;
  2. using AvaloniaApplicationTest.Models;
  3. using ReactiveUI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using static System.Runtime.InteropServices.JavaScript.JSType;
  10. namespace AvaloniaApplicationTest.ViewModels
  11. {
  12. public class AddPageViewModel : ReactiveObject
  13. {
  14. bool edit = false;
  15. Hotel new_hotel = new Hotel();
  16. public AddPageViewModel()
  17. {
  18. }
  19. public AddPageViewModel(int ID) //я не понимаю, каким образом Авалония берет айди именно предпоследнего элемента. Именно элемента
  20. {
  21. edit = true;
  22. new_hotel = MainWindowViewModel.myConnection.Hotels.FirstOrDefault(x=>x.Id == ID);
  23. if (new_hotel != null) {
  24. Name = new_hotel.Name;
  25. Description = new_hotel.Description;
  26. CountOfStars = new_hotel.CountOfStars;
  27. ValueSelect = MainWindowViewModel.myConnection.Countries.First(x => x.Code == new_hotel.CountryCode); ;
  28. }
  29. else
  30. {
  31. Message = "Что-то пошло не так. Нажмите кнопку Отмена";
  32. }
  33. }
  34. public string Name
  35. {
  36. get => name;
  37. set => this.RaiseAndSetIfChanged(ref name, value);
  38. }
  39. public string Description
  40. {
  41. get => description;
  42. set => this.RaiseAndSetIfChanged(ref description, value);
  43. }
  44. public int CountOfStars
  45. {
  46. get => count_of_stars;
  47. set => this.RaiseAndSetIfChanged(ref count_of_stars, value);
  48. }
  49. public List<Country> CBContries
  50. {
  51. get => countries;
  52. set => this.RaiseAndSetIfChanged(ref countries, value);
  53. }
  54. public Country ValueSelect
  55. {
  56. get => data;
  57. set
  58. {
  59. this.RaiseAndSetIfChanged(ref data, value);
  60. }
  61. }
  62. public string Message
  63. {
  64. get => m;
  65. set
  66. {
  67. this.RaiseAndSetIfChanged(ref m, value);
  68. }
  69. }
  70. List<Country> countries = MainWindowViewModel.myConnection.Countries.ToList();
  71. string name = "";
  72. int count_of_stars = 0;
  73. string description = "";
  74. Country data = MainWindowViewModel.myConnection.Countries.First();
  75. string m = "";
  76. void GetNewData()
  77. {
  78. if (!edit) new_hotel.Id = MainWindowViewModel.myConnection.Hotels.Max(x => x.Id)+1;
  79. new_hotel.Name = Name;
  80. new_hotel.CountOfStars = CountOfStars;
  81. new_hotel.CountryCode = ValueSelect.Code;
  82. new_hotel.Description = Description;
  83. }
  84. bool CheckData()
  85. {
  86. if ((Name == string.Empty) || (Description == string.Empty) || (ValueSelect.Name == string.Empty) || (CountOfStars < 0) || (CountOfStars > 5))
  87. {
  88. Message = "Некорректные данные. Повторите";
  89. return false;
  90. }
  91. return true;
  92. }
  93. public void Save()
  94. {
  95. GetNewData();
  96. if (CheckData())
  97. {
  98. Message = string.Empty;
  99. try
  100. {
  101. if (!edit) MainWindowViewModel.myConnection.Hotels.Add(new_hotel);
  102. MainWindowViewModel.myConnection.SaveChanges();
  103. Message = "Сохранено. Нажмите кнопку Отмена.";
  104. }
  105. catch
  106. {
  107. Message = "Что-то пошло не так!";
  108. }
  109. }
  110. }
  111. }
  112. }