123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO;
- namespace AutoservisDrive.Pages
- {
- /// <summary>
- /// Логика взаимодействия для AddEditPage.xaml
- /// </summary>
- public partial class AddEditPage : Page
- {
- private Entities.Service _currentService = null;
- private byte[] _mainImageData;
- public AddEditPage()
- {
- InitializeComponent();
- }
- public AddEditPage(Entities.Service service)
- {
- InitializeComponent();
- _currentService = service;
- Title = "Редактировать услугу";
- TBoxTitle.Text = _currentService.Title;
- TBoxCost.Text = _currentService.Cost.ToString("N2");
- TBoxDuration.Text = (_currentService.DurationInSeconds / 60).ToString();
- TBoxDescription.Text = _currentService.Description;
- if (_currentService.Discount>0)
- TBoxDiscount.Text = (_currentService.Discount.Value*100).ToString();
- if (_currentService.MainImage != null)
- ImageService.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(_currentService.MainImage);
- }
- private void BtnSelectImage_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "Image |*.png; *.jpg; *.jpeg";
- if (ofd.ShowDialog() == true)
- {
- _mainImageData = File.ReadAllBytes(ofd.FileName);
- ImageService.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(_mainImageData);
- }
- }
- private void BtnSave_Click(object sender, RoutedEventArgs e)
- {
- var errorMessage = CheckErrors();
- if (errorMessage.Length > 0)
- {
- MessageBox.Show(errorMessage, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- else
- {
- if (_currentService != null)
- {
- var service = new Entities.Service
- {
- Title = TBoxTitle.Text,
- Cost = decimal.Parse(TBoxCost.Text),
- DurationInSeconds = int.Parse(TBoxDuration.Text) * 60,
- Description = TBoxDescription.Text,
- Discount = string.IsNullOrWhiteSpace(TBoxDiscount.Text) ? 0 : double.Parse(TBoxDiscount.Text) / 100,
- MainImage = _mainImageData
- };
- App.Context.Services.Add(service);
- App.Context.SaveChanges();
- }
- else
- {
- _currentService.Title = TBoxTitle.Text;
- _currentService.Cost = decimal.Parse(TBoxCost.Text);
- _currentService.DurationInSeconds = int.Parse(TBoxDuration.Text);
- _currentService.Discount = string.IsNullOrWhiteSpace(TBoxDiscount.Text) ? 0 : double.Parse(TBoxDiscount.Text) / 100;
- if (_mainImageData != null)
- {
- _currentService.MainImage = _mainImageData;
- }
- App.Context.SaveChanges();
- }
-
- NavigationService.GoBack();
- }
- }
- private string CheckErrors()
- {
- var errorBuilder = new StringBuilder();
- //Проверка на заполнение наименования услуги
- if (string.IsNullOrWhiteSpace(TBoxTitle.Text))
- errorBuilder.AppendLine("Названиеуслуги обязательно для заполнения");
- //Проверка новой услуги с уже существующими в БД
- var serviceFromDB = App.Context.Services.ToList().FirstOrDefault(P => P.Title.ToLower() == TBoxTitle.Text.ToLower());
- if (serviceFromDB != null && serviceFromDB != _currentService)
- {
- errorBuilder.AppendLine("Такая услуга уже есть в базе данных;");
- }
- decimal cost = 0;
- //Проверка неотрицательности цены за услугу
- if (decimal.TryParse(TBoxCost.Text, out cost) == false || cost <= 0)
- {
- errorBuilder.AppendLine("Стоимость услуги должна быть положительным числом;");
- }
- int durationInMinutes = 0;
- //Проверка неотрицательности длительности услуги
- if (int.TryParse(TBoxDuration.Text, out durationInMinutes) == false || durationInMinutes > 240 || durationInMinutes <= 0)
- {
- errorBuilder.AppendLine("Длительность оказания услуги должна быть положительным " + "числом (не больше, чем 4 часа);");
- }
- //Проверка заполения скидки
- if (!string.IsNullOrEmpty(TBoxDiscount.Text))
- {
- int discount = 0;
- //Проверка на принадлежность к диапазону от 0 до 100
- if (int.TryParse(TBoxDiscount.Text, out discount) == false || discount < 0 || discount > 100)
- {
- errorBuilder.AppendLine("Размер скидки - целое число в диапазоне от 0 до 100%;");
- }
- }
- //Просто вывод ошибок, которые нужно исправить
- if (errorBuilder.Length > 0)
- {
- errorBuilder.Insert(0, "Устраните следующие ошибки:\n");
- }
- return errorBuilder.ToString();
- }
- }
- }
|