123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using Avalonia.Controls;
- using Avalonia.Media;
- using FinancialManagement.Models;
- using ReactiveUI;
- using FinancialManagement.Views;
- using FinancialManagement.ViewModels;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.EntityFrameworkCore;
- namespace FinancialManagement.ViewModels
- {
- public class MainWindowViewModel : ViewModelBase
- {
- ShmelevTeamContext ppContext = new ShmelevTeamContext();
- UserControl uc = new AuthorizationPage();
- AuthorizationPageViewModel authorizationPageViewModel = new AuthorizationPageViewModel();
- RegistrationPageViewModel registrationPageViewModel;
- FinancialManagementViewModel financialManagementViewModel;
- FinanceTableViewModel financeTableViewModel;
- FinanceDataAccountViewModel financeDataAccountViewModel;
- UserProfileViewModel userProfileViewModel;
- CategoryTableViewModel categoryTableViewModel;
- CategoryDataAccountViewModel categoryDataAccountViewModel;
- public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
- public AuthorizationPageViewModel AuthorizationPageVM { get => authorizationPageViewModel; set => authorizationPageViewModel = value; }
- public RegistrationPageViewModel RegistrationPageVM { get => registrationPageViewModel; set => registrationPageViewModel = value; }
- public FinancialManagementViewModel FinancialManagementVM { get => financialManagementViewModel; set => financialManagementViewModel = value; }
- public FinanceTableViewModel FinanceTableVM { get => financeTableViewModel; set => financeTableViewModel = value; }
- public FinanceDataAccountViewModel FinanceDataAccountVM { get => financeDataAccountViewModel; set => financeDataAccountViewModel = value; }
- public UserProfileViewModel UserProfileVM { get => userProfileViewModel; set => userProfileViewModel = value; }
- public CategoryTableViewModel CategoryTableVM { get => categoryTableViewModel; set => categoryTableViewModel = value; }
- public CategoryDataAccountViewModel CategoryDataAccountVM { get => categoryDataAccountViewModel; set => categoryDataAccountViewModel = value; }
- public void LodedToProgram()
- {
- User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- if (currentUser != null)
- {
- FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username);
- UC = new FinancialManagementPage();
- }
- else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
- }
- public void LogedToProgram(User? currentUser)
- {
- AuthorizationPageVM.Login = currentUser.Login;
- AuthorizationPageVM.Password = currentUser.Password;
- if (currentUser != null)
- {
- FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
- FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username);
- UC = new FinancialManagementPage();
- }
- else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
- }
- public void ToAuthorizathionPage()
- {
- UC = new AuthorizationPage();
- }
- public void ToFinanceManagementPage()
- {
- UC = new FinancialManagementPage();
- }
- public void RegistrationInDB()
- {
- User? currentUser = registrationPageViewModel.AddUserInDataBase();
- if (currentUser != null)
- {
- LogedToProgram(currentUser);
- }
- }
- public void ToRegistrationPage()
- {
- registrationPageViewModel = new RegistrationPageViewModel(ppContext);
- UC = authorizationPageViewModel.LoadPageRegistration();
- }
- public void ToFinanceTable()
- {
- User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
- FinanceDataAccountVM = new FinanceDataAccountViewModel();
- UC = new FinanceTable();
- }
- public void OpenNewFinanceDate()
- {
- User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
- FinanceDataAccountVM = new FinanceDataAccountViewModel();
- FinanceDataAccountVM = new FinanceDataAccountViewModel(ppContext, currentUser);
- UC = new FinanceDataAccount();
- }
- public void OpenFinance()
- {
- User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- FinanceDataAccountVM = new FinanceDataAccountViewModel(FinanceTableVM.ItemFinance,currentUser, ppContext);
- UC = new FinanceDataAccount();
- }
- public bool SaveFinance()
- {
- User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- Dictionary<string, Finance> objectFinance = FinanceDataAccountVM.GetDataFinance(currentUser);
- if (objectFinance.ContainsKey("4"))
- {
- Finance currentFinance = objectFinance["4"];
- if (ppContext.Finances.FirstOrDefault(x => x.FinanceId == currentFinance.FinanceId) != null)
- {
- ppContext.SaveChanges();
- }
- else
- {
- ppContext.Add(currentFinance);
- ppContext.SaveChanges();
- }
- FinanceDataAccountVM.Message = "Данные сохранены в базе";
- FinanceDataAccountVM.ColorMessage = Brushes.Azure;
- return true;
- }
- else if (objectFinance.ContainsKey("3"))
- {
- FinanceDataAccountVM.Message = "Нулевые данные";
- FinanceDataAccountVM.ColorMessage = Brushes.Red;
- return false;
- }
- else if (objectFinance.ContainsKey("2"))
- {
- FinanceDataAccountVM.Message = "Название категории не может превышать длину в 50 символов";
- FinanceDataAccountVM.ColorMessage = Brushes.Red;
- return false;
- }
- else if (objectFinance.ContainsKey("1"))
- {
- FinanceDataAccountVM.Message = "Не все поля заполнены";
- FinanceDataAccountVM.ColorMessage = Brushes.Red;
- return false;
- }
-
- return false;
- }
- public void DeleteSrticleInBD()
- {
- if (FinanceTableVM.Finance == null)
- {
- return;
- }
- else
- {
- ppContext.Remove(FinanceTableVM.ItemFinance);
- ppContext.SaveChanges();
- ToFinanceTable();
- }
- }
- public void ToProfile()
- {
- User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- UserProfileVM = new UserProfileViewModel(currentUser, ppContext);
- UC = new UserProfilePage();
- }
- public void SaveChangeUserData()
- {
- User currentUser = UserProfileVM.SaveChanges();
- if (currentUser != null)
- {
- LogedToProgram(currentUser);
- }
- }
- public void BackNotChanges()
- {
- LodedToProgram();
- }
- public void ToCategoryTable()
- {
- User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext);
- UC = new CategoryTable();
- }
- public void ToFinanceDataAccount()
- {
- UC = new FinanceDataAccount();
- }
- public void ToCategoryData()
- {
- UC = new CategoryDataAccount();
- }
- public void OpenNewCategoryData()
- {
- User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext);
- CategoryDataAccountVM = new CategoryDataAccountViewModel();
- CategoryDataAccountVM = new CategoryDataAccountViewModel(ppContext, currentUser);
- UC = new CategoryDataAccount();
- }
- public void OpenCategory()
- {
- User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- CategoryDataAccountVM = new CategoryDataAccountViewModel(CategoryTableVM.ItemCategory, currentUser, ppContext);
- UC = new CategoryDataAccount();
- }
- public bool SaveCategory()
- {
- User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
- Dictionary<string, Category> objectFinance = CategoryDataAccountVM.GetDataFinance(currentUser);
- if (objectFinance.ContainsKey("4"))
- {
- Category currentFinance = objectFinance["4"];
- if (ppContext.Categories.FirstOrDefault(x => x.CategoryId == currentFinance.CategoryId) != null)
- {
- ppContext.SaveChanges();
- }
- else
- {
- ppContext.Add(currentFinance);
- ppContext.SaveChanges();
- }
- CategoryDataAccountVM.Message = "Данные сохранены в базе";
- CategoryDataAccountVM.ColorMessage = Brushes.Azure;
- return true;
- }
- else if (objectFinance.ContainsKey("2"))
- {
- CategoryDataAccountVM.Message = "Название категории не может превышать длину в 50 символов";
- CategoryDataAccountVM.ColorMessage = Brushes.Red;
- return false;
- }
- else if (objectFinance.ContainsKey("1"))
- {
- CategoryDataAccountVM.Message = "Не все поля заполнены";
- CategoryDataAccountVM.ColorMessage = Brushes.Red;
- return false;
- }
- return false;
- }
- public void DeleteCategory()
- {
- if (CategoryTableVM.Categores == null)
- {
- return;
- }
- else
- {
- ppContext.Remove(CategoryTableVM.ItemCategory);
- ppContext.SaveChanges();
- ToCategoryTable();
- }
- }
- public void ShowHidePasswordAuthorization()
- {
- if (AuthorizationPageVM.PasswordChar == null)
- {
- AuthorizationPageVM.PasswordChar = '*';
- }
- else if (AuthorizationPageVM.PasswordChar != null)
- {
- AuthorizationPageVM.PasswordChar = null;
- }
- }
- public void ShowHidePasswordRegistration()
- {
- if (RegistrationPageVM.PasswordChar == null)
- {
- RegistrationPageVM.PasswordChar = '*';
- }
- else if (RegistrationPageVM.PasswordChar != null)
- {
- RegistrationPageVM.PasswordChar = null;
- }
- }
- public void ShowHidePasswordProfile()
- {
- if (UserProfileVM.PasswordChar == null)
- {
- UserProfileVM.PasswordChar = '*';
- }
- else if (UserProfileVM.PasswordChar != null)
- {
- UserProfileVM.PasswordChar = null;
- }
- }
- public void ToLoadOut()
- {
- AuthorizationPageVM.Login = null;
- AuthorizationPageVM.Password = null;
- ToAuthorizathionPage();
- }
- }
- }
|