TheShopCritics2/TSC2/Components/CSharp/DatabaseManager.cs

134 lines
5.6 KiB
C#

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<Tuple<double[], string, string>> LoadAllShops()
{
List<Tuple<double[], string, string>> results = new List<Tuple<double[], string, string>>();
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<double[], string, string> tuple = new Tuple<double[], string, string>(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();
}
}
}