89 lines
2.4 KiB
C#
89 lines
2.4 KiB
C#
using CineBook.Data;
|
|
using CineBook.Helpers;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
namespace CineBook.Views;
|
|
|
|
public partial class LoginWindow : Window
|
|
{
|
|
private readonly Repository _repo;
|
|
private readonly bool _dbConfigured;
|
|
|
|
public LoginWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
var cs = DbConfig.GetConnectionString();
|
|
if (string.IsNullOrWhiteSpace(cs))
|
|
{
|
|
MessageBox.Show(
|
|
"Es ist keine DB-Verbindung konfiguriert.\n\n" +
|
|
"Bitte setze die Umgebungsvariable 'CINEBOOK_CONNECTION_STRING'.\n" +
|
|
"(Siehe Kommentar in Data/DbConfig.cs)",
|
|
"Konfiguration fehlt",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Error);
|
|
// Window bleibt offen, damit die Info sichtbar bleibt.
|
|
_repo = new Repository(new Db(""));
|
|
_dbConfigured = false;
|
|
}
|
|
else
|
|
{
|
|
var db = new Db(cs);
|
|
_repo = new Repository(db);
|
|
_dbConfigured = true;
|
|
}
|
|
UsernameBox.Focus();
|
|
}
|
|
|
|
private void LoginClick(object sender, RoutedEventArgs e) => TryLogin();
|
|
|
|
private void InputKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter) TryLogin();
|
|
}
|
|
|
|
private void TryLogin()
|
|
{
|
|
if (!_dbConfigured)
|
|
{
|
|
ShowError("DB-Verbindung nicht konfiguriert (CINEBOOK_CONNECTION_STRING). Login nicht möglich.");
|
|
return;
|
|
}
|
|
var user = UsernameBox.Text.Trim();
|
|
var pass = PasswordBox.Password;
|
|
|
|
if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
|
|
{
|
|
ShowError("Bitte Benutzername und Passwort eingeben.");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var found = _repo.GetUserByCredentials(user, pass);
|
|
if (found == null)
|
|
{
|
|
ShowError("Falscher Benutzername oder Passwort.");
|
|
PasswordBox.Clear();
|
|
return;
|
|
}
|
|
Session.CurrentUser = found;
|
|
var main = new MainWindow();
|
|
main.Show();
|
|
Close();
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
ShowError("DB-Fehler: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void ShowError(string msg)
|
|
{
|
|
StatusText.Text = "⚠ " + msg;
|
|
StatusText.Visibility = Visibility.Visible;
|
|
}
|
|
}
|