using System; using System.Collections.Generic; using System.Linq; using AvaloniaApplication4.AdditionalModels; using AvaloniaApplication4.Models; using AvaloniaApplication4.Views.ActionPages; using Microsoft.EntityFrameworkCore; using ReactiveUI; namespace AvaloniaApplication4.ViewModels { public class EditAndAddModuleViewModel : ViewModelBase { GlobalsData globalsData; User? user; Module module; bool isNotNew = false; List mdkWithModulesBinding = new List(); public bool IsNotNew { get => isNotNew; set => isNotNew = value; } public Module Module { get => module; set => module = value; } public List MdkWithModulesBinding { get { return mdkWithModulesBinding; } set { mdkWithModulesBinding = value; OnPropertyChanged("MdkWithModulesBinding"); } } public EditAndAddModuleViewModel(GlobalsData globalsData, User? user) { this.globalsData = globalsData; this.user = user; module = new Module(); } public EditAndAddModuleViewModel(GlobalsData globalsData, User? user, Module module) : this(globalsData, user) { this.module = module; isNotNew = true; var all = dataBase.MdkWithModules .Include(x => x.IdMdkNavigation) .Include(x => x.IdModuleNavigation) .Where(x => x.IdModule == module.IdModule) .ToList(); foreach (var obj in all) { var currentSubject = dataBase.Subjects.FirstOrDefault(x => x.IdSubject == obj.IdMdk); if (currentSubject != null) { mdkWithModulesBinding.Add(new BindingMdkWithModels(currentSubject)); } } } public void DeleteCurrentSubject(int idSubject) { var temp = MdkWithModulesBinding; var currentSubject = temp.FirstOrDefault(x => x.Id == idSubject); if (currentSubject != null) { temp.Remove(currentSubject); } MdkWithModulesBinding = temp; } public void AddNewSubject() { var temp = MdkWithModulesBinding; temp.Add(new BindingMdkWithModels()); MdkWithModulesBinding = temp; } public void Save() { if (Module.CodeModule != string.Empty && Module.Name != string.Empty) { if (!isNotNew) { dataBase.Modules.Add(Module); //тут надо внести в базу все предметы которые относятся к модулю, исключая при этом повторяющиеся элементы } else { var obj = dataBase.Modules.FirstOrDefault(x => x.IdModule == Module.IdModule); if (obj != null) { obj.Name = Module.Name; obj.CodeModule = Module.CodeModule; //тут тоже самое, но немного сложнее, надо проанализировать уже имеющиеся записи и добавить только новые также исключая повторение DeleteDuplicates(); var allItems = dataBase.MdkWithModules.Where(x => x.IdModule == obj.IdModule).ToList(); foreach (var item in allItems) { dataBase.MdkWithModules.Remove(item); } dataBase.SaveChanges(); foreach (var item in MdkWithModulesBinding) { var newObj = new MdkWithModule() { IdMdk = item.Id, IdModule = obj.IdModule }; dataBase.MdkWithModules.Add(newObj); } dataBase.SaveChanges(); } } dataBase.SaveChanges(); } } public void Delete() { if (isNotNew) { var obj = dataBase.Modules.FirstOrDefault(x => x.IdModule == Module.IdModule); if (obj != null) { dataBase.Modules.Remove(obj); dataBase.SaveChanges(); GoBack(); } } } /// /// Исключение повторяющихся записей в списке предметов /// private void DeleteDuplicates() { for (int i = 1; i < MdkWithModulesBinding.Count; i++) { if (MdkWithModulesBinding[i-1].Id == MdkWithModulesBinding[i].Id) { MdkWithModulesBinding.Remove(MdkWithModulesBinding[i]); } } } public void GoBack() { globalsData.GlobalUserControl = new AllSubjectsModulesAndProgramTemplates(user, ref globalsData); } public class BindingMdkWithModels: ViewModelBase { List allMDK; Subject currentMDK; int id; public BindingMdkWithModels() { allMDK = dataBase.Subjects .Include(x=>x.IdCodeAcademicSubjectNavigation) .Where(x=>x.IdCodeAcademicSubjectNavigation.IdCodeAcademicSubjects == "МДК") .ToList(); currentMDK = allMDK.First(); id = currentMDK.IdSubject; } public BindingMdkWithModels(Subject currentMDK) : this() { this.currentMDK = allMDK.FirstOrDefault(x=>x.IdSubject == currentMDK.IdSubject); id = this.currentMDK.IdSubject; } public List AllMDK { get => allMDK; set => allMDK = value; } public Subject CurrentMDK { get => currentMDK; set => currentMDK = value; } public int Id { get => id; set => id = value; } } } }