Browse Source

Add login

AngelikaSuhareva 5 months ago
parent
commit
dd19af77d6

+ 17 - 0
ViewModels/LoginViewModel.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using ReactiveUI;
+
+namespace YtYtAvalonia.ViewModels
+{
+	public class LoginViewModel : ReactiveObject
+	{
+		string login = "";
+		string password = "";
+		string message = "";
+
+        public string Login { get => login; set => login = value; }
+        public string Password { get => password; set => password = value; }
+        public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
+    }
+}

+ 20 - 6
ViewModels/MainWindowViewModel.cs

@@ -1,10 +1,10 @@
 using YtYtAvalonia.Models;
 using YtYtAvalonia.Views;
 using YtYtAvalonia.ViewModels;
-
 using Avalonia.Controls;
 using ReactiveUI;
 using YtYtAvalonia.View;
+using System.Linq;
 
 namespace YtYtAvalonia.ViewModels
 {
@@ -17,14 +17,28 @@ namespace YtYtAvalonia.ViewModels
         RegistrationViewModel regPage = new RegistrationViewModel(myConnection);
         public RegistrationViewModel RegPage { get => regPage; set => regPage = value; }
 
-#pragma warning restore CA1822 // Mark members as static
-        public string Greeting => "Welcome to Avalonia!";
-
         public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
-#pragma warning restore CA1822 // Mark members as static
-
+        
         private UserControl uc = new LoginView();
 
         public void LoadRegistrationView() => UC = new RegistrationView();
+
+        LoginViewModel loginVM = new LoginViewModel();
+        public LoginViewModel LoginVM { get => loginVM; set => loginVM = value; }
+
+
+        public void LoadPersonalAccount()
+        {
+            User? currentUser = myConnection.Users.FirstOrDefault(x => x.Login == LoginVM.Login && x.Password == LoginVM.Password);
+            if(currentUser == null)
+            {
+                LoginVM.Message = "Пользователя с такими данными не существует.";
+            }
+            else
+            {
+                LoginVM.Message = "Ecgt[!";
+                UC = new PersonalAccount();
+            }
+        }
     }
 }

+ 5 - 3
Views/LoginView.axaml

@@ -2,6 +2,7 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 			 xmlns:vm="using:YtYtAvalonia.ViewModels"
+			 x:DataType="vm:MainWindowViewModel"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="YtYtAvalonia.View.LoginView">
@@ -35,9 +36,10 @@
 			</StackPanel.Styles>
 
 			<TextBlock HorizontalAlignment="Center" Margin="0 0 0 30" FontFamily="Roboto" FontSize="25" FontWeight="Bold">Авторизация</TextBlock>
-			<TextBox Watermark="Логин" Width="360" FontSize="13" Margin="0 0 0 10" ></TextBox>
-			<TextBox Watermark="Пароль" PasswordChar="•" Width="360" ></TextBox>
-			<Button HorizontalAlignment="Center" Margin="0 20 0 10" Width="230" Height="45" CornerRadius="10" Background="#38BFF2">
+			<TextBox Text="{Binding LoginVM.Login}" Watermark="Логин" Width="360" FontSize="13" Margin="0 0 0 10" ></TextBox>
+			<TextBox Text="{Binding LoginVM.Password}" Watermark="Пароль" PasswordChar="•" Width="360" ></TextBox>
+			<TextBlock Text="{Binding LoginVM.Message}"/>
+			<Button HorizontalAlignment="Center" Margin="0 20 0 10" Width="230" Height="45" CornerRadius="10" Background="#38BFF2" Command="{Binding LoadPersonalAccount}">
 				<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto" FontSize="16" FontWeight="DemiBold" Foreground="White">Войти</TextBlock>
 			</Button>
 			<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">

+ 2 - 0
Views/LoginView.axaml.cs

@@ -1,6 +1,7 @@
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
+using YtYtAvalonia.ViewModels;
 
 namespace YtYtAvalonia.View;
 
@@ -9,5 +10,6 @@ public partial class LoginView : UserControl
     public LoginView()
     {
         InitializeComponent();
+        //DataContext = new LoginViewModel();
     }
 }

+ 10 - 0
Views/PersonalAccount.axaml

@@ -0,0 +1,10 @@
+<UserControl xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+			 xmlns:vm="using:YtYtAvalonia.ViewModels"
+			 x:DataType="vm:MainWindowViewModel"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+             x:Class="YtYtAvalonia.Views.PersonalAccount">
+  Welcome to Avalonia!
+</UserControl>

+ 13 - 0
Views/PersonalAccount.axaml.cs

@@ -0,0 +1,13 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+
+namespace YtYtAvalonia.Views;
+
+public partial class PersonalAccount : UserControl
+{
+    public PersonalAccount()
+    {
+        InitializeComponent();
+    }
+}