62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using System.Windows;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using SimpleWiki.Data;
|
||
using SimpleWiki.Models;
|
||
using SimpleWiki.Services;
|
||
|
||
namespace SimpleWiki.Views;
|
||
|
||
public partial class RegisterWindow : Window
|
||
{
|
||
public RegisterWindow()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void RegisterButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var fullNameError = InputValidator.ValidateFullName(FullNameTextBox.Text);
|
||
var emailError = InputValidator.ValidateEmail(EmailTextBox.Text);
|
||
var passwordError = InputValidator.ValidatePassword(PasswordBox.Password);
|
||
var errors = InputValidator.JoinErrors(fullNameError, emailError, passwordError);
|
||
|
||
if (!string.IsNullOrWhiteSpace(errors))
|
||
{
|
||
MessageBox.Show(errors, "Ошибка валидации", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
return;
|
||
}
|
||
|
||
using var db = new AppDbContext();
|
||
var email = EmailTextBox.Text.Trim().ToLowerInvariant();
|
||
var exists = db.Users.AsNoTracking().Any(x => x.Email.ToLower() == email);
|
||
|
||
if (exists)
|
||
{
|
||
MessageBox.Show("Пользователь с таким email уже существует.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
|
||
return;
|
||
}
|
||
|
||
var user = new User
|
||
{
|
||
FullName = FullNameTextBox.Text.Trim(),
|
||
Email = email,
|
||
PasswordHash = PasswordHasher.HashPassword(PasswordBox.Password),
|
||
Role = IsAdminCheckBox.IsChecked == true ? Roles.Admin : Roles.User,
|
||
CreatedAt = DateTime.UtcNow
|
||
};
|
||
|
||
db.Users.Add(user);
|
||
db.SaveChanges();
|
||
|
||
MessageBox.Show("Аккаунт создан. Теперь можно войти.", "Успех", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
new LoginWindow().Show();
|
||
Close();
|
||
}
|
||
|
||
private void BackButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
new LoginWindow().Show();
|
||
Close();
|
||
}
|
||
}
|