AddEditPage.xaml.cs 4.8 KB

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