Finish Add/Update Customers Form
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Data; // For CommandType
|
||||
using System.Data;
|
||||
using MySql.Data.MySqlClient;
|
||||
using C969Project.Data.Models;
|
||||
|
||||
@@ -8,11 +8,6 @@ namespace C969Project.Data
|
||||
public static class DatabaseHelper
|
||||
{
|
||||
private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection");
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
// Initialization logic if any
|
||||
}
|
||||
|
||||
public static User? Login(string username, string password)
|
||||
{
|
||||
@@ -47,6 +42,7 @@ namespace C969Project.Data
|
||||
return null;
|
||||
}
|
||||
|
||||
#region Customers
|
||||
public static List<Customer> RetrieveCustomers()
|
||||
{
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
@@ -83,12 +79,7 @@ namespace C969Project.Data
|
||||
|
||||
return customers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new customer record to the database and returns the generated customerId.
|
||||
/// </summary>
|
||||
/// <param name="customer">The customer object to add.</param>
|
||||
/// <returns>The ID of the newly added customer; otherwise, 0 if an error occurs.</returns>
|
||||
|
||||
public static int AddCustomer(Customer customer)
|
||||
{
|
||||
int customerId = 0;
|
||||
@@ -97,11 +88,10 @@ namespace C969Project.Data
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed customerId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
"VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
|
||||
"SELECT LAST_INSERT_ID();";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -110,7 +100,7 @@ namespace C969Project.Data
|
||||
command.Parameters.AddWithValue("@active", customer.Active);
|
||||
command.Parameters.AddWithValue("@createDate", DateTime.UtcNow);
|
||||
command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username);
|
||||
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually
|
||||
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
|
||||
|
||||
object result = command.ExecuteScalar();
|
||||
if (result != null && result != DBNull.Value)
|
||||
@@ -140,14 +130,14 @@ namespace C969Project.Data
|
||||
|
||||
string query =
|
||||
"UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " +
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
command.Parameters.AddWithValue("@customerName", customer.CustomerName);
|
||||
command.Parameters.AddWithValue("@addressId", customer.AddressId);
|
||||
command.Parameters.AddWithValue("@active", customer.Active);
|
||||
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually
|
||||
command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username);
|
||||
command.Parameters.AddWithValue("@customerId", customer.CustomerId);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
@@ -191,7 +181,10 @@ namespace C969Project.Data
|
||||
{
|
||||
DeleteCustomer(customer.CustomerId);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Addresses
|
||||
|
||||
public static Address? RetrieveAddress(int addressId)
|
||||
{
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
@@ -271,12 +264,7 @@ namespace C969Project.Data
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new address to the database and returns the generated addressId.
|
||||
/// </summary>
|
||||
/// <param name="address">The address object to add.</param>
|
||||
/// <returns>The ID of the newly added address; otherwise, 0 if an error occurs.</returns>
|
||||
|
||||
public static int AddAddress(Address address)
|
||||
{
|
||||
int addressId = 0;
|
||||
@@ -285,11 +273,10 @@ namespace C969Project.Data
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed addressId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO address (address, address2, cityId, postalCode, phone, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
"VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
|
||||
"SELECT LAST_INSERT_ID();";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -330,7 +317,7 @@ namespace C969Project.Data
|
||||
|
||||
string query =
|
||||
"UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " +
|
||||
"phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -384,6 +371,10 @@ namespace C969Project.Data
|
||||
DeleteAddress(address.Id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Appointments
|
||||
|
||||
public static Appointment? RetrieveAppointment(int appointmentId)
|
||||
{
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
@@ -587,6 +578,9 @@ namespace C969Project.Data
|
||||
DeleteAppointment(appointment.AppointmentId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cities
|
||||
public static City? RetrieveCity(int cityId)
|
||||
{
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
@@ -684,7 +678,6 @@ namespace C969Project.Data
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b.
|
||||
Console.WriteLine($"Error getting city ID: {ex.Message}");
|
||||
}
|
||||
return cityId;
|
||||
@@ -698,11 +691,10 @@ namespace C969Project.Data
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed cityId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
"VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
|
||||
"SELECT LAST_INSERT_ID();";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -740,7 +732,7 @@ namespace C969Project.Data
|
||||
|
||||
string query =
|
||||
"UPDATE city SET city = @cityName, countryId = @countryId, " +
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -790,7 +782,9 @@ namespace C969Project.Data
|
||||
{
|
||||
DeleteCity(city.CityID);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Countries
|
||||
public static Country? RetrieveCountry(int countryId)
|
||||
{
|
||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
@@ -885,7 +879,6 @@ namespace C969Project.Data
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b.
|
||||
Console.WriteLine($"Error getting country ID: {ex.Message}");
|
||||
}
|
||||
return countryId;
|
||||
@@ -899,11 +892,10 @@ namespace C969Project.Data
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// Removed countryId from INSERT statement as it's auto-incremented
|
||||
string query =
|
||||
"INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " +
|
||||
"VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"SELECT LAST_INSERT_ID();"; // Get the newly generated ID
|
||||
"VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " +
|
||||
"SELECT LAST_INSERT_ID();";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -940,7 +932,7 @@ namespace C969Project.Data
|
||||
|
||||
string query =
|
||||
"UPDATE country SET country = @countryName, " +
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; // Use CURRENT_TIMESTAMP for lastUpdate
|
||||
"lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId";
|
||||
|
||||
using MySqlCommand command = new MySqlCommand(query, connection);
|
||||
|
||||
@@ -989,5 +981,6 @@ namespace C969Project.Data
|
||||
{
|
||||
DeleteCountry(country.CountryID);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user