/// <summary> /// Adds a new article in the DB /// </summary> /// <param name="Obj">Object article to add in the DB.</param> public override void AddElement(Object Obj) { Articles Article = (Articles)Obj; Articles ArticleFound = Manager.GetArticle(Article.Reference); if (ArticleFound == null) { Manager.InsertArticle(Article); } else { throw new Exception("L'article de référence " + ArticleFound.Reference + " existe déja dans la base"); } }
/// <summary> /// Add of modify the article when the user has completed the form /// </summary> /// <param name="Sender"></param> /// <param name="E"></param> private void Btn_Valider_Click(object Sender, EventArgs E) { if (!CheckEntries()) { MessageBox.Show("Certains champs ne sont pas valides !", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { string NameMessage = ""; try { if (Article == null)//ajout { NameMessage = "L'ajout "; Article = new Articles( Tbx_Reference.Text, Tbx_Description.Text, ((KeyValuePair <int, string>)Cbx_Famille.SelectedItem).Key, ((KeyValuePair <int, string>)Cbx_SousFamille.SelectedItem).Key, ((KeyValuePair <int, string>)Cbx_Marque.SelectedItem).Key, Convert.ToDouble(Tbx_Prix.Text), Convert.ToInt32(Tbx_Quantite.Text) ); ControllerArticles.AddElement(Article); this.DialogResult = DialogResult.OK; } else//modification { NameMessage = "La modification "; Article.Description = Tbx_Description.Text; Article.IdFamille = ((KeyValuePair <int, string>)Cbx_Famille.SelectedItem).Key; Article.IdSousFamille = ((KeyValuePair <int, string>)Cbx_SousFamille.SelectedItem).Key; Article.IdMarque = ((KeyValuePair <int, string>)Cbx_Marque.SelectedItem).Key; Article.PrixHT = Convert.ToDouble(Tbx_Prix.Text); Article.Quantite = Convert.ToInt32(Tbx_Quantite.Text); ControllerArticles.ChangeElement(Article); this.DialogResult = DialogResult.OK; } this.Close(); } catch (Exception Ex) { this.DialogResult = DialogResult.Cancel; MessageBox.Show("Une erreur est survenue lors de " + NameMessage.ToLower() + "avec le message suivant:\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Comfort constructor - Init attributes /// </summary> /// <param name="Filename"> Filename of the xml file gave by the view </param> public ControllerParserXML(string Filename) { this.Filename = Filename; ArgsEvent = new MyEventArgs(); DbManager = new DBManager(); XmlDocument = new XmlDocument(); Article = null; // Init dictionary. One entry = one enum CounterTypeMessage = new Dictionary <TypeMessage, int>(); foreach (TypeMessage Foo in Enum.GetValues(typeof(TypeMessage))) { CounterTypeMessage.Add(Foo, 0); } }
/// <summary> /// Called by childs when they want to add an article to the database /// </summary> protected void AddArticle() { Article = new Articles(); Article.Description = Node.SelectSingleNode("description").InnerText; Article.Reference = Node.SelectSingleNode("refArticle").InnerText; TreatFamille(); if (!TreatSousFamille()) // Check if the subFamily correspond to the good family. If mistake, generate error signal and the article won't be updatd. { TreatMarque(); Article.PrixHT = Convert.ToDouble(Node.SelectSingleNode("prixHT").InnerText); Article.Quantite = 1; DbManager.InsertArticle(Article); UpdateListView(TypeMessage.Succès, SubjectMessage.Ajouter_article, "L'article " + Article.Reference + " a été ajouté"); } }
/// <summary> /// Changes the values about an article stored in the DB depending on the reference /// </summary> /// <param name="Obj">Object article with the changes</param> /// <returns>Returns the number of articles updated</returns> public override int ChangeElement(Object Obj) { int Count; Articles Article = (Articles)(Obj); if (Manager.GetArticle(Article.Reference) != null) { Count = Manager.UpdateArticle(Article); if (Count != 1) { throw new Exception("Une erreur liée à la base de données à empêcher la modification de l'article de référence " + Article.Reference); } } else { throw new Exception("L'article de référence " + Article.Reference + " n'existe pas dans la base"); } return(Count); }
/// <summary> /// Get one article by reference /// </summary> /// <param name="Reference"> The reference of the article to get </param> /// <returns> The article searched </returns> public Articles GetArticle(string Reference) { Articles Article = new Articles(); SQLiteCommand Sql = new SQLiteCommand("SELECT * FROM Articles WHERE RefArticle = @reference", Conn); Sql.Parameters.AddWithValue("@reference", Reference); SQLiteDataReader Reader = Sql.ExecuteReader(); if (Reader.Read()) { Article.ConvertDataReaderToArticles(Reader); Article.IdFamille = GetSousFamille(Id: Convert.ToInt32(Reader.GetValue(2))).IdFamille; // Get the Famille linked to the sousFamille to return a complete article return(Article); } else { return(null); } }
/// <summary> /// Update an article to the database /// </summary> /// <param name="Article"> The article to update </param> /// <returns> The number of rticle updated </returns> public int UpdateArticle(Articles Article) { SQLiteCommand Sql = new SQLiteCommand( "UPDATE Articles SET description = @description, RefSousFamille = @idSousFamille, RefMarque = @idMarque, PrixHT = @prixHT, Quantite = @quantite " + "WHERE RefArticle = @reference", Conn); Sql.Parameters.AddWithValue("@reference", Article.Reference); Sql.Parameters.AddWithValue("@description", Article.Description); Sql.Parameters.AddWithValue("@idSousFamille", Article.IdSousFamille); Sql.Parameters.AddWithValue("@idMarque", Article.IdMarque); Sql.Parameters.AddWithValue("@prixHT", Article.PrixHT); Sql.Parameters.AddWithValue("@quantite", Article.Quantite); try { return(Sql.ExecuteNonQuery()); } catch (Exception Ex) { throw new Exception(Ex.Message); } }
/// <summary> /// Insert article to the database /// </summary> /// <param name="Article"> The article to add </param> /// <returns> The id of the article added </returns> public int InsertArticle(Articles Article) { SQLiteCommand Sql = new SQLiteCommand( "INSERT INTO Articles (RefArticle, Description, RefSousFamille, RefMarque, PrixHT, Quantite) VALUES (@reference, @description, @idSousFamille, @idMarque, @prixHT, @quantite)", Conn); Sql.Parameters.AddWithValue("@reference", Article.Reference); Sql.Parameters.AddWithValue("@description", Article.Description); Sql.Parameters.AddWithValue("@idSousFamille", Article.IdSousFamille); Sql.Parameters.AddWithValue("@idMarque", Article.IdMarque); Sql.Parameters.AddWithValue("@prixHT", Article.PrixHT); Sql.Parameters.AddWithValue("@quantite", Article.Quantite); try { Sql.ExecuteNonQuery(); return(Convert.ToInt32(Conn.LastInsertRowId)); } catch (Exception Ex) { throw new Exception(Ex.Message); } }
/// <summary> /// Get all article of the database /// </summary> /// <param name="Columnsort"> Column to sort </param> /// <param name="Ascending"> The direction </param> /// <returns> The list of all article </returns> public Dictionary <string, Articles> GetAllArticles(string Columnsort = "RefArticle", bool Ascending = true) { string Order = "ASC"; if (!Ascending) { Order = "DESC"; } Dictionary <string, Articles> DictionaryArticles = new Dictionary <string, Articles>(); SQLiteCommand Sql = new SQLiteCommand("select * from Articles order by " + Columnsort + " " + Order, Conn); SQLiteDataReader Reader = Sql.ExecuteReader(); while (Reader.Read()) { Articles Article = new Articles(); Article.ConvertDataReaderToArticles(Reader); // Set attributes to the article thanks to the reader Article.IdFamille = GetSousFamille(Id: Article.IdSousFamille).IdFamille; DictionaryArticles[Article.Reference] = Article; } return(DictionaryArticles); }