AddEditPage.xaml.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Savelev_ToursApp
  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 = Savelev_ToursEntities.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.CountOfStars < 1 || _currentHotel.CountOfStars > 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. Savelev_ToursEntities.GetContext().Hotels.Add(_currentHotel);
  47. try
  48. {
  49. Savelev_ToursEntities.GetContext().SaveChanges();
  50. MessageBox.Show("Информация сохранена!");
  51. }
  52. catch(Exception ex)
  53. {
  54. MessageBox.Show(ex.Message.ToString());
  55. }
  56. }
  57. }
  58. }