diff --git a/TSC2/Components/CSharp/DatabaseManager.cs b/TSC2/Components/CSharp/DatabaseManager.cs new file mode 100644 index 0000000..f221e3e --- /dev/null +++ b/TSC2/Components/CSharp/DatabaseManager.cs @@ -0,0 +1,133 @@ +using MySql.Data.MySqlClient; +using System.Data; +using Telerik.SvgIcons; +using TSC2.Components.Layout; + +namespace TSC2.Components.CSharp +{ + public class DatabaseManager + { + private static string _connectionString = "Server=127.0.0.1;User ID=root;Password=root;Database=tsc2"; + + + + /**********************************************/ + // SHOP INFORMATION + /**********************************************/ + public static List> LoadAllShops() + { + List> results = new List>(); + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + using (var cmd = new MySqlCommand("SELECT * FROM shopinformation", conn)) + { + using (MySqlDataReader reader = cmd.ExecuteReader()) + { + while(reader.Read()) + { + var uniqueID = reader.GetString(0); + var shopName = reader.GetString(1); + //var address = reader.GetString(2); + //var phone = reader.GetString(3); + //var website = reader.GetString(4); + //var hours = reader.GetString(5); + //var about = reader.GetString(6); + double latitude = reader.GetDouble(7); + double longitude = reader.GetDouble(8); + + double[] coords = [latitude, longitude]; + Tuple tuple = new Tuple(coords, shopName, uniqueID); + + results.Add(tuple); + } + } + } + + return results; + } + + + + /**********************************************/ + // LOGIN FLOW + /**********************************************/ + public async static Task SignInGoogle() + { + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + using (var cmd = new MySqlCommand("SELECT * FROM userinformation WHERE Platform='Google' AND Token='" + MainLayout.Session["id"] + "'", conn)) + using (var count_cmd = new MySqlCommand("SELECT COUNT(*) FROM (SELECT * FROM userinformation WHERE Platform='Google' AND Token='" + MainLayout.Session["id"] + "') AS result", conn)) + { + int count = Convert.ToInt32(count_cmd.ExecuteScalar()); + + if (count == 0) // User is not already in our database + { + try + { + // Generate guid + Guid guid = Guid.NewGuid(); + + var insert_cmd = new MySqlCommand(); + + insert_cmd.CommandText = "INSERT INTO userinformation(UniqueID,FullName,Platform,Token) VALUES(@uniqueid,@fullname,@platform,@token)"; + insert_cmd.Connection = conn; + + insert_cmd.Parameters.AddWithValue("@uniqueid", guid.ToString()); + insert_cmd.Parameters.AddWithValue("@fullname", MainLayout.Session["name"]); + insert_cmd.Parameters.AddWithValue("@platform", "Google"); + insert_cmd.Parameters.AddWithValue("@token", MainLayout.Session["id"]); + + insert_cmd.ExecuteNonQuery(); + MainLayout.Session.Add("unique_id", guid.ToString()); + } + catch(Exception ex) + { + await Console.Out.WriteLineAsync(ex.Message); + } + } + } + conn.Close(); + } + + public async static Task SignInFacebook() + { + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); + + using (var cmd = new MySqlCommand("SELECT * FROM userinformation WHERE Platform='Facebook' AND Token='" + MainLayout.Session["id"] + "'", conn)) + using (var count_cmd = new MySqlCommand("SELECT COUNT(*) FROM (SELECT * FROM userinformation WHERE Platform='Facebook' AND Token='" + MainLayout.Session["id"] + "') AS result", conn)) + { + int count = Convert.ToInt32(count_cmd.ExecuteScalar()); + + if (count == 0) // User is not already in our database + { + try + { + // Generate guid + Guid guid = Guid.NewGuid(); + + var insert_cmd = new MySqlCommand(); + + insert_cmd.CommandText = "INSERT INTO userinformation(UniqueID,FullName,Platform,Token) VALUES(@uniqueid,@fullname,@platform,@token)"; + insert_cmd.Connection = conn; + + insert_cmd.Parameters.AddWithValue("@uniqueid", guid.ToString()); + insert_cmd.Parameters.AddWithValue("@fullname", MainLayout.Session["name"]); + insert_cmd.Parameters.AddWithValue("@platform", "Facebook"); + insert_cmd.Parameters.AddWithValue("@token", MainLayout.Session["id"]); + + insert_cmd.ExecuteNonQuery(); + MainLayout.Session.Add("unique_id", guid.ToString()); + } + catch (Exception ex) + { + await Console.Out.WriteLineAsync(ex.Message); + } + } + } + conn.Close(); + } + } +} diff --git a/TSC2/Components/CSharp/MapDriver.cs b/TSC2/Components/CSharp/MapDriver.cs index 60365cb..7ab49cd 100644 --- a/TSC2/Components/CSharp/MapDriver.cs +++ b/TSC2/Components/CSharp/MapDriver.cs @@ -1,25 +1,19 @@ -namespace TSC2.Components; +using TSC2.Components.CSharp; + +namespace TSC2.Components; public class MapDriver { - public static List<(double[], string)> InitializeMarkers(double[] inputCoords) + public static List> InitializeMarkers() { // Initialization - List<(double[], string)> results = []; + List> results = []; - // Read list of addresses and names from DB and contact search API to get location - List<(double[] Coords, string Name)> db = new List<(double[], string)>(); - db.Add(([42.4649, -83.3684], "1")); - db.Add(([42.6649, -83.5684], "2")); - db.Add(([42.8649, -83.7684], "3")); + // Read list of addresses and names from DB + List> db = DatabaseManager.LoadAllShops(); foreach (var entry in db) { - (double[] Coords, string Name) info = ([], ""); - - // Read address and contact POI - info.Coords = entry.Coords; // TODO: replace with API lookup for the current entry's address - info.Name = entry.Name; // TODO: replace with name found in DB - + Tuple info = new Tuple(entry.Item1, entry.Item2, entry.Item3); results.Add(info); } return results; diff --git a/TSC2/Components/Layout/MainLayout.razor.cs b/TSC2/Components/Layout/MainLayout.razor.cs index b1352b7..cda628e 100644 --- a/TSC2/Components/Layout/MainLayout.razor.cs +++ b/TSC2/Components/Layout/MainLayout.razor.cs @@ -21,7 +21,6 @@ // This should only execute if the user is signed in AND is the first render Greeting = "Hello, " + Session["name"]; - Console.Out.WriteLine("Updated greeting"); } @@ -30,7 +29,6 @@ { Session = new Dictionary(); _navigationManager.NavigateTo("/"); - Console.WriteLine("Signed out"); } } } diff --git a/TSC2/Components/Pages/FacebookSignIn.razor b/TSC2/Components/Pages/FacebookSignIn.razor index 95d864e..149229c 100644 --- a/TSC2/Components/Pages/FacebookSignIn.razor +++ b/TSC2/Components/Pages/FacebookSignIn.razor @@ -1,6 +1,7 @@ @using System.Net.Http; @using System.Text.Json; @using TSC2.Components.Layout; +@using TSC2.Components.CSharp; @page "/signin-facebook" @@ -45,8 +46,6 @@ var id_json = await client.GetStringAsync("https://graph.facebook.com/me?fields=id&access_token=" + tokenResponse.access_token); FacebookAuthResponse id_response = JsonSerializer.Deserialize(id_json); - Console.WriteLine("ID: " + id_response.id); - // Configure the request List> requestData = new List>(); requestData.Add(new KeyValuePair("access_token", tokenResponse.access_token)); @@ -71,7 +70,9 @@ MainLayout.Session.Add("id", profileResponse.id); MainLayout.Session.Add("name", profileResponse.name); MainLayout.UpdateGreeting(); - Console.WriteLine("Signed in successfully. (FB)"); + + // Contact database + DatabaseManager.SignInFacebook(); } _navigationManager.NavigateTo("/"); diff --git a/TSC2/Components/Pages/GoogleSignIn.razor b/TSC2/Components/Pages/GoogleSignIn.razor index 579eae3..9795506 100644 --- a/TSC2/Components/Pages/GoogleSignIn.razor +++ b/TSC2/Components/Pages/GoogleSignIn.razor @@ -1,6 +1,7 @@ @using System.Net.Http; @using System.Text.Json; @using TSC2.Components.Layout; +@using TSC2.Components.CSharp; @page "/signin-google" @@ -38,7 +39,7 @@ auth_json = await response.Content.ReadAsStringAsync(); } OAuthTokenResponse tokenResponse = JsonSerializer.Deserialize(auth_json); - Console.WriteLine("AUTH: " + auth_json); + // Use the access token to access their information; name, email, & token using (var client = new HttpClient()) { @@ -47,16 +48,16 @@ } GoogleProfileResponse profileResponse = JsonSerializer.Deserialize(profile_json); - - Console.WriteLine("PROFILE: " + profile_json); - if (MainLayout.Session.Count == 0) // Fpr security, only add to the session if nothing exists in the session. { + // Add variables to the session MainLayout.Session.Add("id", profileResponse.id); MainLayout.Session.Add("email", profileResponse.email); MainLayout.Session.Add("name", profileResponse.name); MainLayout.UpdateGreeting(); - Console.WriteLine("Signed in successfully."); + + // Contact database + DatabaseManager.SignInGoogle(); } _navigationManager.NavigateTo("/"); diff --git a/TSC2/Components/Pages/Home.razor b/TSC2/Components/Pages/Home.razor index c9d10b6..171dbd7 100644 --- a/TSC2/Components/Pages/Home.razor +++ b/TSC2/Components/Pages/Home.razor @@ -22,22 +22,7 @@ - Dealers - Car | Canvas - Say Hello - -
- - @helloString - - @code { - MarkupString helloString; - - void SayHelloHandler() - { - string msg = string.Format("Hello from Telerik Blazor at {0}.
Now you can use C# to write front-end!", DateTime.Now); - helloString = new MarkupString(msg); - } - } + The Shop Critics