123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using AutoServiceSultik;
- using AutoServiceSultik.Entites;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO;
- 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;
- namespace AutoServiceSultik.Pages
- {
- /// <summary>
- /// Логика взаимодействия для AddEditPage.xaml
- /// </summary>
- public partial class AddEditPage : Page
- {
- private Service _currentService = null;
- private byte[] _mainImageData;
- public AddEditPage()
- {
- InitializeComponent();
- }
- public AddEditPage(Service service)
- {
- InitializeComponent();
- _currentService = service;
- tbName.Text = service.Title;
- tbCost.Text = service.Cost.ToString("N2");
- tbDurationInSecond.Text = (service.DurationInSeconds / 60).ToString();
- tbDiscription.Text = service.Description;
- if (service.Discount > 0)
- tbDiscount.Text = (service.Discount.Value * 100).ToString();
- if (service.Image != null)
- imgImage.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(service.Image);
- }
- private string CheckErrors()
- {
- var errorBuider = new StringBuilder();
- if (string.IsNullOrEmpty(tbName.Text))
- errorBuider.AppendLine("Название услуги обязательно для заполнения;");
- var serviceFromDB = App.context.Services.ToList().FirstOrDefault(p => p.Title.ToLower() == tbName.Text.ToLower());
- if (serviceFromDB != null && serviceFromDB != _currentService)
- errorBuider.AppendLine("Такая услуга уже существует;");
- decimal cost = 0;
- if (decimal.TryParse(tbCost.Text, out cost) == false || cost <= 0)
- errorBuider.AppendLine("Стоимость услуги должно быть положительное число;");
- if (!string.IsNullOrEmpty(tbDiscount.Text))
- {
- int discoutn = 0;
- if (int.TryParse(tbDiscount.Text, out discoutn) == false || discoutn < 0 || discoutn > 100)
- errorBuider.AppendLine("Размер скидки - целое число в диапазоне от 0 до 100%");
- }
- if (errorBuider.Length > 0)
- errorBuider.Insert(0, "Устраните следующие ошибки: \n");
- return errorBuider.ToString();
- }
- 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);
- imgImage.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 Service
- {
- Title = tbName.Text,
- Cost = decimal.Parse(tbCost.Text),
- DurationInSeconds = int.Parse(tbDurationInSecond.Text) * 60,
- Description = tbDiscription.Text,
- Discount = string.IsNullOrEmpty(tbDiscount.Text) ? 0 : double.Parse(tbDiscount.Text) / 100,
- Image = _mainImageData
- };
- App.context.Services.Add(service);
- App.context.SaveChanges();
- } else
- {
- _currentService.Title = tbName.Text;
- _currentService.Cost = decimal.Parse(tbCost.Text);
- _currentService.DurationInSeconds = int.Parse(tbDurationInSecond.Text) * 60;
- _currentService.Description = tbDiscription.Text;
- _currentService.Discount = string.IsNullOrEmpty(tbDiscount.Text) ? 0 : double.Parse(tbDiscount.Text) / 100;
- if(_mainImageData != null)
- _currentService.Image = _mainImageData;
- App.context.SaveChanges();
- }
- App.MainFrame.GoBack();
- }
- }
- }
- }
|