c969-project/C969Project/Data/DatabaseHelper.cs
2025-06-14 21:03:33 -05:00

120 lines
4.1 KiB
C#

using C969Project.Data.Models;
using MySql.Data.MySqlClient;
namespace C969Project.Data;
public static class DatabaseHelper
{
public static void Initialize()
{
}
public static User? Login(string username, string password)
{
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
try
{
connection.Open();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
}
string query = $"SELECT * FROM client_schedule.user WHERE userName = '{username}'";
using MySqlCommand command = new MySqlCommand(query, connection);
using MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string name = reader.GetString("userName");
string pass = reader.GetString("password");
if (name == username && password == pass)
{
int id = reader.GetInt32("userId");
return new User(id, name);
}
}
return null;
}
public static List<Customer> RetrieveCustomers()
{
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
try
{
connection.Open();
}
catch (MySqlException e)
{
Console.WriteLine(e);
throw;
}
List<Customer> customers = new List<Customer>();
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;
}
}
}