53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CryptoPortfolio_Tracker.Views;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls.Notifications;
|
|
using CryptoPortfolio_Tracker.Context;
|
|
using CryptoPortfolio_Tracker.Core;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CryptoPortfolio_Tracker.ViewModels
|
|
{
|
|
public partial class LoginViewModel : ObservableValidator
|
|
{
|
|
[Required] [ObservableProperty] public partial string Login { get; set; } = "admin";
|
|
|
|
[Required] [ObservableProperty] public partial string Password { get; set; } = "admin";
|
|
|
|
[RelayCommand]
|
|
private async Task LoginAsync()
|
|
{
|
|
if (Validate())
|
|
{
|
|
await using CryptoContext context = new();
|
|
|
|
var user = await context.Users.FirstOrDefaultAsync(u =>
|
|
(u.Email == Login || u.Login == Login) && u.Password == Password);
|
|
|
|
if (user == null)
|
|
{
|
|
MainView.Current.ShowToast("Неверный логин или пароль", NotificationType.Error);
|
|
return;
|
|
}
|
|
|
|
AppState.CurrentUser = user;
|
|
|
|
await MainView.Current.ReplaceAsync(new HomeView
|
|
{
|
|
DataContext = new HomeViewModel()
|
|
});
|
|
}
|
|
}
|
|
|
|
private bool Validate()
|
|
{
|
|
ValidateAllProperties();
|
|
return !HasErrors;
|
|
}
|
|
}
|
|
} |