From d5801ec6b332ffb9329f05d8b18fe10c842127f8 Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Sat, 14 Jun 2025 21:03:33 -0500 Subject: [PATCH] Customer database stuff --- C969Project/Data/DatabaseHelper.cs | 109 ++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 34 deletions(-) diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index 9f8d981..600a150 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -14,8 +14,7 @@ public static class DatabaseHelper public static User? Login(string username, string password) { - using MySqlConnection connection = - new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); try { connection.Open(); @@ -44,36 +43,78 @@ public static class DatabaseHelper return null; } - // private static void PopulateUsers() - // { - // using MySqlConnection connection = new(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - // try - // { - // connection.Open(); - // } - // catch (MySqlException ex) - // { - // Console.WriteLine(ex.Message); - // throw; - // } - // - // string query = "SELECT * FROM client_schedule.user"; - // - // using MySqlCommand command = new(query, connection); - // using MySqlDataReader reader = command.ExecuteReader(); - // while (reader.Read()) - // { - // int id = reader.GetInt32("userId"); - // string name = reader.GetString("userName"); - // string password = reader.GetString("password"); - // int active = reader.GetInt32("active"); - // DateTime createDate = reader.GetDateTime("createDate"); - // string createdBy = reader.GetString("createdBy"); - // var lastUpdated = reader.GetDateTime("lastUpdate"); // Timestap - // string lastUpdateBy = reader.GetString("lastUpdateBy"); - // Console.WriteLine($"Id: {id}; Name: {name}; Password: {password}; Active: {active}; Created by: {createdBy}; Last Updated by: {lastUpdateBy}"); - // var user = new User(id, name); - // Users.Add(user); - // } - // } + public static List RetrieveCustomers() + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + } + catch (MySqlException e) + { + Console.WriteLine(e); + throw; + } + + List customers = new List(); + + using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.customer", connection); + using MySqlDataReader reader = command.ExecuteReader(); + while (reader.Read()) + { + var customer = new Customer() + { + CustomerId = reader.GetInt32("customerId"), + CustomerName = reader.GetString("customerName"), + AddressId = reader.GetInt32("addressId"), + Active = reader.GetInt32("active"), + CreateDate = reader.GetDateTime("createDate"), + CreatedBy = reader.GetString("createdBy"), + LastUpdate = reader.GetDateTime("lastUpdate"), + LastUpdateBy = reader.GetString("lastUpdateBy"), + }; + + customers.Add(customer); + } + + return customers; + } + + public static void AddCustomer(Customer customer) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "INSERT INTO customer (customerId, customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " + + "VALUES (@customerId, @customerName, @addressId, @active, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)"; + + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@customerId", customer.CustomerId); + command.Parameters.AddWithValue("@customerName", customer.CustomerName); + command.Parameters.AddWithValue("@addressId", customer.AddressId); + command.Parameters.AddWithValue("@active", customer.Active); + command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + int rowsAffected = command.ExecuteNonQuery(); + + + } + catch (MySqlException e) + { + Console.WriteLine($"MySQL Error: {e.Message}"); + throw; + } + catch (Exception ex) + { + Console.WriteLine($"General Error: {ex.Message}"); + throw; + } + } } \ No newline at end of file