Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.Shapes;
- using System.Text.RegularExpressions;
- using Clemtor.Models;
- namespace Clemtor.Views
- {
- /// <summary>
- /// Logique d'interaction pour addProduct.xaml
- /// </summary>
- public partial class addProduct : Window
- {
- private readonly Models.Clemtor _db = new Models.Clemtor();
- public List<Models.categories> categoriesList;
- public readonly string regexName = @"^[A-Za-zéàèêëïîç\- ]+$";
- private bool nameProductIsValid = false;
- private bool imageProductIsValid = false;
- private bool descriptionProductIsValid = false;
- private bool referenceProductIsValid = false;
- private bool priceProductIsValid = false;
- private bool quantityProductIsValid = false;
- private bool categoriesListIsValid = false;
- public addProduct()
- {
- InitializeComponent();
- ComboBoxNameOfCategories();
- }
- public void ComboBoxNameOfCategories()
- {
- categoriesList = _db.categories.ToList();
- categoriesComboBox.DataContext = categoriesList;
- categoriesComboBox.SelectedValuePath = "idCategories";
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- CollectionViewSource productsViewSource = (CollectionViewSource)(this.FindResource("productsViewSource"));
- }
- public void NameProductValidation()
- {
- if (!String.IsNullOrEmpty(textBoxProductName.Text))
- {
- // si il passe la regex, return true
- if (Regex.IsMatch(textBoxProductName.Text, regexName))
- {
- if (_db.products.Any(x => x.name == textBoxProductName.Text))
- {
- nameProductIsValid = false;
- textBlockErrorNameProduct.Text = "Le nom du produit a déjà été enregistré";
- } else
- {
- nameProductIsValid = true;
- textBlockErrorNameProduct.Text = "";
- }
- }
- else
- {
- nameProductIsValid = false;
- textBlockErrorNameProduct.Text = "Le nom du produit n'est pas valide";
- }
- }
- else
- {
- nameProductIsValid = false;
- textBlockErrorNameProduct.Text = "Ce champ est requis";
- }
- }
- public void imageValidation()
- {
- }
- public void DescriptionValidation()
- {
- if (String.IsNullOrEmpty(textBoxDescriptionProduct.Text))
- {
- descriptionProductIsValid = false;
- textBlockErrorDescriptionProduct.Text = "Ce champ est requis";
- }
- else
- {
- descriptionProductIsValid = true;
- textBlockErrorDescriptionProduct.Text = "";
- }
- }
- public void ReferenceValidation()
- {
- if (!String.IsNullOrEmpty(textBoxReferenceProduct.Text))
- {
- if (_db.products.Any(x => x.reference == textBoxReferenceProduct.Text))
- {
- referenceProductIsValid = false;
- textBlockErrorReferenceProduct.Text = "Cette référence est déjà enregistrée";
- } else
- {
- referenceProductIsValid = true;
- textBlockErrorReferenceProduct.Text = "";
- }
- }
- else
- {
- referenceProductIsValid = false;
- textBlockErrorReferenceProduct.Text = "Ce champ est requis";
- }
- }
- public void PriceValidation()
- {
- if (!String.IsNullOrEmpty(textBoxPriceProduct.Text))
- {
- if (float.TryParse(textBoxPriceProduct.Text, out float price) && price < 0)
- {
- priceProductIsValid = false;
- textBlockErrorPriceProduct.Text = "Le prix doit être des chiffres et supérieur à 0";
- }
- else
- {
- priceProductIsValid = true;
- textBlockErrorPriceProduct.Text = "";
- }
- }
- else
- {
- priceProductIsValid = false;
- textBlockErrorPriceProduct.Text = "Ce champ est requis";
- }
- }
- public void QuantityValidation()
- {
- if (!String.IsNullOrEmpty(textBoxQuantityProduct.Text))
- {
- if (int.TryParse(textBoxQuantityProduct.Text, out int quantity) && quantity < 0)
- {
- quantityProductIsValid = false;
- textBlockErrorQuantityProduct.Text = "La quantité doit être des chiffres et supérieur à 0";
- }
- else
- {
- quantityProductIsValid = true;
- textBlockErrorQuantityProduct.Text = "";
- }
- }
- else
- {
- quantityProductIsValid = false;
- textBlockErrorQuantityProduct.Text = "Ce champ est requis";
- }
- }
- public void CategoriesListValidation()
- {
- if (categoriesComboBox.SelectedValue == null)
- {
- categoriesListIsValid = false;
- textBlockErrorCategories.Text = "Ce champ est requis";
- }
- else
- {
- categoriesListIsValid = true;
- textBlockErrorCategories.Text = "";
- }
- }
- private void AddProductToDatabase_Click(object sender, RoutedEventArgs e)
- {
- NameProductValidation();
- imageValidation();
- DescriptionValidation();
- ReferenceValidation();
- PriceValidation();
- QuantityValidation();
- CategoriesListValidation();
- float.TryParse(textBoxPriceProduct.Text, out float price);
- int.TryParse(textBoxQuantityProduct.Text, out int quantity);
- if (categoriesComboBox.SelectedValue == null)
- {
- categoriesListIsValid = false;
- textBlockErrorCategories.Text = "Ce champ est requis";
- }
- if (nameProductIsValid && descriptionProductIsValid && referenceProductIsValid && priceProductIsValid && quantityProductIsValid && categoriesListIsValid)
- {
- try
- {
- Models.products addProduct = new Models.products()
- {
- name = textBoxProductName.Text,
- image = "default",
- description = textBoxDescriptionProduct.Text,
- prix = price,
- quantity = quantity,
- reference = textBoxReferenceProduct.Text,
- idUser = userConnected.IdUserConnected,
- idCategories = Convert.ToInt32(categoriesComboBox.SelectedValue)
- };
- _db.products.Add(addProduct);
- _db.SaveChanges();
- MessageBox.Show("Ajout d'un produit avec succès", "Succès", MessageBoxButton.OK, MessageBoxImage.Information);
- textBoxProductName.Text = null;
- textBoxDescriptionProduct.Text = null;
- textBoxPriceProduct.Text = null;
- textBoxQuantityProduct.Text = null;
- textBoxReferenceProduct.Text = null;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- /// <summary>
- /// Méthode afin d'acceder a la fenêtre menuAdmin
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnBack_Click(object sender, RoutedEventArgs e)
- {
- menuAdmin windowMenuAdmin = new menuAdmin();
- windowMenuAdmin.Show();
- this.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement