AddEditPage.xaml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace ToursAppKhritankov
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для AddEditPage.xaml
  19. /// </summary>
  20. public partial class AddEditPage : Page
  21. {
  22. private Hotel _currentHotel = new Hotel();
  23. public AddEditPage(Hotel selectedHotel)
  24. {
  25. InitializeComponent();
  26. if (selectedHotel != null)
  27. _currentHotel = selectedHotel;
  28. DataContext = _currentHotel;
  29. ComboCountries.ItemsSource = ToursKhritankovEntities.GetContext().Country.ToList();
  30. }
  31. private void BtnSave_Click(object sender, RoutedEventArgs e)
  32. {
  33. StringBuilder errors = new StringBuilder();
  34. if (string.IsNullOrWhiteSpace(_currentHotel.Name))
  35. errors.AppendLine("Укажите название отеля");
  36. if (_currentHotel.CountOnStars < 1 || _currentHotel.CountOnStars > 5)
  37. errors.AppendLine("Количество звёзд - число от 1 до 5");
  38. if (_currentHotel.Country == null)
  39. errors.AppendLine("Выберите страну");
  40. if (errors.Length > 0)
  41. {
  42. MessageBox.Show(errors.ToString());
  43. return;
  44. }
  45. if (_currentHotel.id == 0)
  46. ToursKhritankovEntities.GetContext().Hotel.Add(_currentHotel);
  47. try
  48. {
  49. ToursKhritankovEntities.GetContext().SaveChanges();
  50. MessageBox.Show("Информация сохранена");
  51. Manager.MainFrame.GoBack();
  52. }
  53. catch (Exception ex)
  54. {
  55. MessageBox.Show(ex.Message.ToString());
  56. }
  57. }
  58. }
  59. }