63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using C969Project.Data.Models;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace C969Project.Data;
|
|
|
|
public static class DatabaseHelper
|
|
{
|
|
public static List<User> Users { get; private set; } = new();
|
|
|
|
public static void Initialize()
|
|
{
|
|
PopulateUsers();
|
|
}
|
|
|
|
public static User? GetUserById(int userId)
|
|
{
|
|
User user = Users.Find(u => u.UserId == userId);
|
|
return user;
|
|
}
|
|
|
|
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}");
|
|
User user = new()
|
|
{
|
|
UserId = id,
|
|
Username = name,
|
|
Password = password,
|
|
Active = active,
|
|
CreateDate = createDate,
|
|
CreatedBy = createdBy,
|
|
LastUpdate = lastUpdated,
|
|
LastUpdateBy = lastUpdateBy
|
|
};
|
|
Users.Add(user);
|
|
}
|
|
}
|
|
} |