95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using C969Project.Data;
|
|
using C969Project.Data.Models;
|
|
|
|
namespace C969Project
|
|
{
|
|
public partial class LoginForm : Form
|
|
{
|
|
public LoginForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
loginLabel.Text = Localization.GetLocalization("login_text");
|
|
usernameTextBox.PlaceholderText = Localization.GetLocalization("username_text");
|
|
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;
|
|
}
|
|
|
|
LogHistory(usr);
|
|
Console.WriteLine($"Login successful, {usr.Username}");
|
|
AppState.CurrentUser = usr;
|
|
|
|
CheckForAppointments();
|
|
|
|
var dash = new DashboardForm();
|
|
dash.FormClosing += (o, args) => { Close(); };
|
|
dash.Show();
|
|
Hide();
|
|
}
|
|
|
|
private void CheckForAppointments()
|
|
{
|
|
if (AppState.CurrentUser is null)
|
|
{
|
|
MessageBox.Show("A problem occured retrieving appointments.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
var appointments = DatabaseHelper.RetrieveAppointments(AppState.CurrentUser.UserId);
|
|
|
|
DateTime nowUtc = DateTime.UtcNow;
|
|
DateTime fifteenMinutesFromNowUtc = nowUtc.AddMinutes(15);
|
|
|
|
var upcoming = appointments
|
|
.Where(a => a.Start >= nowUtc && a.Start <= fifteenMinutesFromNowUtc)
|
|
.OrderBy(a => a.Start)
|
|
.ToList();
|
|
|
|
if (upcoming.Any())
|
|
{
|
|
var appt = upcoming.First();
|
|
DateTime localStart = appt.Start.ToLocalTime();
|
|
MessageBox.Show(
|
|
$"You have an upcoming {appt.AppointmentType} appointment '{appt.Title}' at {localStart}.",
|
|
"Upcoming Appointment",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Information);
|
|
}
|
|
}
|
|
|
|
private void UpdateLoginButtonState()
|
|
{
|
|
if (string.IsNullOrEmpty(usernameTextBox.Text) || string.IsNullOrEmpty(passwordTextBox.Text))
|
|
{
|
|
loginButton.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
loginButton.Enabled = true;
|
|
}
|
|
}
|
|
|
|
private void LogHistory(User user)
|
|
{
|
|
string text = $"{DateTime.UtcNow} UTC : {user.Username}";
|
|
using StreamWriter sw = new StreamWriter("Login_History.txt", true);
|
|
sw.WriteLine(text);
|
|
}
|
|
}
|
|
}
|