12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System;
- using System.Collections.Generic;
- using Microsoft.EntityFrameworkCore.Infrastructure;
- using ReactiveUI;
- using RegAuth.Models;
- using System.Security.Cryptography;
- using System.Text;
- using Microsoft.EntityFrameworkCore;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- namespace RegAuth.ViewModels
- {
- internal class RegViewModel : ViewModelBase, INotifyPropertyChanged
- {
- private readonly validViewModel _otherViewModel;
- public RegViewModel()
- {
- _otherViewModel = new validViewModel(); // Ñîçäàåì ýêçåìïëÿð validViewModel
- }
- User _newUser = new User();
- public User NewUser
- {
- get => _newUser;
- set => this.RaiseAndSetIfChanged(ref _newUser, value);
- }
- string _password;
- private string _passwordError;
- public string Password
- {
- get => _password;
- set
- {
- if (this.RaiseAndSetIfChanged(ref _password, value))
- {
- // Ïðîâåðÿåì ïàðîëü è óñòàíàâëèâàåì ñîîáùåíèå îá îøèáêå
- PasswordError = _otherViewModel.ValidatePassword(value);
- }
- }
- }
- public string PasswordError
- {
- get => _passwordError;
- private set
- {
- this.RaiseAndSetIfChanged(ref _passwordError, value); // Óâåäîìëÿåì îá èçìåíåíèè
- }
- }
- public void AddUser()
- {
- Console.WriteLine("Ó ìåíÿ óæå åñòü àäìèí");
- if (!string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(PasswordError))//åñëè ó íàñ ïàðîëü íå ïóñò è îøèáîê íå íàáëþäàåòñÿ, òîãäà ìåòîä ðàáîòàåò äàëüøå è ðåàëèçóåò äîáàâëåíèå â áä
- {
- NewUser.Password = MD5.HashData(Encoding.ASCII.GetBytes(Password));
- NewUser.Roleid = 2;
- MainWindowViewModel.myConnection.Users.Add(NewUser);
- MainWindowViewModel.myConnection.SaveChanges();
- MainWindowViewModel.Instance.Uc = new Main();
- }
- }
- protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)//ýòî íàì íàäî, ÷òîáû êîððåêòíî îáðàáîòàòü ðåçóëüòàò â RaiseAndSetIfChanged
- {
- if (EqualityComparer<T>.Default.Equals(field, value)) return false; // Åñëè çíà÷åíèå íå èçìåíèëîñü, âîçâðàùàåì false
- field = value; // Îáíîâëÿåì ïîëå
- OnPropertyChanged(propertyName); // Âûçûâàåì ñîáûòèå èçìåíåíèÿ ñâîéñòâà
- return true; // Âîçâðàùàåì true, åñëè çíà÷åíèå èçìåíèëîñü
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
|