From 8d4a29556be433f3a49e106204ed9cdb3004a61b Mon Sep 17 00:00:00 2001 From: Jeremy Mill Date: Tue, 3 Nov 2015 20:59:12 -0500 Subject: [PATCH] finished untested add customer API --- .../TeamDBAwesome/SqlService/MySqlService.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs b/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs index a68b95b..aca7a1b 100644 --- a/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs +++ b/TeamDBAwesome/TeamDBAwesome/SqlService/MySqlService.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Web; using MySql.Data.MySqlClient; +using TeamDBAwesome.Models; namespace TeamDBAwesome.SqlService { @@ -69,5 +70,53 @@ private bool CloseConnection() return false; } } + + public bool AddNewUser(Customer newCust) + { + bool open = this.OpenConnection(); + + if(open == true) + { + + //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 + ")"; + + MySqlCommand command = new MySqlCommand(update, SqlConn); + command.ExecuteNonQuery(); + + //now we need to get the primary key from that recent update + string pk_query = "SELECT LAST_INSERT_ID()"; + command = new MySqlCommand(pk_query, SqlConn); + string newPersonID = command.ExecuteScalar() + ""; + + //now we can put into Customer + if(newCust.Company=="" || newCust.Company == null) + { + update = "INSERT INTO chinook.Customer(Company,SupportRepID,PersonID) VALUES (" + newCust.Company + "," + newCust.SupportRepId + + "," + newPersonID + ")"; + } + else + { + update = "INSERT INTO chinook.Customer(Company,SupportRepID,PersonID) VALUES (" + "NULL" + "," + newCust.SupportRepId + + "," + newPersonID + ")"; + } + + command = new MySqlCommand(update, SqlConn); + command.ExecuteNonQuery(); + + //close the connection + this.CloseConnection(); + + return true; + } + else + { + return false; + } + + + } } } \ No newline at end of file