using Avalonia.Controls; using ReactiveUI; using AvaloniaApplicationTestNew.Views; using AvaloniaApplicationTestNew.ViewModels; using AvaloniaApplicationTestNew.Models; using Microsoft.EntityFrameworkCore; using System.Linq; using System; namespace AvaloniaApplicationTestNew.ViewModels { public class MainWindowViewModel : ViewModelBase { UserControl uc = new Markup(); UserControl page = new Authorization(); Logined? current_user; AuthorizationViewModel authorizationViewModel = new AuthorizationViewModel(); UserPageViewModel userPageViewModel; AdminPageViewModel adminPageViewModel; AddUserPageViewModel addUserPageViewModel; bool isButtonVisible; public Logined? Current_user { get => current_user; set => this.RaiseAndSetIfChanged(ref current_user, value); } public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); } public UserControl Page { get => page; set => this.RaiseAndSetIfChanged(ref page,value); } public AuthorizationViewModel AuthorizationVM { get => authorizationViewModel; set => authorizationViewModel = value; } public UserPageViewModel UserPageVM { get => userPageViewModel; set => userPageViewModel = value; } public AdminPageViewModel AdminPageVM { get => adminPageViewModel; set => adminPageViewModel = value; } public AddUserPageViewModel AddUserPageVM { get => addUserPageViewModel; set => addUserPageViewModel = value; } public bool IsButtonVisible { get => isButtonVisible; set => this.RaiseAndSetIfChanged(ref isButtonVisible, value); } public static MainWindowViewModel Self; public MainWindowViewModel() { Self = this; } public void Loged() { IsButtonVisible = false; Current_user = myContext.Logineds.Include(x => x.User.IdGenderNavigation).FirstOrDefault(x => x.Login == AuthorizationVM.Login && x.Password == AuthorizationVM.Password); if (Current_user != null) { switch(Current_user.IdRole) { case 1: Page = new AdminPage(); AdminPageVM = new AdminPageViewModel(); GetAccess(); break; case 2: UserPageVM = new UserPageViewModel(current_user.User.IdUser); Page = new UserPage(); break; } } else { AuthorizationVM.Message = "Неверные данные"; AuthorizationVM.Error(); Page = new Authorization(); } } public void GetAccess() { IsButtonVisible = true; } public void BackToList() { if (IsButtonVisible) { Page = new AdminPage(); } } public void DeleteUser() { int id = UserPageVM.Current_user.IdUser; if(myContext.Logineds.FirstOrDefault(x => x.Id == id).IdRole == 1) { UserPageVM.Message = "Пользователя нельзя удалить, так как он администратор"; } else { myContext.Remove(myContext.Users.FirstOrDefault(x => x.IdUser == id)); myContext.Remove(myContext.Logineds.FirstOrDefault(x => x.Id == id)); myContext.SaveChanges(); Page = new AdminPage(); } } } }