CategoryDataAccountViewModel.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Avalonia.Controls;
  2. using ReactiveUI;
  3. using FinancialManagement.Views;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Avalonia.Media;
  10. using FinancialManagement.Models;
  11. namespace FinancialManagement.ViewModels
  12. {
  13. public class CategoryDataAccountViewModel : ReactiveObject
  14. {
  15. ShmelevTeamContext ppContext;
  16. List<Category> categores;
  17. Category? categoryItem;
  18. int userIdLoad;
  19. string categoryTitle;
  20. int categoryIdFinance;
  21. string message = "";
  22. private IBrush colorMessage;
  23. public int UserIdLoad { get => userIdLoad; set => this.RaiseAndSetIfChanged(ref userIdLoad, value); }
  24. public int CategoryIdFinance { get => categoryIdFinance; set => this.RaiseAndSetIfChanged(ref categoryIdFinance, value); }
  25. public List<Category> Category { get => categores; set => this.RaiseAndSetIfChanged(ref categores, value); }
  26. public IBrush ColorMessage { get => colorMessage; set => this.RaiseAndSetIfChanged(ref colorMessage, value); }
  27. public Category? CategoryItem { get => categoryItem; set => categoryItem = value; }
  28. public string CategoryTitle { get => categoryTitle; set => this.RaiseAndSetIfChanged(ref categoryTitle, value); }
  29. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  30. public int LoadCategoryId(string categoryId)
  31. {
  32. int categoryId1 = -1;
  33. using (ShmelevTeamContext ppContext = new ShmelevTeamContext())
  34. {
  35. var financeCategory = ppContext.Categories
  36. .Where(f => f.Title == categoryId)
  37. .Select(f => f.CategoryId)
  38. .FirstOrDefault();
  39. categoryId1 = financeCategory;
  40. }
  41. return categoryId1;
  42. }
  43. public CategoryDataAccountViewModel()
  44. {
  45. categoryItem = new Category();
  46. }
  47. public CategoryDataAccountViewModel(ShmelevTeamContext ppContext, User currentUser)
  48. {
  49. UserIdLoad = currentUser.UserId;
  50. this.ppContext = ppContext;
  51. categoryItem = new Category();
  52. }
  53. public CategoryDataAccountViewModel(Category catItem, User currentUser, ShmelevTeamContext ppContext)
  54. {
  55. UserIdLoad = currentUser.UserId;
  56. this.ppContext = ppContext;
  57. this.categoryItem = catItem;
  58. categoryIdFinance = catItem.CategoryId;
  59. categoryTitle = catItem.Title;
  60. }
  61. public Dictionary<string, Category> GetDataFinance(User currentUser)
  62. {
  63. UserIdLoad = currentUser.UserId;
  64. if (CategoryIdFinance == null)
  65. return new Dictionary<string, Category> { { "1", null } };
  66. else if (CategoryTitle.Length > 50) return new Dictionary<string, Category> { { "2", null } };
  67. else
  68. {
  69. CategoryItem.IdUser = currentUser.UserId;
  70. CategoryItem.CategoryId = LoadCategoryId(CategoryTitle);
  71. CategoryItem.Title = CategoryTitle;
  72. return new Dictionary<string, Category> { { "4", CategoryItem } }; ;
  73. }
  74. }
  75. }
  76. }