123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<BindingMdkWithModels> mdkWithModulesBinding = new List<BindingMdkWithModels>();
- public bool IsNotNew { get => isNotNew; set => isNotNew = value; }
- public Module Module { get => module; set => module = value; }
- public List<BindingMdkWithModels> 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();
- }
- }
- }
- /// <summary>
- /// Èñêëþ÷åíèå ïîâòîðÿþùèõñÿ çàïèñåé â ñïèñêå ïðåäìåòîâ
- /// </summary>
- 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<Subject> 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<Subject> AllMDK { get => allMDK; set => allMDK = value; }
- public Subject CurrentMDK { get => currentMDK; set => currentMDK = value; }
- public int Id { get => id; set => id = value; }
- }
- }
- }
|