Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.Text.RegularExpressions;
  15. using Clemtor.Models;
  16.  
  17. namespace Clemtor.Views
  18. {
  19.     /// <summary>
  20.     /// Logique d'interaction pour addProduct.xaml
  21.     /// </summary>
  22.     public partial class addProduct : Window
  23.     {
  24.         private readonly Models.Clemtor _db = new Models.Clemtor();
  25.  
  26.         public List<Models.categories> categoriesList;
  27.  
  28.         public readonly string regexName = @"^[A-Za-zéàèêëïîç\- ]+$";
  29.  
  30.         private bool nameProductIsValid = false;
  31.         private bool imageProductIsValid = false;
  32.         private bool descriptionProductIsValid = false;
  33.         private bool referenceProductIsValid = false;
  34.         private bool priceProductIsValid = false;
  35.         private bool quantityProductIsValid = false;
  36.         private bool categoriesListIsValid = false;
  37.  
  38.  
  39.         public addProduct()
  40.         {
  41.             InitializeComponent();
  42.             ComboBoxNameOfCategories();
  43.         }
  44.  
  45.         public void ComboBoxNameOfCategories()
  46.         {
  47.             categoriesList = _db.categories.ToList();
  48.             categoriesComboBox.DataContext = categoriesList;
  49.             categoriesComboBox.SelectedValuePath = "idCategories";
  50.         }
  51.         private void Window_Loaded(object sender, RoutedEventArgs e)
  52.         {
  53.  
  54.             CollectionViewSource productsViewSource = (CollectionViewSource)(this.FindResource("productsViewSource"));
  55.         }
  56.  
  57.         public void NameProductValidation()
  58.         {
  59.  
  60.             if (!String.IsNullOrEmpty(textBoxProductName.Text))
  61.             {
  62.                 // si il passe la regex, return true
  63.                 if (Regex.IsMatch(textBoxProductName.Text, regexName))
  64.                 {
  65.                     if (_db.products.Any(x => x.name == textBoxProductName.Text))
  66.                     {
  67.                         nameProductIsValid = false;
  68.                         textBlockErrorNameProduct.Text = "Le nom du produit a déjà été enregistré";
  69.                     } else
  70.                     {
  71.                         nameProductIsValid = true;
  72.                         textBlockErrorNameProduct.Text = "";
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     nameProductIsValid = false;
  78.                     textBlockErrorNameProduct.Text = "Le nom du produit n'est pas valide";
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 nameProductIsValid = false;
  84.                 textBlockErrorNameProduct.Text = "Ce champ est requis";
  85.             }
  86.         }
  87.         public void imageValidation()
  88.         {
  89.  
  90.         }
  91.  
  92.         public void DescriptionValidation()
  93.         {
  94.             if (String.IsNullOrEmpty(textBoxDescriptionProduct.Text))
  95.             {
  96.                 descriptionProductIsValid = false;
  97.                 textBlockErrorDescriptionProduct.Text = "Ce champ est requis";
  98.             }
  99.             else
  100.             {
  101.                 descriptionProductIsValid = true;
  102.                 textBlockErrorDescriptionProduct.Text = "";
  103.             }
  104.         }
  105.  
  106.         public void ReferenceValidation()
  107.         {
  108.             if (!String.IsNullOrEmpty(textBoxReferenceProduct.Text))
  109.             {
  110.                 if (_db.products.Any(x => x.reference == textBoxReferenceProduct.Text))
  111.                 {
  112.                     referenceProductIsValid = false;
  113.                     textBlockErrorReferenceProduct.Text = "Cette référence est déjà enregistrée";
  114.                 } else
  115.                 {
  116.                     referenceProductIsValid = true;
  117.                     textBlockErrorReferenceProduct.Text = "";
  118.                 }
  119.             }
  120.             else
  121.             {
  122.                 referenceProductIsValid = false;
  123.                 textBlockErrorReferenceProduct.Text = "Ce champ est requis";
  124.             }
  125.         }
  126.  
  127.         public void PriceValidation()
  128.         {
  129.             if (!String.IsNullOrEmpty(textBoxPriceProduct.Text))
  130.             {
  131.                 if (float.TryParse(textBoxPriceProduct.Text, out float price) && price < 0)
  132.                 {
  133.                     priceProductIsValid = false;
  134.                     textBlockErrorPriceProduct.Text = "Le prix doit être des chiffres et supérieur à 0";
  135.                 }
  136.                 else
  137.                 {
  138.                     priceProductIsValid = true;
  139.                     textBlockErrorPriceProduct.Text = "";
  140.                 }
  141.             }
  142.             else
  143.             {
  144.                 priceProductIsValid = false;
  145.                 textBlockErrorPriceProduct.Text = "Ce champ est requis";
  146.             }
  147.         }
  148.  
  149.         public void QuantityValidation()
  150.         {
  151.             if (!String.IsNullOrEmpty(textBoxQuantityProduct.Text))
  152.             {
  153.                 if (int.TryParse(textBoxQuantityProduct.Text, out int quantity) && quantity < 0)
  154.                 {
  155.                     quantityProductIsValid = false;
  156.                     textBlockErrorQuantityProduct.Text = "La quantité doit être des chiffres et supérieur à 0";
  157.                 }
  158.                 else
  159.                 {
  160.                     quantityProductIsValid = true;
  161.                     textBlockErrorQuantityProduct.Text = "";
  162.                 }
  163.             }
  164.             else
  165.             {
  166.                 quantityProductIsValid = false;
  167.                 textBlockErrorQuantityProduct.Text = "Ce champ est requis";
  168.             }
  169.         }
  170.  
  171.         public void CategoriesListValidation()
  172.         {
  173.             if (categoriesComboBox.SelectedValue == null)
  174.             {
  175.                 categoriesListIsValid = false;
  176.                 textBlockErrorCategories.Text = "Ce champ est requis";
  177.             }
  178.             else
  179.             {
  180.                 categoriesListIsValid = true;
  181.                 textBlockErrorCategories.Text = "";
  182.             }
  183.         }
  184.  
  185.         private void AddProductToDatabase_Click(object sender, RoutedEventArgs e)
  186.         {
  187.             NameProductValidation();
  188.             imageValidation();
  189.             DescriptionValidation();
  190.             ReferenceValidation();
  191.             PriceValidation();
  192.             QuantityValidation();
  193.             CategoriesListValidation();
  194.             float.TryParse(textBoxPriceProduct.Text, out float price);
  195.             int.TryParse(textBoxQuantityProduct.Text, out int quantity);
  196.             if (categoriesComboBox.SelectedValue == null)
  197.             {
  198.                 categoriesListIsValid = false;
  199.                 textBlockErrorCategories.Text = "Ce champ est requis";
  200.             }
  201.             if (nameProductIsValid && descriptionProductIsValid && referenceProductIsValid && priceProductIsValid && quantityProductIsValid && categoriesListIsValid)
  202.             {
  203.                 try
  204.                 {
  205.                     Models.products addProduct = new Models.products()
  206.                     {
  207.                         name = textBoxProductName.Text,
  208.                         image = "default",
  209.                         description = textBoxDescriptionProduct.Text,
  210.                         prix = price,
  211.                         quantity = quantity,
  212.                         reference = textBoxReferenceProduct.Text,
  213.                         idUser = userConnected.IdUserConnected,
  214.                         idCategories = Convert.ToInt32(categoriesComboBox.SelectedValue)
  215.                     };
  216.                     _db.products.Add(addProduct);
  217.                     _db.SaveChanges();
  218.                     MessageBox.Show("Ajout d'un produit avec succès", "Succès", MessageBoxButton.OK, MessageBoxImage.Information);
  219.                     textBoxProductName.Text = null;
  220.                     textBoxDescriptionProduct.Text = null;
  221.                     textBoxPriceProduct.Text = null;
  222.                     textBoxQuantityProduct.Text = null;
  223.                     textBoxReferenceProduct.Text = null;
  224.                 }
  225.                 catch (Exception ex)
  226.                 {
  227.                     throw ex;
  228.                 }
  229.             }
  230.         }
  231.         /// <summary>
  232.         /// Méthode afin d'acceder a la fenêtre menuAdmin
  233.         /// </summary>
  234.         /// <param name="sender"></param>
  235.         /// <param name="e"></param>
  236.         private void BtnBack_Click(object sender, RoutedEventArgs e)
  237.         {
  238.             menuAdmin windowMenuAdmin = new menuAdmin();
  239.             windowMenuAdmin.Show();
  240.             this.Close();
  241.         }
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement