AddChangePage.xaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using DB.Classes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. enum CreateState {Create, Update };
  17. namespace DB.Pages
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для AddChangePage.xaml
  21. /// </summary>
  22. public partial class AddChangePage : Page
  23. {
  24. CreateState state;
  25. Service service = new Service();
  26. public AddChangePage()
  27. {
  28. InitializeComponent();
  29. state = CreateState.Create;
  30. }
  31. public AddChangePage(int id)
  32. {
  33. InitializeComponent();
  34. state = CreateState.Update;
  35. service = DataBaseClass.entity.Service.FirstOrDefault(x => x.ID == id);
  36. TBTitle.Text = service.Title;
  37. TBCost.Text = service.Cost.ToString();
  38. BTNDio.Content = "Change";
  39. }
  40. private void BTNDio_Click(object sender, RoutedEventArgs e)
  41. {
  42. Service check = DataBaseClass.entity.Service.FirstOrDefault(x => x.Title.ToLower() == TBTitle.Text.ToLower());
  43. if(check != null)
  44. {
  45. MessageBox.Show("Такая запись уже есть!");
  46. return;
  47. }
  48. bool flag = true;
  49. while(flag)
  50. {
  51. if(state == CreateState.Create)
  52. {
  53. service.Title = TBTitle.Text;
  54. service.Cost = Convert.ToDecimal(TBCost.Text);
  55. DataBaseClass.entity.Service.Add(service);
  56. }
  57. else
  58. {
  59. service.Title = TBTitle.Text;
  60. service.Cost = Convert.ToDecimal(TBCost.Text);
  61. }
  62. if(state != CreateState.Update)
  63. {
  64. MessageBox.Show("Добавление!");
  65. }
  66. else
  67. {
  68. MessageBox.Show("Изменение!");
  69. }
  70. DataBaseClass.entity.SaveChanges();
  71. ListViewPage listViewPage = new ListViewPage();
  72. listViewPage.LVService.ItemsSource = DataBaseClass.entity.Service.ToList();
  73. FrameClass.MainFrame.Navigate(listViewPage);
  74. flag = false;
  75. }
  76. }
  77. }
  78. }