/// <summary> /// CREATE /// </summary> /// <param name="licenseNumber"></param> /// <param name="companyParkingCode"></param> public static void CreateParkClient(ClientData data) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConString("ParkingDatabase"))) { ParkClient client = new ParkClient(); LicensePlate licensePlate = new LicensePlate(); licensePlate.LicenseNumber = data.LicenseNumber; client.LicensePlate = licensePlate; client.CompanyParkingCode = data.CompanyParkingCode; if (RegisteredLicensePlate(client.LicensePlate.LicenseNumber)) { throw new ArgumentException("Number"); } else { if (VerifyParkingCode(client.CompanyParkingCode)) { connection.Execute("INSERT INTO ParkClients (CompanyParkingCode) VALUES (@CompanyParkingCode)", new { @CompanyParkingCode = client.CompanyParkingCode }); string parkClientIDKey = connection.ExecuteScalar("SELECT MAX(ParkClientID) FROM ParkClients WHERE CompanyParkingCode = @CompanyParkingCode", new { @CompanyParkingCode = client.CompanyParkingCode }).ToString(); connection.Execute("INSERT INTO LicensePlates (ParkClientIDKey, LicenseNumber) VALUES (@ParkClientIDKey, @LicenseNumber)", new { @ParkClientIDKey = parkClientIDKey, @LicenseNumber = client.LicensePlate.LicenseNumber }); } else { throw new ArgumentException("Code"); } } } }
/// <summary> /// UPDATE /// </summary> /// <param name="client"></param> public static void UpdateParkClient(ClientData clientData) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConString("ParkingDatabase"))) { if (RegisteredLicensePlate(clientData.LicenseNumber)) { if (VerifyParkingCode(clientData.CompanyParkingCode)) { ParkClient client = new ParkClient(); client = ReadParkClient(clientData.LicenseNumber); connection.Execute("UPDATE ParkClients SET CompanyParkingCode = @CompanyParkingCode WHERE ParkClientID = @ParkClientID", new { @CompanyParkingCode = clientData.CompanyParkingCode, @ParkClientID = client.ParkClientID }); connection.Execute("UPDATE LicensePlates SET LicenseNumber = @LicenseNumber WHERE LicensePlateID = @LicensePlateID", new { @LicenseNumber = client.LicensePlate.LicenseNumber, @LicensePlateID = client.LicensePlate.LicensePlateID }); connection.Execute("UPDATE LicensePlates SET ParkClientIDKey = @ParkClientIDKey WHERE LicensePlateID = @LicensePlateID", new { @ParkClientIDKey = client.LicensePlate.ParkClientIDKey, @LicensePlateID = client.LicensePlate.LicensePlateID }); } else { throw new ArgumentException("Code"); } } else { throw new ArgumentException("Number"); } } }
public static void GenerateDatabase() { List <ClientData> clients = new List <ClientData>(); for (int i = 0; i < 10; i++) { ClientData data = new ClientData(); LicensePlate licensePlate = new LicensePlate(); ParkClient parkClient = new ParkClient(); data.LicenseNumber = licensePlate.GenerateLicensePlate().LicenseNumber; data.CompanyParkingCode = parkClient.GenerateParkingCode(); clients.Add(data); } foreach (ClientData data in clients) { CreateParkClient(data); } }
/// <summary> /// DELETE /// </summary> /// <param name="client"></param> public static void DeleteParkClient(string licenseNumber) { ParkClient client = new ParkClient(); try { client = ReadParkClient(licenseNumber); } catch { throw; } using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConString("ParkingDatabase"))) { client.LicensePlate = connection.QuerySingle <LicensePlate>("SELECT * FROM LicensePlates WHERE LicenseNumber = @LicenseNumber", new { @LicenseNumber = licenseNumber }); connection.Execute("DELETE FROM LicensePlates WHERE LicensePlateID = @LicensePlateID", new { @LicensePlateID = client.LicensePlate.LicensePlateID }); connection.Execute("DELETE FROM ParkClients WHERE ParkClientID = @ParkClientID", new { @ParkClientID = client.ParkClientID }); } }
public static ParkClient ReadParkClient(string licenseNumber) { ParkClient parkClient = new ParkClient(); LicensePlate licensePlate = new LicensePlate(); using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConString("ParkingDatabase"))) { try { licensePlate = connection.QuerySingle <LicensePlate>("SELECT * FROM LicensePlates WHERE LicenseNumber = @LicenseNumber", new { @LicenseNumber = licenseNumber }); } catch (InvalidOperationException) { throw; } licensePlate = connection.QuerySingle <LicensePlate>("SELECT * FROM LicensePlates WHERE LicenseNumber = @LicenseNumber", new { @LicenseNumber = licenseNumber }); parkClient = connection.QuerySingle <ParkClient>("SELECT * FROM ParkClients WHERE ParkClientID = @ParkClientIDKey", new { @ParkClientIDKey = licensePlate.ParkClientIDKey }); parkClient.LicensePlate = licensePlate; return(parkClient); } }