Customer database stuff
This commit is contained in:
parent
238d3be5ec
commit
d5801ec6b3
@ -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<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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user