diff --git a/.gitignore b/.gitignore index 02b32dc..cc7298e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +ConnectionString.txt + # User-specific files *.rsuser *.suo @@ -365,3 +367,5 @@ FodyWeavers.xsd .csproj /Gallium v1/Data/ConnexionDAO.cs /Gallium v1/Data/InformatioConnexion.cs + +.idea \ No newline at end of file diff --git a/Couche IHM/Couche Data/Couche Data.csproj b/Couche IHM/Couche Data/Couche Data.csproj index 3e3f7b3..a05696a 100644 --- a/Couche IHM/Couche Data/Couche Data.csproj +++ b/Couche IHM/Couche Data/Couche Data.csproj @@ -8,6 +8,16 @@ + + + + + + + + + + diff --git a/Couche IHM/Couche Data/Dao/UserDAO.cs b/Couche IHM/Couche Data/Dao/AccountDAO.cs similarity index 50% rename from Couche IHM/Couche Data/Dao/UserDAO.cs rename to Couche IHM/Couche Data/Dao/AccountDAO.cs index dad4623..cfbf1cd 100644 --- a/Couche IHM/Couche Data/Dao/UserDAO.cs +++ b/Couche IHM/Couche Data/Dao/AccountDAO.cs @@ -5,17 +5,16 @@ namespace Couche_Data.Dao { - - public class UserDAO : IUserDAO + public class AccountDAO : IAccountDao { - public void CreateCompte(User compte) + public void CreateAdhérent(Account adhérent) { //Connection dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = $"INSERT INTO users VALUES (0,'{compte.Prenom}','{compte.Nom}','{compte.Mail}','{compte.HashedPassword}',{compte.IdRole})"; + string stm = $"INSERT INTO User VALUES (0,'{adhérent.Identifiant}','{adhérent.Prenom}','{adhérent.Nom}','{adhérent.Mail}',{adhérent.RoleId},'{adhérent.Formation}',{adhérent.Argent},{adhérent.IsMember},' ',' ','2030-11-25 00:00:00')"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); @@ -24,87 +23,94 @@ public void CreateCompte(User compte) using (MySqlCommand selectCommand = new MySqlCommand("SELECT LAST_INSERT_ID() AS nouvel_id", dbsDAO.Instance.Sql)) { int nouvelId = Convert.ToInt32(selectCommand.ExecuteScalar()); - compte.ID = nouvelId; - + adhérent.Id = nouvelId; } + dbsDAO.Instance.CloseDatabase(); } - public List GetComptes() + + public List GetAdhérents() { //Connection dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = "SELECT * FROM users ORDER BY firstname"; + string stm = "SELECT * FROM User ORDER BY userId"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); //lecture de la requette MySqlDataReader rdr = cmd.ExecuteReader(); - List users = new List(); + + List acomptes = new List(); while (rdr.Read()) { - users.Add(new User(rdr.GetInt32("user_id"), rdr.GetString("lastname"), rdr.GetString("firstname"), rdr.GetString("email"), rdr.GetString("password"), rdr.GetInt16("grade_id"))); + acomptes.Add(new Account(rdr.GetInt32("id"), rdr.GetString("userId"), rdr.GetString("lastName"), rdr.GetString("firstName"), rdr.GetString("email"), rdr.GetFloat("deposit"),rdr.GetString("year"),rdr.GetBoolean("isMember"), rdr.GetInt16("role"))); } - + rdr.Close(); dbsDAO.Instance.CloseDatabase(); - return users; + return acomptes; } + public List GetRoles() { + //Connection dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm2 = "SELECT * FROM grades"; - MySqlCommand cmd2 = new MySqlCommand(stm2, dbsDAO.Instance.Sql); - cmd2.Prepare(); + string stm = "SELECT * FROM Role"; + MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); + cmd.Prepare(); //lecture de la requette - MySqlDataReader rdr2 = cmd2.ExecuteReader(); + MySqlDataReader rdr = cmd.ExecuteReader(); + List roles = new List(); - while (rdr2.Read()) + while (rdr.Read()) { - roles.Add(new Role(rdr2.GetInt32("grade_id"), rdr2.GetString("name"))); + roles.Add(new Role(rdr.GetInt32("id"), rdr.GetString("name"))); } - rdr2.Close(); + + rdr.Close(); dbsDAO.Instance.CloseDatabase(); return roles; } - public void RemoveCompte(User compte) + public void RemoveAdhérent(Account adhérent) { //Connection dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = $"DELETE FROM users WHERE user_id = {compte.ID}"; + string stm = $"DELETE FROM User WHERE id = {adhérent.Id}"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); + //lecture de la requette cmd.ExecuteNonQuery(); dbsDAO.Instance.CloseDatabase(); } - - public void UpdateCompte(User compte) + public void UpdateAdhérent(Account adhérent) { //Connection dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = $"UPDATE users SET firstname = '{compte.Prenom}',lastname = '{compte.Nom}',email = '{compte.Mail}',password = '{compte.HashedPassword}',grade_id = {compte.IdRole} WHERE user_id = {compte.ID}"; + + string formattedAmountMoney = adhérent.Argent.ToString(System.Globalization.CultureInfo.InvariantCulture); + string stm = $"UPDATE User set lastName = '{adhérent.Nom}', firstName = '{adhérent.Prenom}', deposit = {formattedAmountMoney}, isMember = {adhérent.IsMember}, role = {adhérent.RoleId} , email = '{adhérent.Mail}' ,year = '{adhérent.Formation}',password = '{adhérent.HashedPassword}', userId = '{adhérent.Identifiant}' WHERE id = {adhérent.Id}"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); - + + //lecture de la requette cmd.ExecuteNonQuery(); dbsDAO.Instance.CloseDatabase(); - } - + } } - } diff --git a/Couche IHM/Couche Data/Dao/AcompteDAO.cs b/Couche IHM/Couche Data/Dao/AcompteDAO.cs deleted file mode 100644 index 8ecb5ef..0000000 --- a/Couche IHM/Couche Data/Dao/AcompteDAO.cs +++ /dev/null @@ -1,91 +0,0 @@ - -using Couche_Data.Interfaces; -using Modeles; -using MySql.Data.MySqlClient; - -namespace Couche_Data.Dao -{ - public class AcompteDAO : IAcompteDao - { - - public void CreateAdhérent(Acompte adhérent) - { - //Connection - dbsDAO.Instance.OpenDataBase(); - - //Requette SQL - string stm = $"INSERT INTO acompte VALUES (0,'{adhérent.Nom}','{adhérent.Prenom}',{adhérent.Argent},{adhérent.StillAdherent},'{adhérent.Formation}','{adhérent.Identifiant}')"; - MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); - cmd.Prepare(); - - //lecture de la requette - cmd.ExecuteNonQuery(); - using (MySqlCommand selectCommand = new MySqlCommand("SELECT LAST_INSERT_ID() AS nouvel_id", dbsDAO.Instance.Sql)) - { - int nouvelId = Convert.ToInt32(selectCommand.ExecuteScalar()); - adhérent.Id = nouvelId; - } - - dbsDAO.Instance.CloseDatabase(); - } - - - public List GetAdhérents() - { - //Connection - dbsDAO.Instance.OpenDataBase(); - - //Requette SQL - string stm = "SELECT * FROM acompte ORDER BY login"; - MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); - cmd.Prepare(); - - //lecture de la requette - MySqlDataReader rdr = cmd.ExecuteReader(); - - List acomptes = new List(); - while (rdr.Read()) - { - acomptes.Add(new Acompte(rdr.GetInt32("acompte_id"), rdr.GetString("login"), rdr.GetString("nom"), rdr.GetString("prenom"), rdr.GetFloat("balance"), rdr.GetString("formation"),rdr.GetBoolean("isAdherent"))); - } - - rdr.Close(); - dbsDAO.Instance.CloseDatabase(); - return acomptes; - } - - public void RemoveAdhérent(Acompte adhérent) - { - //Connection - dbsDAO.Instance.OpenDataBase(); - - //Requette SQL - string stm = $"DELETE FROM acompte WHERE acompte_id = {adhérent.Id}"; - MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); - cmd.Prepare(); - - //lecture de la requette - cmd.ExecuteNonQuery(); - - dbsDAO.Instance.CloseDatabase(); - } - - public void UpdateAdhérent(Acompte adhérent) - { - //Connection - dbsDAO.Instance.OpenDataBase(); - - //Requette SQL - string formattedAmountMoney = adhérent.Argent.ToString(System.Globalization.CultureInfo.InvariantCulture); - string stm = $"UPDATE acompte set nom = '{adhérent.Nom}', prenom = '{adhérent.Prenom}', balance = {formattedAmountMoney}, isAdherent = {adhérent.StillAdherent}, formation = '{adhérent.Formation}', login = '{adhérent.Identifiant}' WHERE acompte_id = {adhérent.Id}"; - MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); - cmd.Prepare(); - - //lecture de la requette - cmd.ExecuteNonQuery(); - - dbsDAO.Instance.CloseDatabase(); - - } - } -} diff --git a/Couche IHM/Couche Data/Dao/CachedLogReader.cs b/Couche IHM/Couche Data/Dao/CachedLogReader.cs new file mode 100644 index 0000000..734234a --- /dev/null +++ b/Couche IHM/Couche Data/Dao/CachedLogReader.cs @@ -0,0 +1,68 @@ +using Couche_Data.Interfaces; +using Modeles; +using System.Threading.Tasks.Dataflow; + +namespace Couche_Data.Dao +{ + public class CachedLogReader : IPaginatedLogReader + { + private List allLogs; + private BufferBlock buffer; + private int pageSize; + private int pageIndex; + private bool exhausted; + private object loadLock; + + public CachedLogReader(int pageSize, List allLogs) + { + this.allLogs = allLogs; + buffer = new BufferBlock(); + this.pageSize = pageSize; + pageIndex = 0; + exhausted = false; + loadLock = new object(); + } + + public int PageSize => pageSize; + + public IAsyncEnumerable GetAsyncStream(CancellationToken ct = default) => buffer.ReceiveAllAsync(ct); + + public void LoadNextPage() + { + lock (loadLock) + { + if (!exhausted) + { + int pageStart = pageIndex * pageSize; + int pageEnd = pageStart + pageSize; + + if (pageEnd > allLogs.Count) + { + pageEnd = allLogs.Count; + exhausted = true; + } + else + { + pageIndex++; + } + + for (int i = pageStart; i < pageEnd; i++) + { + buffer.Post(allLogs[i]); + } + } + } + } + + public void Reset() + { + // désactiver l'ancien tampon et en créer un nouveau + buffer.Complete(); + buffer = new BufferBlock(); + + // remettre le curseur au début + pageIndex = 0; + exhausted = false; + } + } +} diff --git a/Couche IHM/Couche Data/Dao/LogDAO.cs b/Couche IHM/Couche Data/Dao/LogDAO.cs index 05df1bd..4983bf6 100644 --- a/Couche IHM/Couche Data/Dao/LogDAO.cs +++ b/Couche IHM/Couche Data/Dao/LogDAO.cs @@ -1,45 +1,48 @@ -using Modeles; +using Couche_Data.Interfaces; +using Modeles; using MySql.Data.MySqlClient; namespace Couche_Data.Dao { - public class LogDAO + public class LogDAO : ILogDAO { public void CreateLog(Log log) { - lock (dbsDAO.Instance.DatabaseLock) - { - //Connection - dbsDAO.Instance.OpenDataBase(); + string connString = dbsDAO.ConnectionString; + MySqlConnection sql = new MySqlConnection(connString); - //Requette SQL - string formattedDate = log.Date.ToString("yyyy-MM-dd HH:mm:ss"); - string stm = $"INSERT INTO logs VALUES(0,'{log.Message}','{formattedDate}',{log.Theme},'{log.Auteur}')"; - MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); - cmd.Prepare(); + //Connection + sql.Open(); - //lecture de la requette - cmd.ExecuteNonQuery(); + //Requette SQL + string formattedDate = log.Date.ToString("yyyy-MM-dd HH:mm:ss"); + string stm = $"INSERT INTO logs VALUES(0,@message,'{formattedDate}',{log.Theme},'{log.Auteur}')"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Parameters.AddWithValue("@message", log.Message); + cmd.Prepare(); - dbsDAO.Instance.CloseDatabase(); - } + //lecture de la requette + cmd.ExecuteNonQuery(); + + sql.Close(); + } - public List GetLogs(int mois,int annee) + public List GetLogs(int mois, int annee) { //Connection - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_gallium", "none"); + string connString = dbsDAO.ConnectionString; MySqlConnection sql = new MySqlConnection(connString); sql.Open(); //Requette SQL - string stm = $"SELECT * FROM Logs WHERE YEAR(date_at) = {annee} AND MONTH(date_at) = {mois} ORDER BY date_at DESC"; + string stm = $"SELECT * FROM logs WHERE YEAR(date_at) = {annee} AND MONTH(date_at) = {mois} ORDER BY date_at DESC"; MySqlCommand cmd = new MySqlCommand(stm, sql); cmd.Prepare(); //lecture de la requette - MySqlDataReader rdr = cmd.ExecuteReader(); + MySqlDataReader rdr = cmd.ExecuteReader(); List logs = new List(); while (rdr.Read()) @@ -51,9 +54,9 @@ public List GetLogs(int mois,int annee) return logs; } - - - - + public IPaginatedLogReader GetLogsReader(int mois, int annee) + { + return new CachedLogReader(20, this.GetLogs(mois, annee)); + } } } diff --git a/Couche IHM/Couche Data/Dao/OrderDAO.cs b/Couche IHM/Couche Data/Dao/OrderDAO.cs new file mode 100644 index 0000000..dcf975a --- /dev/null +++ b/Couche IHM/Couche Data/Dao/OrderDAO.cs @@ -0,0 +1,78 @@ +using Couche_Data.Interfaces; +using Modeles; +using MySql.Data.MySqlClient; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Couche_Data.Dao +{ + public class OrderDAO : IOrderDao + { + public void ProcessOrder(Order order) + { + //Connection + dbsDAO.Instance.OpenDataBase(); + + float prix = 0; + //Requette SQL + for (int i=0; i GetProducts() dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = "SELECT * FROM products ORDER BY name"; + string stm = "SELECT * FROM Product ORDER BY name"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); @@ -50,8 +50,8 @@ public List GetProducts() while (rdr.Read()) { - int categoryId = rdr.IsDBNull(rdr.GetOrdinal("category_id")) ? -1 : rdr.GetInt32("category_id"); - products.Add(new Product(rdr.GetInt32("product_id"), rdr.GetString("name"), rdr.GetInt32("stock"), rdr.GetFloat("price_na"), rdr.GetFloat("price_a"), categoryId)); + int categoryId = rdr.IsDBNull(rdr.GetOrdinal("category")) ? -1 : rdr.GetInt32("category"); + products.Add(new Product(rdr.GetInt32("id"), rdr.GetString("name"), rdr.GetInt32("stock"), rdr.GetFloat("nonMemberPrice"), rdr.GetFloat("memberPrice"), categoryId)); } rdr.Close(); dbsDAO.Instance.CloseDatabase(); @@ -64,7 +64,7 @@ public void RemoveProduct(Product product) dbsDAO.Instance.OpenDataBase(); //Requette SQL - string stm = $"DELETE FROM products WHERE product_id = {product.ID}"; + string stm = $"DELETE FROM Product WHERE id = {product.ID}"; MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); @@ -77,22 +77,21 @@ public void RemoveProduct(Product product) public void UpdateProduct(Product product) { - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - MySqlConnection sql = new MySqlConnection(connString); - sql.Open(); + //Connection + dbsDAO.Instance.OpenDataBase(); //Requette SQL string prixA = product.PrixAdherent.ToString(System.Globalization.CultureInfo.InvariantCulture); string prixNA = product.PrixNonAdherent.ToString(System.Globalization.CultureInfo.InvariantCulture); - string stm = $"UPDATE products SET name = '{product.NomProduit}', stock = {product.Quantite}, price_a = {prixA}, price_na = {prixNA}, category_id = {product.Categorie} WHERE product_id = {product.ID}"; - MySqlCommand cmd = new MySqlCommand(stm, sql); + string stm = $"UPDATE Product SET name = '{product.NomProduit}', stock = {product.Quantite}, memberPrice = {prixA}, nonMemberPrice = {prixNA}, category = {product.Categorie} WHERE id = {product.ID}"; + MySqlCommand cmd = new MySqlCommand(stm, dbsDAO.Instance.Sql); cmd.Prepare(); //lecture de la requette cmd.ExecuteNonQuery(); - sql.Close(); - + dbsDAO.Instance.CloseDatabase(); + } } diff --git a/Couche IHM/Couche Data/Dao/StatAcompteDAO.cs b/Couche IHM/Couche Data/Dao/StatAcompteDAO.cs index 73749fa..61e2a40 100644 --- a/Couche IHM/Couche Data/Dao/StatAcompteDAO.cs +++ b/Couche IHM/Couche Data/Dao/StatAcompteDAO.cs @@ -1,57 +1,175 @@ -using Modeles; +using Couche_Data.Interfaces; +using Modeles; using MySql.Data.MySqlClient; - +using System.Diagnostics; namespace Couche_Data.Dao { - public class StatAcompteDAO + public class StatAccountDAO : IStatAccountDAO { - public List GetStat() + public List GetStatByWeek(int semaine, int year) + { + //Connection + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + + //Requette SQL + string stm = $"SELECT acompte_id,sum(amount) as argent FROM best_acomptes WHERE WEEK(date) = {semaine} AND " + + $"YEAR(date) = {year} group by acompte_id order by argent desc"; + + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); + + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statAccountList = new List(); + while (rdr.Read()) + { + statAccountList.Add(new StatAccount(0, DateTime.Now, rdr.GetFloat("argent"), rdr.GetInt32("acompte_id"))); + } + + rdr.Close(); + sql.Close(); + return statAccountList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats acompte : {ex.Message}"); + return new(); + } + } + + public List GetStatByMonth(int month, int year) { //Connection - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - MySqlConnection sql = new MySqlConnection(connString); - sql.Open(); + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); - //Requette SQL - string stm = "SELECT acompte_id,sum(amount) as argent FROM best_acomptes WHERE YEARWEEK(date) = YEARWEEK(CURRENT_DATE) group by acompte_id order by argent desc"; - MySqlCommand cmd = new MySqlCommand(stm, sql); - cmd.Prepare(); + //Requette SQL + string stm = $"SELECT acompte_id,sum(amount) as argent FROM best_acomptes WHERE month(date) = {month} AND " + + $"YEAR(date) = {year} group by acompte_id order by argent desc"; - //lecture de la requette - MySqlDataReader rdr = cmd.ExecuteReader(); + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); - List statAcompteList = new List(); - while (rdr.Read()) + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statAccountList = new List(); + while (rdr.Read()) + { + statAccountList.Add(new StatAccount(0, DateTime.Now, rdr.GetFloat("argent"), rdr.GetInt32("acompte_id"))); + } + + rdr.Close(); + sql.Close(); + return statAccountList; + } + catch (Exception ex) { - statAcompteList.Add(new StatAcompte(0, DateTime.Now, rdr.GetFloat("argent"), rdr.GetInt16("acompte_id"))); + Debug.WriteLine($"Erreur pendant le chargement des stats acompte : {ex.Message}"); + return new(); } + } + + public List GetStatByYear(int year) + { + //Connection + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); - rdr.Close(); - sql.Close(); - return statAcompteList; + //Requette SQL + string stm = $"SELECT acompte_id,sum(amount) as argent FROM best_acomptes WHERE " + + $"YEAR(date) = {year} group by acompte_id order by argent desc"; + + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); + + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statAccountList = new List(); + while (rdr.Read()) + { + statAccountList.Add(new StatAccount(0, DateTime.Now, rdr.GetFloat("argent"), rdr.GetInt32("acompte_id"))); + } + + rdr.Close(); + sql.Close(); + return statAccountList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats acompte : {ex.Message}"); + return new(); + } } + public void CreateStat(StatAccount stat) + { + //Connection + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + + //Requette SQL + string formattedDate = stat.Date.ToString("yyyy-MM-dd"); + string formattedAmountMoney = stat.Money.ToString(System.Globalization.CultureInfo.InvariantCulture); + string stm = $"INSERT INTO best_acomptes VALUES(0,{formattedAmountMoney},'{formattedDate}',{stat.Account_Id})"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); + //lecture de la requette + cmd.ExecuteNonQuery(); - public void CreateStat(StatAcompte stat) + sql.Close(); + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant l'ajout d'une stat acompte : {ex.Message}"); + } + } + + public List GetStatBOfAcompteByMonth(int year, int acompte_id) { - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - MySqlConnection sql = new MySqlConnection(connString); - sql.Open(); + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); - //Requette SQL - string formattedDate = stat.Date.ToString("yyyy-MM-dd"); - string formattedAmountMoney = stat.Money.ToString(System.Globalization.CultureInfo.InvariantCulture); - string stm = $"INSERT INTO best_acomptes VALUES(0,{formattedAmountMoney},'{formattedDate}',{stat.Acompte_Id})"; - MySqlCommand cmd = new MySqlCommand(stm, sql); - cmd.Prepare(); + //Requette SQL - //lecture de la requette - cmd.ExecuteNonQuery(); + string stm = $"SELECT NVL(acompte_id,{acompte_id}) as acompte_id,lm.mois as date,NVL(SUM(bs.amount), 0) AS argent FROM ( SELECT 1 AS mois UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) lm LEFT JOIN best_acomptes bs ON MONTH(bs.date) = lm.mois AND YEAR(bs.date) = {year} AND bs.acompte_id = {acompte_id} GROUP BY lm.mois ORDER BY lm.mois;"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); - sql.Close(); + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statAccountList = new List(); + while (rdr.Read()) + { + statAccountList.Add(new StatAccount(0, new DateTime(year, rdr.GetInt16("date"), 1), rdr.GetFloat("argent"), rdr.GetInt32("acompte_id"))); + } + + rdr.Close(); + sql.Close(); + return statAccountList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats acompte : {ex.Message}"); + return new(); + } } } } diff --git a/Couche IHM/Couche Data/Dao/StatProduitDAO.cs b/Couche IHM/Couche Data/Dao/StatProduitDAO.cs index dd329d0..d6a4aa8 100644 --- a/Couche IHM/Couche Data/Dao/StatProduitDAO.cs +++ b/Couche IHM/Couche Data/Dao/StatProduitDAO.cs @@ -1,57 +1,163 @@ -using Modeles; +using Couche_Data.Interfaces; +using Modeles; using MySql.Data.MySqlClient; - +using System.Diagnostics; namespace Couche_Data.Dao { - public class StatProduitDAO + public class StatProduitDAO : IStatProduitDAO { + public List GetStatByYear(int year) + { + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + + //Requette SQL + string stm = $"SELECT product_id,sum(number_sales) as nb FROM best_sales WHERE YEAR(date_sale) = {year} group by product_id order by nb desc"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); + + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statProduitList = new List(); + while (rdr.Read()) + { + statProduitList.Add(new StatProduit(0, DateTime.Now, rdr.GetInt16("nb"), rdr.GetInt16("product_id"))); + } + + rdr.Close(); + sql.Close(); + return statProduitList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats produit : {ex.Message}"); + return new(); + } + } - public List GetStat() + public List GetStatByWeek(int semaine, int year) { - //Connection - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - MySqlConnection sql = new MySqlConnection(connString); - sql.Open(); + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + + //Requette SQL + string stm = $"SELECT product_id,sum(number_sales) as nb FROM best_sales WHERE week(date_sale) = {semaine} AND YEAR(date_sale) = {year} group by product_id order by nb desc"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); - //Requette SQL - string stm = "SELECT product_id,sum(number_sales) as nb FROM best_sales WHERE YEARWEEK(date_sale) = YEARWEEK(CURRENT_DATE) group by product_id order by nb desc"; - MySqlCommand cmd = new MySqlCommand(stm, sql); - cmd.Prepare(); + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); - //lecture de la requette - MySqlDataReader rdr = cmd.ExecuteReader(); + List statProduitList = new List(); + while (rdr.Read()) + { + statProduitList.Add(new StatProduit(0, DateTime.Now, rdr.GetInt16("nb"), rdr.GetInt16("product_id"))); + } - List statProduitList = new List(); - while (rdr.Read()) + rdr.Close(); + sql.Close(); + return statProduitList; + } + catch (Exception ex) { - statProduitList.Add(new StatProduit(0, DateTime.Now, rdr.GetInt16("nb"), rdr.GetInt16("product_id"))); + Debug.WriteLine($"Erreur pendant le chargement des stats produit : {ex.Message}"); + return new(); } + } + + public void CreateStat(StatProduit stat) + { + //Connection + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); - rdr.Close(); - sql.Close(); - return statProduitList; + //Requette SQL + string formattedDate = stat.Date.ToString("yyyy-MM-dd"); + string stm = $"INSERT INTO best_sales VALUES(0,'{formattedDate}',{stat.Number_sales},{stat.Product_id})"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); + + //lecture de la requette + cmd.ExecuteNonQuery(); + + sql.Close(); + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant l'ajout d'une stat produit : {ex.Message}"); + } } + public List GetStatByMonth(int month, int year) + { + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + //Requette SQL + string stm = $"SELECT product_id,sum(number_sales) as nb FROM best_sales WHERE month(date_sale) = {month} AND YEAR(date_sale) = {year} group by product_id order by nb desc"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); - public void CreateStat(StatProduit stat) + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); + + List statProduitList = new List(); + while (rdr.Read()) + { + statProduitList.Add(new StatProduit(0, DateTime.Now, rdr.GetInt16("nb"), rdr.GetInt16("product_id"))); + } + + rdr.Close(); + sql.Close(); + return statProduitList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats produit : {ex.Message}"); + return new(); + } + } + + public List GetStatBOfProductByMonth(int year,int product_id) { - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - MySqlConnection sql = new MySqlConnection(connString); - sql.Open(); + MySqlConnection sql = new MySqlConnection(dbsDAO.ConnectionString); + try + { + sql.Open(); + + //Requette SQL + string stm = $"SELECT NVL(product_id,{product_id}) as product_id,lm.mois as date,NVL(SUM(bs.number_sales), 0) AS nb FROM ( SELECT 1 AS mois UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) lm LEFT JOIN best_sales bs ON MONTH(bs.date_sale) = lm.mois AND YEAR(bs.date_sale) = {year} AND bs.product_id = {product_id} GROUP BY lm.mois ORDER BY lm.mois;"; + MySqlCommand cmd = new MySqlCommand(stm, sql); + cmd.Prepare(); - //Requette SQL - string formattedDate = stat.Date.ToString("yyyy-MM-dd"); - string stm = $"INSERT INTO best_sales VALUES(0,'{formattedDate}',{stat.Number_sales},{stat.Product_id})"; - MySqlCommand cmd = new MySqlCommand(stm, sql); - cmd.Prepare(); + //lecture de la requette + MySqlDataReader rdr = cmd.ExecuteReader(); - //lecture de la requette - cmd.ExecuteNonQuery(); + List statProduitList = new List(); + while (rdr.Read()) + { + statProduitList.Add(new StatProduit(0, rdr.GetDateTime("date"), rdr.GetInt16("nb"), rdr.GetInt16("product_id"))); + } - sql.Close(); - + rdr.Close(); + sql.Close(); + return statProduitList; + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur pendant le chargement des stats produit : {ex.Message}"); + return new(); + } } } } diff --git a/Couche IHM/Couche Data/Dao/dbsDAO.cs b/Couche IHM/Couche Data/Dao/dbsDAO.cs index 4664d20..c178921 100644 --- a/Couche IHM/Couche Data/Dao/dbsDAO.cs +++ b/Couche IHM/Couche Data/Dao/dbsDAO.cs @@ -1,4 +1,5 @@ using MySql.Data.MySqlClient; +using System.Reflection; namespace Couche_Data.Dao { @@ -33,23 +34,7 @@ public static dbsDAO Instance } } - /// - /// Permet de faire des requêtes - /// - public MySqlCommand CMD - { - get => cmd; - set => cmd = value; - } - - /// - /// permet de lire les données - /// - public MySqlDataReader Reader - { - get => reader; - set => reader = value; - } + /// /// Vérifie si la connexion à la bdd existe @@ -75,13 +60,46 @@ public object DatabaseLock get { return databaseLock; } } + private static string? connectionString; + + public static string ConnectionString + { + get + { + var assembly = Assembly.GetExecutingAssembly(); + string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith("ConnectionString.txt")); + using Stream stream = assembly.GetManifestResourceStream(resourceName)!; + using StreamReader reader = new StreamReader(stream); + connectionString = reader.ReadToEnd(); + + return connectionString; + } + } + public static string ConnectionStringV + { + get + { + + var assembly = Assembly.GetExecutingAssembly(); + string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith("ConnectionString.txt")); + using Stream stream = assembly.GetManifestResourceStream(resourceName)!; + using StreamReader reader = new StreamReader(stream); + connectionString = reader.ReadToEnd(); + connectionString = connectionString.Replace("database=c2_gallium", "database=c2_etismash"); // TODO enlever cette ligne quand stat avec api + + return connectionString; + } + } + + + + /// /// Se connecte à la base de donnée /// private void ConnexionToBdd() { - string connString = String.Format("server={0};port={1};user id={2};password={3};database={4};SslMode={5}", "51.178.36.43", "3306", "c2_gallium", "DfD2no5UJc_nB", "c2_etismash", "none"); - this.sql = new MySqlConnection(connString); + sql = new MySqlConnection(ConnectionString); } /// diff --git a/Couche IHM/Couche Data/Interfaces/IAcompteDao.cs b/Couche IHM/Couche Data/Interfaces/IAcompteDao.cs index af60874..14f10e8 100644 --- a/Couche IHM/Couche Data/Interfaces/IAcompteDao.cs +++ b/Couche IHM/Couche Data/Interfaces/IAcompteDao.cs @@ -4,29 +4,33 @@ namespace Couche_Data.Interfaces { - public interface IAcompteDao + public interface IAccountDao { /// /// Permet de récupérer tous les adhérents /// - public List GetAdhérents(); + public List GetAdhérents(); + /// + /// Permet de récupérer tous les roles + /// + public List GetRoles(); /// /// Permet de supprimer un adhérent /// - public void RemoveAdhérent(Acompte adhérent); + public void RemoveAdhérent(Account adhérent); /// /// Permet de créer un nouvel adhérent /// - public void CreateAdhérent(Acompte adhérent); + public void CreateAdhérent(Account adhérent); /// /// Permet de modifier un adhérent /// - public void UpdateAdhérent(Acompte adhérent); + public void UpdateAdhérent(Account adhérent); } } diff --git a/Couche IHM/Couche Data/Interfaces/ILogDAO.cs b/Couche IHM/Couche Data/Interfaces/ILogDAO.cs new file mode 100644 index 0000000..49abdc9 --- /dev/null +++ b/Couche IHM/Couche Data/Interfaces/ILogDAO.cs @@ -0,0 +1,11 @@ +using Modeles; + +namespace Couche_Data.Interfaces +{ + public interface ILogDAO + { + void CreateLog(Log log); + List GetLogs(int mois, int annee); + IPaginatedLogReader GetLogsReader(int mois, int annee); + } +} \ No newline at end of file diff --git a/Couche IHM/Couche Data/Interfaces/IOrderDao.cs b/Couche IHM/Couche Data/Interfaces/IOrderDao.cs new file mode 100644 index 0000000..d24dd5d --- /dev/null +++ b/Couche IHM/Couche Data/Interfaces/IOrderDao.cs @@ -0,0 +1,14 @@ +using Modeles; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Couche_Data.Interfaces +{ + public interface IOrderDao + { + void ProcessOrder(Order order); + } +} diff --git a/Couche IHM/Couche Data/Interfaces/IPaginatedReader.cs b/Couche IHM/Couche Data/Interfaces/IPaginatedReader.cs new file mode 100644 index 0000000..255d3fd --- /dev/null +++ b/Couche IHM/Couche Data/Interfaces/IPaginatedReader.cs @@ -0,0 +1,36 @@ +using Modeles; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Couche_Data.Interfaces +{ + /// + /// Permet de récupérer les logs page par page. + /// + public interface IPaginatedLogReader + { + /// + /// Le nombre d'éléments par page. + /// + int PageSize { get; } + + /// + /// Charge la page suivante s'il reste des logs à charger + /// + void LoadNextPage(); + + /// + /// Récupère les logs de manière asynchrone au fur et à mesure qu'ils sont chargés. + /// + /// Un flux de logs. + IAsyncEnumerable GetAsyncStream(CancellationToken ct = default); + + /// + /// Réinitialise le lecteur au début. + /// + void Reset(); + } +} diff --git a/Couche IHM/Couche Data/Interfaces/IStatAcompteDAO.cs b/Couche IHM/Couche Data/Interfaces/IStatAcompteDAO.cs new file mode 100644 index 0000000..a9eea3e --- /dev/null +++ b/Couche IHM/Couche Data/Interfaces/IStatAcompteDAO.cs @@ -0,0 +1,13 @@ +using Modeles; + +namespace Couche_Data.Interfaces +{ + public interface IStatAccountDAO + { + void CreateStat(StatAccount stat); + List GetStatByWeek(int semaine,int year); + List GetStatByMonth(int month, int year); + List GetStatByYear(int year); + List GetStatBOfAcompteByMonth(int year, int acompte_id); + } +} \ No newline at end of file diff --git a/Couche IHM/Couche Data/Interfaces/IStatProduitDAO.cs b/Couche IHM/Couche Data/Interfaces/IStatProduitDAO.cs new file mode 100644 index 0000000..68ba752 --- /dev/null +++ b/Couche IHM/Couche Data/Interfaces/IStatProduitDAO.cs @@ -0,0 +1,13 @@ +using Modeles; + +namespace Couche_Data.Interfaces +{ + public interface IStatProduitDAO + { + void CreateStat(StatProduit stat); + List GetStatByWeek(int semaine,int year); + List GetStatByMonth(int month, int year); + List GetStatByYear(int year); + List GetStatBOfProductByMonth(int year, int product_id); + } +} \ No newline at end of file diff --git a/Couche IHM/Couche Data/Interfaces/IUserDAO.cs b/Couche IHM/Couche Data/Interfaces/IUserDAO.cs deleted file mode 100644 index 801436b..0000000 --- a/Couche IHM/Couche Data/Interfaces/IUserDAO.cs +++ /dev/null @@ -1,36 +0,0 @@ - -using Modeles; - -namespace Couche_Data.Interfaces -{ - - public interface IUserDAO - { - /// - /// Créer un compte - /// - public void CreateCompte(User compte); - - /// - /// Enlève un compte - /// - public void RemoveCompte(User compte); - - /// - /// Met à jour un compte - /// - public void UpdateCompte(User compte); - - - /// - /// Récupère tous les comptes - /// - public List GetComptes(); - - /// - /// Récupère tous les comptes - /// - public List GetRoles(); - - } -} diff --git a/Couche IHM/Couche IHM/Assets/Styles/style.xaml b/Couche IHM/Couche IHM/Assets/Styles/style.xaml index 46625e8..199443c 100644 --- a/Couche IHM/Couche IHM/Assets/Styles/style.xaml +++ b/Couche IHM/Couche IHM/Assets/Styles/style.xaml @@ -1,4 +1,5 @@  pack://application:,,,/Fonts/#There Brat @@ -24,6 +25,13 @@ + + + + @@ -450,7 +451,7 @@ Width="150" Margin="20,20,20,10" materialDesign:HintAssist.Hint="Prénom" - Text="{Binding AdherentViewModel.CurrentAcompte.PrenomIHM}" + Text="{Binding AccountsViewModel.CurrentAccount.PrenomIHM}" FontSize="16" Style="{StaticResource MaterialDesignFilledTextBox}" MaxLength="100"/> @@ -466,11 +467,8 @@ MaxLength="8"> - - - @@ -484,7 +482,7 @@ Style="{StaticResource MaterialDesignFilledTextBox}"> @@ -499,7 +497,7 @@ Opacity="0.7" Margin="20,20,20,10" materialDesign:HintAssist.Hint="Formation (optional)" - Text="{Binding AdherentViewModel.CurrentAcompte.FormationIHM}" + Text="{Binding AccountsViewModel.CurrentAccount.FormationIHM}" FontSize="16" Style="{StaticResource MaterialDesignFilledTextBox}"/> @@ -518,7 +516,7 @@ HorizontalAlignment="Center" materialDesign:CheckBoxAssist.CheckBoxSize="25" Grid.Row="10" - IsChecked="{Binding AdherentViewModel.CurrentAcompte.IsAdherentIHM}" + IsChecked="{Binding AccountsViewModel.CurrentAccount.IsAdherentIHM}" Background="Green" /> @@ -527,7 +525,7 @@ Grid.Row="11" FontSize="16" Margin="20,0,20,10" - Command="{Binding AdherentViewModel.CurrentAcompte.ResetAdh}" + Command="{Binding AccountsViewModel.CurrentAccount.ResetAdh}" Content="Annuler" BorderThickness="0" Background="Gray" @@ -540,15 +538,15 @@ Grid.Row="11" Content="Modifier" BorderThickness="0" - Command="{Binding AdherentViewModel.CurrentAcompte.ModifyAdherent}" + Command="{Binding AccountsViewModel.CurrentAccount.ModifyAdherent}" Background="{StaticResource galliumColor}"> @@ -134,15 +137,15 @@ - + - + - + @@ -160,7 +163,8 @@ Foreground="LightGreen" Background="LightGray" BorderBrush="Transparent" - Maximum="100" + Maximum="{Binding DataContext.StatViewModel.PodiumAccount[0].Argent, + RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}" Height="12" Value="{Binding Argent}" /> @@ -174,7 +178,7 @@ Grid.Row="2" Grid.Column="4" Grid.ColumnSpan="2" - FontSize="20" + FontSize="23" HorizontalAlignment="Center" FontWeight="DemiBold" Text="Meilleures ventes"/> @@ -204,6 +208,7 @@ Margin="0,20,0,0" Text="{Binding Path=PurchaseCount, StringFormat=' {0}'}" Grid.Column="0" + FontSize="15" /> @@ -211,17 +216,20 @@ Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" + Command="{Binding DataContext.StatViewModel.FindProduct, + RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}" + CommandParameter="{Binding ProductViewModel}" Content="{Binding ProductViewModel.NomProduitIHM}"> @@ -265,7 +273,8 @@ Foreground="#FFB366" Background="LightGray" BorderBrush="Transparent" - Maximum="100" + Maximum="{Binding DataContext.StatViewModel.PodiumProduits[0].PurchaseCount, + RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}" Height="12" Value="{Binding PurchaseCount}" /> diff --git a/Couche IHM/Couche IHM/Frames/FrameCaisse.xaml b/Couche IHM/Couche IHM/Frames/FrameCaisse.xaml index 29cfa6f..29b40e6 100644 --- a/Couche IHM/Couche IHM/Frames/FrameCaisse.xaml +++ b/Couche IHM/Couche IHM/Frames/FrameCaisse.xaml @@ -10,9 +10,9 @@ Title="FrameCaisse" Background="White"> - + - + @@ -20,19 +20,19 @@ - + - + - + @@ -42,7 +42,7 @@ FontWeight="Medium" FontFamily="Roboto" Content="Panier" - FontSize="15" + FontSize="18" Foreground="#FF403D39"/> @@ -113,43 +113,40 @@ - - + + - + - - + + - - - - - - - - - - - + @@ -261,10 +200,10 @@ @@ -273,8 +212,8 @@ @@ -283,9 +222,9 @@ @@ -294,84 +233,45 @@ - - - + + + + + + + diff --git a/Couche IHM/Couche IHM/Frames/FrameStatistique.xaml.cs b/Couche IHM/Couche IHM/Frames/FrameStatistique.xaml.cs index c496070..573a99b 100644 --- a/Couche IHM/Couche IHM/Frames/FrameStatistique.xaml.cs +++ b/Couche IHM/Couche IHM/Frames/FrameStatistique.xaml.cs @@ -1,19 +1,30 @@ - +using Couche_IHM.VueModeles; +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; namespace Couche_IHM.Frames { /// - /// Logique d'interaction pour FrameStatistique.xaml + /// Logique d'interaction pour FrameChooseStat.xaml /// public partial class FrameStatistique : Page { - /// - /// Constructeur naturelle - /// public FrameStatistique() { InitializeComponent(); + DataContext = MainWindowViewModel.Instance; } } } diff --git a/Couche IHM/Couche IHM/Frames/FrameStock.xaml b/Couche IHM/Couche IHM/Frames/FrameStock.xaml index 9baf50a..4885c04 100644 --- a/Couche IHM/Couche IHM/Frames/FrameStock.xaml +++ b/Couche IHM/Couche IHM/Frames/FrameStock.xaml @@ -14,7 +14,7 @@ Background="White" ShowsNavigationUI="False"> - + diff --git a/Couche IHM/Couche IHM/Frames/FrameTopStat.xaml b/Couche IHM/Couche IHM/Frames/FrameTopStat.xaml new file mode 100644 index 0000000..461fbfe --- /dev/null +++ b/Couche IHM/Couche IHM/Frames/FrameTopStat.xaml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +