AddEditServicePage.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace Practic.Pages
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для AddEditServicePage.xaml
  22. /// </summary>
  23. public partial class AddEditServicePage : Page
  24. {
  25. private Entities.Service _currentService = null;
  26. private byte[] _mainImageData;
  27. public AddEditServicePage()
  28. {
  29. InitializeComponent();
  30. }
  31. public AddEditServicePage(Entities.Service service)
  32. {
  33. InitializeComponent();
  34. _currentService = service;
  35. Title = "Редактировать услугу";
  36. TBoxTitle.Text = _currentService.Title;
  37. TBoxCost.Text = _currentService.Cost.ToString("N2");
  38. TBoxDuration.Text = (_currentService.DurationInSeconds / 60).ToString();
  39. TBoxDescription.Text = _currentService.Description;
  40. if (_currentService.Discount > 0) TBoxDiscount.Text = (_currentService.Discount.Value * 100).ToString();
  41. if (_currentService.MainImage != null) ImageService.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(_currentService.MainImage);
  42. }
  43. private string CheckErrors()
  44. {
  45. var errorBuilder = new StringBuilder();
  46. if (string.IsNullOrWhiteSpace(TBoxTitle.Text))
  47. errorBuilder.AppendLine("Название услуги обязательно для заполения!");
  48. var serviceFromDB = App.Context.Service.ToList().FirstOrDefault(p => p.Title.ToLower() == TBoxTitle.Text.ToLower());
  49. if (serviceFromDB != null && serviceFromDB != _currentService) errorBuilder.AppendLine("Такая услуга уже есть в базе данных!");
  50. decimal cost = 0;
  51. if (decimal.TryParse(TBoxCost.Text, out cost) == false || cost <= 0) errorBuilder.AppendLine("Стоимость услуги должна быть положительным числом!!!");
  52. int durationInMinutes = 0;
  53. if (int.TryParse(TBoxDuration.Text, out durationInMinutes) == false || durationInMinutes > 240 || durationInMinutes <= 0)
  54. {
  55. errorBuilder.AppendLine("Длительность оказания услуги должна быть положительным" + "числом (не больше 4 часов)!");
  56. }
  57. if (!string.IsNullOrEmpty(TBoxDiscount.Text))
  58. {
  59. int discount = 0;
  60. if (int.TryParse(TBoxDiscount.Text, out discount) == false || discount < 0 || discount > 100)
  61. {
  62. errorBuilder.AppendLine("Размер скидки - целое число в диапазоне от 0 до 100%!");
  63. }
  64. }
  65. if (errorBuilder.Length > 0) errorBuilder.Insert(0, "Устраните следующие ошибки: \n");
  66. return errorBuilder.ToString();
  67. }
  68. private void BtnSelectImage_Click(object sender, RoutedEventArgs e)
  69. {
  70. OpenFileDialog ofd = new OpenFileDialog();
  71. ofd.Filter = "Image | * .png; * .jpg; * .jpeg";
  72. if (ofd.ShowDialog() == true)
  73. {
  74. _mainImageData = File.ReadAllBytes(ofd.FileName);
  75. ImageService.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(_mainImageData);
  76. }
  77. }
  78. private void BtnSave_Click(object sender, RoutedEventArgs e)
  79. {
  80. var errorMassage = CheckErrors();
  81. if (errorMassage.Length > 0)
  82. {
  83. MessageBox.Show(errorMassage, "Ошибка!", MessageBoxButton.OK, MessageBoxImage.Error);
  84. }
  85. else
  86. {
  87. if (_currentService == null)
  88. {
  89. var service = new Entities.Service
  90. {
  91. Title = TBoxTitle.Text,
  92. Cost = decimal.Parse(TBoxCost.Text),
  93. DurationInSeconds = int.Parse(TBoxDuration.Text) * 60,
  94. Description = TBoxDescription.Text,
  95. Discount = string.IsNullOrWhiteSpace(TBoxDiscount.Text) ? 0 : double.Parse(TBoxDiscount.Text) / 100, MainImage = _mainImageData
  96. };
  97. App.Context.Service.Add(service);
  98. App.Context.SaveChanges();
  99. // Сообщение об успешном добавлении
  100. }
  101. else
  102. {
  103. _currentService.Title = TBoxTitle.Text;
  104. _currentService.Cost = decimal.Parse(TBoxCost.Text);
  105. _currentService.DurationInSeconds = int.Parse(TBoxDuration.Text) * 60;
  106. _currentService.Description = TBoxDescription.Text;
  107. _currentService.Discount = string.IsNullOrWhiteSpace(TBoxDiscount.Text) ? 0: double.Parse(TBoxDiscount.Text) / 100;
  108. if (_mainImageData != null) _currentService.MainImage = _mainImageData;
  109. App.Context.SaveChanges();
  110. // Добавляем сообщение об успешном редактировании
  111. }
  112. NavigationService.GoBack();
  113. }
  114. }
  115. }
  116. }