DatabaseHelper update and Models
This commit is contained in:
parent
6324c85080
commit
275b2b29d2
8
C969Project/AppState.cs
Normal file
8
C969Project/AppState.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using C969Project.Data.Models;
|
||||
|
||||
namespace C969Project;
|
||||
|
||||
public static class AppState
|
||||
{
|
||||
public static User CurrentUser { get; set; }
|
||||
}
|
@ -9,9 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||
<PackageReference Include="MySql.Data" Version="9.3.0" />
|
||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -5,22 +5,17 @@ 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)
|
||||
public static User? Login(string username, string password)
|
||||
{
|
||||
User user = Users.Find(u => u.UserId == userId);
|
||||
return user;
|
||||
}
|
||||
|
||||
private static void PopulateUsers()
|
||||
{
|
||||
using MySqlConnection connection = new(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
using MySqlConnection connection =
|
||||
new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
@ -28,36 +23,57 @@ public static class DatabaseHelper
|
||||
catch (MySqlException ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
string query = "SELECT * FROM client_schedule.user";
|
||||
|
||||
using MySqlCommand command = new(query, connection);
|
||||
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())
|
||||
{
|
||||
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()
|
||||
string pass = reader.GetString("password");
|
||||
|
||||
if (name == username && password == pass)
|
||||
{
|
||||
UserId = id,
|
||||
Username = name,
|
||||
Password = password,
|
||||
Active = active,
|
||||
CreateDate = createDate,
|
||||
CreatedBy = createdBy,
|
||||
LastUpdate = lastUpdated,
|
||||
LastUpdateBy = lastUpdateBy
|
||||
};
|
||||
Users.Add(user);
|
||||
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);
|
||||
// }
|
||||
// }
|
||||
}
|
15
C969Project/Data/Models/Address.cs
Normal file
15
C969Project/Data/Models/Address.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace C969Project.Data.Models;
|
||||
|
||||
public class Address
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Address1 {get; set;}
|
||||
public string Address2 {get; set;}
|
||||
public int CityId {get; set;}
|
||||
public string PostalCode {get; set;}
|
||||
public string Phone {get; set;}
|
||||
public DateTime CreateDate {get; set;}
|
||||
public string CreatedBy {get; set;}
|
||||
public DateTime LastUpdate {get; set;}
|
||||
public string LastUpdateBy {get; set;}
|
||||
}
|
20
C969Project/Data/Models/Appointment.cs
Normal file
20
C969Project/Data/Models/Appointment.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace C969Project.Data.Models;
|
||||
|
||||
public class Appointment
|
||||
{
|
||||
public int AppointmentId { get; set; }
|
||||
public int CustomerId { get; set; }
|
||||
public int UserId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Location { get; set; }
|
||||
public string Contact { get; set; }
|
||||
public string AppointmentType { get; set; }
|
||||
public string Url {get; set;}
|
||||
public DateTime Start { get; set; }
|
||||
public DateTime End { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
public string LastUpdateBy { get; set; }
|
||||
}
|
12
C969Project/Data/Models/City.cs
Normal file
12
C969Project/Data/Models/City.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace C969Project.Data.Models;
|
||||
|
||||
public class City
|
||||
{
|
||||
public int CityID { get; set; }
|
||||
public string CityName { get; set; }
|
||||
public int CountryID { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
public string LastUpdateBy { get; set; }
|
||||
}
|
11
C969Project/Data/Models/Country.cs
Normal file
11
C969Project/Data/Models/Country.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace C969Project.Data.Models;
|
||||
|
||||
public class Country
|
||||
{
|
||||
public int CountryID { get; set; }
|
||||
public string CountryName { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
public string LastUpdateBy { get; set; }
|
||||
}
|
13
C969Project/Data/Models/Customer.cs
Normal file
13
C969Project/Data/Models/Customer.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace C969Project.Data.Models;
|
||||
|
||||
public class Customer
|
||||
{
|
||||
public int CustomerId { get; set; }
|
||||
public string CustomerName { get; set; }
|
||||
public int AddressId { get; set; }
|
||||
public int Active { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
public string LastUpdateBy { get; set; }
|
||||
}
|
@ -2,21 +2,12 @@ namespace C969Project.Data.Models;
|
||||
|
||||
public class User
|
||||
{
|
||||
// int id = reader.GetInt32("userId");
|
||||
// string name = reader.GetString("userName");
|
||||
// string password = reader.GetString("password");
|
||||
// int active = reader.GetInt32("active");
|
||||
// DateTime scheduled = reader.GetDateTime("createDate");
|
||||
// string createdBy = reader.GetString("createdBy");
|
||||
// var lastUpdated = reader.GetDateTime("lastUpdate"); // Timestap
|
||||
// string lastUpdateBy = reader.GetString("lastUpdateBy");
|
||||
|
||||
public int UserId { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public int Active { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public DateTime LastUpdate { get; set; }
|
||||
public string LastUpdateBy { get; set; }
|
||||
|
||||
public User(int id, string username)
|
||||
{
|
||||
UserId = id;
|
||||
Username = username;
|
||||
}
|
||||
}
|
2
C969Project/LoginForm.Designer.cs
generated
2
C969Project/LoginForm.Designer.cs
generated
@ -88,7 +88,7 @@
|
||||
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
ClientSize = new System.Drawing.Size(800, 450);
|
||||
Controls.Add(panel1);
|
||||
Text = "Form1";
|
||||
Text = "Login";
|
||||
panel1.ResumeLayout(false);
|
||||
panel1.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
|
@ -1,3 +1,5 @@
|
||||
using C969Project.Data;
|
||||
|
||||
namespace C969Project
|
||||
{
|
||||
public partial class LoginForm : Form
|
||||
@ -11,9 +13,42 @@ namespace C969Project
|
||||
passwordTextBox.PlaceholderText = Localization.GetLocalization("password_text");
|
||||
loginButton.Text = Localization.GetLocalization("login_text");
|
||||
|
||||
|
||||
loginButton.Enabled = false;
|
||||
usernameTextBox.TextChanged += (sender, args) => {UpdateLoginButtonState();};
|
||||
passwordTextBox.TextChanged += (sender, args) => {UpdateLoginButtonState();};
|
||||
loginButton.Click += LoginButtonOnClick;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void LoginButtonOnClick(object? sender, EventArgs e)
|
||||
{
|
||||
var usr = DatabaseHelper.Login(usernameTextBox.Text, passwordTextBox.Text);
|
||||
|
||||
if (usr is null)
|
||||
{
|
||||
MessageBox.Show(Localization.GetLocalization("login_error_message"), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Login successful, {usr.Username}");
|
||||
AppState.CurrentUser = usr;
|
||||
|
||||
var dash = new DashboardForm();
|
||||
dash.Show();
|
||||
Hide();
|
||||
}
|
||||
|
||||
private void UpdateLoginButtonState()
|
||||
{
|
||||
if (string.IsNullOrEmpty(usernameTextBox.Text) || string.IsNullOrEmpty(passwordTextBox.Text))
|
||||
{
|
||||
loginButton.Enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
loginButton.Enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,5 @@
|
||||
"login_text": "Login",
|
||||
"password_text": "Password",
|
||||
"username_text": "Username",
|
||||
"login_error_message": "Invalid username or password"
|
||||
"login_error_message": "Incorrect username or password"
|
||||
}
|
Loading…
Reference in New Issue
Block a user