From 1b9bc300a849a80b12c13a0bdcf808b71d84391d Mon Sep 17 00:00:00 2001 From: Jeremy Mill Date: Wed, 11 Nov 2015 12:57:54 -0500 Subject: [PATCH] working API for add customer --- .../Controllers/TestController.cs | 2 +- .../TeamDBAwesome/SqlService/MySqlService.cs | 20 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/TeamDBAwesome/TeamDBAwesome/Controllers/TestController.cs b/TeamDBAwesome/TeamDBAwesome/Controllers/TestController.cs index 15c6a72..3e7954f 100644 --- a/TeamDBAwesome/TeamDBAwesome/Controllers/TestController.cs +++ b/TeamDBAwesome/TeamDBAwesome/Controllers/TestController.cs @@ -29,7 +29,7 @@ public HttpResponseMessage Get() test.Phone = "9109153999"; test.Post = "06066"; test.State = "CT"; - test.SupportRepId = 0; + test.SupportRepId = 1; //make an instance of the sql controller MySqlService sqlS = new MySqlService(); diff --git a/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs b/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs index f83b21e..0166059 100644 --- a/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs +++ b/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs @@ -80,8 +80,8 @@ public int AddNewUser(Customer newCust) //do the update into person string update = "INSERT into chinook.Person(LastName,FirstName,Address,City,State,PostalCode,Country,Phone,Fax,Email)" + - " VALUES ("+newCust.LName+","+newCust.FName + "," + newCust.Address + "," + newCust.City + "," + newCust.State + - "," + newCust.Post + "," + newCust.Country + "," + newCust.Phone + "," + newCust.Fax + "," + newCust.Email + ")"; + " VALUES (\""+newCust.LName+ "\",\"" + newCust.FName + "\",\"" + newCust.Address + "\",\"" + newCust.City + "\",\"" + newCust.State + + "\",\"" + newCust.Post + "\",\"" + newCust.Country + "\",\"" + newCust.Phone + "\",\"" + newCust.Fax + "\",\"" + newCust.Email + "\");"; MySqlCommand command = new MySqlCommand(update, SqlConn); command.ExecuteNonQuery(); @@ -91,15 +91,23 @@ public int AddNewUser(Customer newCust) command = new MySqlCommand(pk_query, SqlConn); string newPersonID = command.ExecuteScalar() + ""; + //it turns out that CustomerID isn't an auto incrementing value because people suck, so we need to get that + string cust_pk_query = "SELECT CustomerID FROM customer ORDER BY CustomerId DESC LIMIT 1"; + command = new MySqlCommand(cust_pk_query, SqlConn); + string nextCustIDstring = command.ExecuteScalar() + ""; + int next_custID_int = int.Parse(nextCustIDstring); + next_custID_int = next_custID_int + 1; + nextCustIDstring = next_custID_int.ToString(); + //now we can put into Customer - if(newCust.Company=="" || newCust.Company == null) + if (newCust.Company=="" || newCust.Company == null) { - update = "INSERT INTO chinook.Customer(Company,SupportRepID,PersonID) VALUES (" + newCust.Company + "," + newCust.SupportRepId + - "," + newPersonID + ")"; + update = "INSERT INTO chinook.Customer(CustomerID,Company,SupportRepID,PersonID) VALUES (" + nextCustIDstring + ",\"" + newCust.Company + "\",\"" + newCust.SupportRepId + + "\",\"" + newPersonID + "\")"; } else { - update = "INSERT INTO chinook.Customer(Company,SupportRepID,PersonID) VALUES (" + "NULL" + "," + newCust.SupportRepId + + update = "INSERT INTO chinook.Customer(CustomerID,Company,SupportRepID,PersonID) VALUES (" + nextCustIDstring + ", NULL" + "," + newCust.SupportRepId + "," + newPersonID + ")"; }