EditAndAddModuleViewModel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using AvaloniaApplication4.AdditionalModels;
  5. using AvaloniaApplication4.Models;
  6. using AvaloniaApplication4.Views.ActionPages;
  7. using Microsoft.EntityFrameworkCore;
  8. using ReactiveUI;
  9. namespace AvaloniaApplication4.ViewModels
  10. {
  11. public class EditAndAddModuleViewModel : ViewModelBase
  12. {
  13. GlobalsData globalsData;
  14. User? user;
  15. Module module;
  16. bool isNotNew = false;
  17. List<BindingMdkWithModels> mdkWithModulesBinding = new List<BindingMdkWithModels>();
  18. public bool IsNotNew { get => isNotNew; set => isNotNew = value; }
  19. public Module Module { get => module; set => module = value; }
  20. public List<BindingMdkWithModels> MdkWithModulesBinding
  21. {
  22. get { return mdkWithModulesBinding; }
  23. set
  24. {
  25. mdkWithModulesBinding = value;
  26. OnPropertyChanged("MdkWithModulesBinding");
  27. }
  28. }
  29. public EditAndAddModuleViewModel(GlobalsData globalsData, User? user)
  30. {
  31. this.globalsData = globalsData;
  32. this.user = user;
  33. module = new Module();
  34. }
  35. public EditAndAddModuleViewModel(GlobalsData globalsData, User? user, Module module) : this(globalsData, user)
  36. {
  37. this.module = module;
  38. isNotNew = true;
  39. var all = dataBase.MdkWithModules
  40. .Include(x => x.IdMdkNavigation)
  41. .Include(x => x.IdModuleNavigation)
  42. .Where(x => x.IdModule == module.IdModule)
  43. .ToList();
  44. foreach (var obj in all)
  45. {
  46. var currentSubject = dataBase.Subjects.FirstOrDefault(x => x.IdSubject == obj.IdMdk);
  47. if (currentSubject != null)
  48. {
  49. mdkWithModulesBinding.Add(new BindingMdkWithModels(currentSubject));
  50. }
  51. }
  52. }
  53. public void DeleteCurrentSubject(int idSubject)
  54. {
  55. var temp = MdkWithModulesBinding;
  56. var currentSubject = temp.FirstOrDefault(x => x.Id == idSubject);
  57. if (currentSubject != null)
  58. {
  59. temp.Remove(currentSubject);
  60. }
  61. MdkWithModulesBinding = temp;
  62. }
  63. public void AddNewSubject()
  64. {
  65. var temp = MdkWithModulesBinding;
  66. temp.Add(new BindingMdkWithModels());
  67. MdkWithModulesBinding = temp;
  68. }
  69. public void Save()
  70. {
  71. if (Module.CodeModule != string.Empty && Module.Name != string.Empty)
  72. {
  73. if (!isNotNew)
  74. {
  75. dataBase.Modules.Add(Module);
  76. //òóò íàäî âíåñòè â áàçó âñå ïðåäìåòû êîòîðûå îòíîñÿòñÿ ê ìîäóëþ, èñêëþ÷àÿ ïðè ýòîì ïîâòîðÿþùèåñÿ ýëåìåíòû
  77. }
  78. else
  79. {
  80. var obj = dataBase.Modules.FirstOrDefault(x => x.IdModule == Module.IdModule);
  81. if (obj != null)
  82. {
  83. obj.Name = Module.Name;
  84. obj.CodeModule = Module.CodeModule;
  85. //òóò òîæå ñàìîå, íî íåìíîãî ñëîæíåå, íàäî ïðîàíàëèçèðîâàòü óæå èìåþùèåñÿ çàïèñè è äîáàâèòü òîëüêî íîâûå òàêæå èñêëþ÷àÿ ïîâòîðåíèå
  86. DeleteDuplicates();
  87. var allItems = dataBase.MdkWithModules.Where(x => x.IdModule == obj.IdModule).ToList();
  88. foreach (var item in allItems)
  89. {
  90. dataBase.MdkWithModules.Remove(item);
  91. }
  92. dataBase.SaveChanges();
  93. foreach (var item in MdkWithModulesBinding)
  94. {
  95. var newObj = new MdkWithModule() { IdMdk = item.Id, IdModule = obj.IdModule };
  96. dataBase.MdkWithModules.Add(newObj);
  97. }
  98. dataBase.SaveChanges();
  99. }
  100. }
  101. dataBase.SaveChanges();
  102. }
  103. }
  104. public void Delete()
  105. {
  106. if (isNotNew)
  107. {
  108. var obj = dataBase.Modules.FirstOrDefault(x => x.IdModule == Module.IdModule);
  109. if (obj != null)
  110. {
  111. dataBase.Modules.Remove(obj);
  112. dataBase.SaveChanges();
  113. GoBack();
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// Èñêëþ÷åíèå ïîâòîðÿþùèõñÿ çàïèñåé â ñïèñêå ïðåäìåòîâ
  119. /// </summary>
  120. private void DeleteDuplicates()
  121. {
  122. for (int i = 1; i < MdkWithModulesBinding.Count; i++)
  123. {
  124. if (MdkWithModulesBinding[i-1].Id == MdkWithModulesBinding[i].Id)
  125. {
  126. MdkWithModulesBinding.Remove(MdkWithModulesBinding[i]);
  127. }
  128. }
  129. }
  130. public void GoBack()
  131. {
  132. globalsData.GlobalUserControl = new AllSubjectsModulesAndProgramTemplates(user, ref globalsData);
  133. }
  134. public class BindingMdkWithModels: ViewModelBase
  135. {
  136. List<Subject> allMDK;
  137. Subject currentMDK;
  138. int id;
  139. public BindingMdkWithModels()
  140. {
  141. allMDK = dataBase.Subjects
  142. .Include(x=>x.IdCodeAcademicSubjectNavigation)
  143. .Where(x=>x.IdCodeAcademicSubjectNavigation.IdCodeAcademicSubjects == "ÌÄÊ")
  144. .ToList();
  145. currentMDK = allMDK.First();
  146. id = currentMDK.IdSubject;
  147. }
  148. public BindingMdkWithModels(Subject currentMDK) : this()
  149. {
  150. this.currentMDK = allMDK.FirstOrDefault(x=>x.IdSubject == currentMDK.IdSubject);
  151. id = this.currentMDK.IdSubject;
  152. }
  153. public List<Subject> AllMDK { get => allMDK; set => allMDK = value; }
  154. public Subject CurrentMDK { get => currentMDK; set => currentMDK = value; }
  155. public int Id { get => id; set => id = value; }
  156. }
  157. }
  158. }