AddEditPage.xaml.cs 4.9 KB

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