79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using C969Project.Data.Models;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace C969Project.Data;
|
|
|
|
public static class DatabaseHelper
|
|
{
|
|
|
|
|
|
public static void Initialize()
|
|
{
|
|
|
|
}
|
|
|
|
public static User? Login(string username, string password)
|
|
{
|
|
using MySqlConnection connection =
|
|
new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
|
try
|
|
{
|
|
connection.Open();
|
|
}
|
|
catch (MySqlException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
|
|
string query = $"SELECT * FROM client_schedule.user WHERE userName = '{username}'";
|
|
using MySqlCommand command = new MySqlCommand(query, connection);
|
|
using MySqlDataReader reader = command.ExecuteReader();
|
|
|
|
while (reader.Read())
|
|
{
|
|
string name = reader.GetString("userName");
|
|
string pass = reader.GetString("password");
|
|
|
|
if (name == username && password == pass)
|
|
{
|
|
int id = reader.GetInt32("userId");
|
|
return new User(id, name);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// private static void PopulateUsers()
|
|
// {
|
|
// using MySqlConnection connection = new(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
|
// try
|
|
// {
|
|
// connection.Open();
|
|
// }
|
|
// catch (MySqlException ex)
|
|
// {
|
|
// Console.WriteLine(ex.Message);
|
|
// throw;
|
|
// }
|
|
//
|
|
// string query = "SELECT * FROM client_schedule.user";
|
|
//
|
|
// using MySqlCommand command = new(query, connection);
|
|
// using MySqlDataReader reader = command.ExecuteReader();
|
|
// while (reader.Read())
|
|
// {
|
|
// int id = reader.GetInt32("userId");
|
|
// string name = reader.GetString("userName");
|
|
// string password = reader.GetString("password");
|
|
// int active = reader.GetInt32("active");
|
|
// DateTime createDate = reader.GetDateTime("createDate");
|
|
// string createdBy = reader.GetString("createdBy");
|
|
// var lastUpdated = reader.GetDateTime("lastUpdate"); // Timestap
|
|
// string lastUpdateBy = reader.GetString("lastUpdateBy");
|
|
// Console.WriteLine($"Id: {id}; Name: {name}; Password: {password}; Active: {active}; Created by: {createdBy}; Last Updated by: {lastUpdateBy}");
|
|
// var user = new User(id, name);
|
|
// Users.Add(user);
|
|
// }
|
|
// }
|
|
} |