MainWindowViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Avalonia.Controls;
  2. using Avalonia.Media;
  3. using FinancialManagement.Models;
  4. using ReactiveUI;
  5. using FinancialManagement.Views;
  6. using FinancialManagement.ViewModels;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using Microsoft.EntityFrameworkCore;
  10. namespace FinancialManagement.ViewModels
  11. {
  12. public class MainWindowViewModel : ViewModelBase
  13. {
  14. ShmelevTeamContext ppContext = new ShmelevTeamContext();
  15. UserControl uc = new AuthorizationPage();
  16. AuthorizationPageViewModel authorizationPageViewModel = new AuthorizationPageViewModel();
  17. RegistrationPageViewModel registrationPageViewModel;
  18. FinancialManagementViewModel financialManagementViewModel;
  19. FinanceTableViewModel financeTableViewModel;
  20. FinanceDataAccountViewModel financeDataAccountViewModel;
  21. UserProfileViewModel userProfileViewModel;
  22. CategoryTableViewModel categoryTableViewModel;
  23. CategoryDataAccountViewModel categoryDataAccountViewModel;
  24. public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  25. public AuthorizationPageViewModel AuthorizationPageVM { get => authorizationPageViewModel; set => authorizationPageViewModel = value; }
  26. public RegistrationPageViewModel RegistrationPageVM { get => registrationPageViewModel; set => registrationPageViewModel = value; }
  27. public FinancialManagementViewModel FinancialManagementVM { get => financialManagementViewModel; set => financialManagementViewModel = value; }
  28. public FinanceTableViewModel FinanceTableVM { get => financeTableViewModel; set => financeTableViewModel = value; }
  29. public FinanceDataAccountViewModel FinanceDataAccountVM { get => financeDataAccountViewModel; set => financeDataAccountViewModel = value; }
  30. public UserProfileViewModel UserProfileVM { get => userProfileViewModel; set => userProfileViewModel = value; }
  31. public CategoryTableViewModel CategoryTableVM { get => categoryTableViewModel; set => categoryTableViewModel = value; }
  32. public CategoryDataAccountViewModel CategoryDataAccountVM { get => categoryDataAccountViewModel; set => categoryDataAccountViewModel = value; }
  33. public void LodedToProgram()
  34. {
  35. User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  36. if (currentUser != null)
  37. {
  38. FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username);
  39. UC = new FinancialManagementPage();
  40. }
  41. else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
  42. }
  43. public void LogedToProgram(User? currentUser)
  44. {
  45. AuthorizationPageVM.Login = currentUser.Login;
  46. AuthorizationPageVM.Password = currentUser.Password;
  47. if (currentUser != null)
  48. {
  49. FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
  50. FinancialManagementVM = new FinancialManagementViewModel(currentUser.Username);
  51. UC = new FinancialManagementPage();
  52. }
  53. else AuthorizationPageVM.Message = "Вы ввели неккоректные данные";
  54. }
  55. public void ToAuthorizathionPage()
  56. {
  57. UC = new AuthorizationPage();
  58. }
  59. public void ToFinanceManagementPage()
  60. {
  61. UC = new FinancialManagementPage();
  62. }
  63. public void RegistrationInDB()
  64. {
  65. User? currentUser = registrationPageViewModel.AddUserInDataBase();
  66. if (currentUser != null)
  67. {
  68. LogedToProgram(currentUser);
  69. }
  70. }
  71. public void ToRegistrationPage()
  72. {
  73. registrationPageViewModel = new RegistrationPageViewModel(ppContext);
  74. UC = authorizationPageViewModel.LoadPageRegistration();
  75. }
  76. public void ToFinanceTable()
  77. {
  78. User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  79. FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
  80. FinanceDataAccountVM = new FinanceDataAccountViewModel();
  81. UC = new FinanceTable();
  82. }
  83. public void OpenNewFinanceDate()
  84. {
  85. User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  86. FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
  87. FinanceDataAccountVM = new FinanceDataAccountViewModel();
  88. FinanceDataAccountVM = new FinanceDataAccountViewModel(ppContext, currentUser);
  89. UC = new FinanceDataAccount();
  90. }
  91. public void OpenFinance()
  92. {
  93. User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  94. FinanceDataAccountVM = new FinanceDataAccountViewModel(FinanceTableVM.ItemFinance,currentUser, ppContext);
  95. UC = new FinanceDataAccount();
  96. }
  97. public bool SaveFinance()
  98. {
  99. User? currentUser = ppContext.Users.Include(x => x.Finances).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  100. Dictionary<string, Finance> objectFinance = FinanceDataAccountVM.GetDataFinance(currentUser);
  101. if (objectFinance.ContainsKey("4"))
  102. {
  103. Finance currentFinance = objectFinance["4"];
  104. if (ppContext.Finances.FirstOrDefault(x => x.FinanceId == currentFinance.FinanceId) != null)
  105. {
  106. ppContext.SaveChanges();
  107. }
  108. else
  109. {
  110. ppContext.Add(currentFinance);
  111. ppContext.SaveChanges();
  112. }
  113. FinanceDataAccountVM.Message = "Данные сохранены в базе";
  114. FinanceDataAccountVM.ColorMessage = Brushes.Azure;
  115. return true;
  116. }
  117. else if (objectFinance.ContainsKey("3"))
  118. {
  119. FinanceDataAccountVM.Message = "Нулевые данные";
  120. FinanceDataAccountVM.ColorMessage = Brushes.Red;
  121. return false;
  122. }
  123. else if (objectFinance.ContainsKey("2"))
  124. {
  125. FinanceDataAccountVM.Message = "Название категории не может превышать длину в 50 символов";
  126. FinanceDataAccountVM.ColorMessage = Brushes.Red;
  127. return false;
  128. }
  129. else if (objectFinance.ContainsKey("1"))
  130. {
  131. FinanceDataAccountVM.Message = "Не все поля заполнены";
  132. FinanceDataAccountVM.ColorMessage = Brushes.Red;
  133. return false;
  134. }
  135. return false;
  136. }
  137. public void DeleteSrticleInBD()
  138. {
  139. if (FinanceTableVM.Finance == null)
  140. {
  141. return;
  142. }
  143. else
  144. {
  145. ppContext.Remove(FinanceTableVM.ItemFinance);
  146. ppContext.SaveChanges();
  147. ToFinanceTable();
  148. }
  149. }
  150. public void ToProfile()
  151. {
  152. User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  153. UserProfileVM = new UserProfileViewModel(currentUser, ppContext);
  154. UC = new UserProfilePage();
  155. }
  156. public void SaveChangeUserData()
  157. {
  158. User currentUser = UserProfileVM.SaveChanges();
  159. if (currentUser != null)
  160. {
  161. LogedToProgram(currentUser);
  162. }
  163. }
  164. public void BackNotChanges()
  165. {
  166. LodedToProgram();
  167. }
  168. public void ToCategoryTable()
  169. {
  170. User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  171. CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext);
  172. UC = new CategoryTable();
  173. }
  174. public void ToFinanceDataAccount()
  175. {
  176. UC = new FinanceDataAccount();
  177. }
  178. public void ToCategoryData()
  179. {
  180. UC = new CategoryDataAccount();
  181. }
  182. public void OpenNewCategoryData()
  183. {
  184. User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  185. CategoryTableVM = new CategoryTableViewModel(currentUser, ppContext);
  186. CategoryDataAccountVM = new CategoryDataAccountViewModel();
  187. CategoryDataAccountVM = new CategoryDataAccountViewModel(ppContext, currentUser);
  188. UC = new CategoryDataAccount();
  189. }
  190. public void OpenCategory()
  191. {
  192. User? currentUser = ppContext.Users.FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  193. CategoryDataAccountVM = new CategoryDataAccountViewModel(CategoryTableVM.ItemCategory, currentUser, ppContext);
  194. UC = new CategoryDataAccount();
  195. }
  196. public bool SaveCategory()
  197. {
  198. User? currentUser = ppContext.Users.Include(x => x.Categories).FirstOrDefault(x => x.Login == AuthorizationPageVM.Login && x.Password == AuthorizationPageVM.Password);
  199. Dictionary<string, Category> objectFinance = CategoryDataAccountVM.GetDataFinance(currentUser);
  200. if (objectFinance.ContainsKey("4"))
  201. {
  202. Category currentFinance = objectFinance["4"];
  203. if (ppContext.Categories.FirstOrDefault(x => x.CategoryId == currentFinance.CategoryId) != null)
  204. {
  205. ppContext.SaveChanges();
  206. }
  207. else
  208. {
  209. ppContext.Add(currentFinance);
  210. ppContext.SaveChanges();
  211. }
  212. CategoryDataAccountVM.Message = "Данные сохранены в базе";
  213. CategoryDataAccountVM.ColorMessage = Brushes.Azure;
  214. return true;
  215. }
  216. else if (objectFinance.ContainsKey("2"))
  217. {
  218. CategoryDataAccountVM.Message = "Название категории не может превышать длину в 50 символов";
  219. CategoryDataAccountVM.ColorMessage = Brushes.Red;
  220. return false;
  221. }
  222. else if (objectFinance.ContainsKey("1"))
  223. {
  224. CategoryDataAccountVM.Message = "Не все поля заполнены";
  225. CategoryDataAccountVM.ColorMessage = Brushes.Red;
  226. return false;
  227. }
  228. return false;
  229. }
  230. public void DeleteCategory()
  231. {
  232. if (CategoryTableVM.Categores == null)
  233. {
  234. return;
  235. }
  236. else
  237. {
  238. ppContext.Remove(CategoryTableVM.ItemCategory);
  239. ppContext.SaveChanges();
  240. ToCategoryTable();
  241. }
  242. }
  243. public void ShowHidePasswordAuthorization()
  244. {
  245. if (AuthorizationPageVM.PasswordChar == null)
  246. {
  247. AuthorizationPageVM.PasswordChar = '*';
  248. }
  249. else if (AuthorizationPageVM.PasswordChar != null)
  250. {
  251. AuthorizationPageVM.PasswordChar = null;
  252. }
  253. }
  254. public void ShowHidePasswordRegistration()
  255. {
  256. if (RegistrationPageVM.PasswordChar == null)
  257. {
  258. RegistrationPageVM.PasswordChar = '*';
  259. }
  260. else if (RegistrationPageVM.PasswordChar != null)
  261. {
  262. RegistrationPageVM.PasswordChar = null;
  263. }
  264. }
  265. public void ShowHidePasswordProfile()
  266. {
  267. if (UserProfileVM.PasswordChar == null)
  268. {
  269. UserProfileVM.PasswordChar = '*';
  270. }
  271. else if (UserProfileVM.PasswordChar != null)
  272. {
  273. UserProfileVM.PasswordChar = null;
  274. }
  275. }
  276. public void ToLoadOut()
  277. {
  278. AuthorizationPageVM.Login = null;
  279. AuthorizationPageVM.Password = null;
  280. ToAuthorizathionPage();
  281. }
  282. }
  283. }