123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using CommunityToolkit.Mvvm.ComponentModel;
- using DynamicData;
- using EntranseTesting.Models;
- using Microsoft.EntityFrameworkCore;
- using MsBox.Avalonia.Enums;
- using MsBox.Avalonia;
- using ReactiveUI;
- namespace EntranseTesting.ViewModels
- {
- public partial class TaskMainViewModel : ObservableObject, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- string? searchLine = "";
- bool selectedInTest = true;
- string selectedCategory = "Âñå òèïû çàäàíèé";
- string selectedSort = "Áåç ñîðòèðîâêè";
- [ObservableProperty] List<String> categoryList = ["Âñå òèïû çàäàíèé", "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà", "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà", "Óïîðÿäî÷èâàíèå ýëåìåíòîâ", "Ñîîòíîøåíèå âåëè÷èí", "Ñîîòíîøåíèå ýëåìåíòîâ", "Ïîäñòàíîâêà îòâåòîâ"];
- [ObservableProperty] List<String> sortList = ["Áåç ñîðòèðîâêè", "×àñòî âñòðå÷àåìûå âîïðîñû", "Ìàëî âñòðå÷àåìûå âîïðîñû", "Ïî êîëè÷åñòâó + îòâåòîâ", "Ïî êîëè÷åñòâó - îòâåòîâ"];
- ObservableCollection<Question> questions = new ObservableCollection<Question>();
- public ObservableCollection<Question> Questions { get => questions; set { questions = value; OnPropertyChanged("Questions"); } }
- public string SelectedCategory { get => selectedCategory; set { selectedCategory = value; OnPropertyChanged("SelectedCategory"); filter(); } }
- public string SelectedSort { get => selectedSort; set { selectedSort = value; OnPropertyChanged("SelectedSort"); filter(); } }
- public string? SearchLine { get => searchLine; set { searchLine = value; OnPropertyChanged("SearchLine"); filter(); } }
- public bool SelectedInTest { get => selectedInTest; set { selectedInTest = value; OnPropertyChanged("SelectedInTest"); filter(); } }
- public TaskMainViewModel()
- {
- filter();
- }
- public async void DeleteQuestion(int idQuestion)
- {
- try
- {
- var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå âîïðîñà", "Ìû íå ðåêîìåíäóåì âàì óäàëÿòü âîïðîñûþ Âû äåéñòâèòåëüíî õîòèòå óäàëèòü ýòîò âîïðîñ?", ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- Question q = connection.Questions.FirstOrDefault(tb => tb.Id == idQuestion);
- if (q != null)
- connection.Questions.Remove(q);
- connection.SaveChanges();
- filter();
- await MessageBoxManager.GetMessageBoxStandard("Óñïåøíîå óäàëåíèå", "Âîïðîñ óñïåøíî óäàëåí", ButtonEnum.Ok).ShowAsync();
- break;
- }
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.WriteLine(ex.Message);
- #endif
- }
- }
- private void filter()
- {
- try
- {
- Questions.Clear();
- List<Question> _list = new EntranceTestingContext().Questions
- .Include(tb => tb.IdCategoryNavigation).ThenInclude(tb => tb.IdOrientationNavigation)
- .Include(tb => tb.ElementOfChooses)
- .Include(tb => tb.ElementOfArrangements)
- .Include(tb => tb.TextOfPuttings).ThenInclude(tb => tb.ElementOfPuttings)
- .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement1Navigations)
- .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement2Navigations)
- .Include(tb => tb.Groups).ThenInclude(tb => tb.ElementOfGroups)
- .Include(tb => tb.UserResponses).ToList();
- //comboBox wiht typing Question (selected type)
- if (SelectedCategory != "Âñå òèïû çàäàíèé" && SelectedCategory != null)
- _list = _list.Where(tb => tb.IdCategoryNavigation.Name.ToLower().Contains(SelectedCategory.ToLower())).ToList();
- //search
- if (!string.IsNullOrWhiteSpace(SearchLine))
- _list = _list.Where(tb => tb.FillName.ToLower().Contains(SearchLine.ToLower())).ToList();
- if (SelectedInTest)
- _list = _list.Where(tb => tb.InTest == true).ToList();
- //sort
- switch (SelectedSort)
- {
- case "×àñòî âñòðå÷àåìûå âîïðîñû":
- {
- _list = _list.OrderByDescending(tb => tb.CountInResponse).ToList();
- break;
- }
- case "Ìàëî âñòðå÷àåìûå âîïðîñû":
- {
- _list = _list.OrderBy(tb => tb.CountInResponse).ToList();
- break;
- }
- case "Ïî êîëè÷åñòâó + îòâåòîâ":
- {
- _list = _list.OrderByDescending(tb => tb.CountCorrectly).ToList();
- break;
- }
- case "Ïî êîëè÷åñòâó - îòâåòîâ":
- {
- _list = _list.OrderByDescending(tb => tb.UnCountCorrectly).ToList();
- break;
- }
- default:
- break;
- }
- if (_list.Count > 0)
- foreach (Question q in _list)
- Questions.Add(q);
- else
- Questions = new ObservableCollection<Question>();
- }
- catch (Exception ex)
- {
- #if DEBUG
- Debug.WriteLine(ex.Message);
- #endif
- }
- }
- }
- }
|