From 2b38373d9f6994d8a3d046a8ba857ae8b9b142a9 Mon Sep 17 00:00:00 2001 From: Zagrebin Date: Thu, 12 Dec 2024 10:20:21 +0300 Subject: [PATCH] main group window --- Presence.Desktop/Helpers/CsvHelper.cs | 41 +++++ Presence.Desktop/Models/GroupPresenter.cs | 2 +- Presence.Desktop/Presence.Desktop.csproj | 4 - .../ViewModels/EditWindowViewModel.cs | 6 + .../ViewModels/MainWindowViewModel.cs | 153 ++++++++++++++++-- Presence.Desktop/Views/EditWindow.axaml | 17 ++ Presence.Desktop/Views/EditWindow.axaml.cs | 13 ++ Presence.Desktop/Views/MainWindow.axaml | 39 +++-- Presence.Desktop/Views/MainWindow.axaml.cs | 5 +- data/Repository/IGroupRepository.cs | 24 +++ data/Repository/SQLGroupRepository.cs | 50 +++++- domain/Service/GroupService.cs | 25 +++ domain/UseCase/IGroupUseCase.cs | 6 + 13 files changed, 358 insertions(+), 27 deletions(-) create mode 100644 Presence.Desktop/Helpers/CsvHelper.cs create mode 100644 Presence.Desktop/ViewModels/EditWindowViewModel.cs create mode 100644 Presence.Desktop/Views/EditWindow.axaml create mode 100644 Presence.Desktop/Views/EditWindow.axaml.cs diff --git a/Presence.Desktop/Helpers/CsvHelper.cs b/Presence.Desktop/Helpers/CsvHelper.cs new file mode 100644 index 0000000..f342f4c --- /dev/null +++ b/Presence.Desktop/Helpers/CsvHelper.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using domain.Request; +using Presence.Desktop.Models; + +namespace Presence.Desktop.Helpers +{ + public static class CsvHelper + { + public static IEnumerable ReadCsvStudents(string path) + { + List students = new List(); + var csvLines = File.ReadAllLines(path, Encoding.GetEncoding("utf-8")); + + using (var reader = new StreamReader(path)) + { + string line; + + while ((line = reader.ReadLine()) != null) + { + var values = line.Split(';'); + if (values.Length != 3) + continue; + + students.Add(new AddStudentRequest + { + LastName = values[0], + FirstName = values[1], + Patronymic = values[2] + }); + } + } + + return students; + } + } +} diff --git a/Presence.Desktop/Models/GroupPresenter.cs b/Presence.Desktop/Models/GroupPresenter.cs index aad4c03..1ecc2e1 100644 --- a/Presence.Desktop/Models/GroupPresenter.cs +++ b/Presence.Desktop/Models/GroupPresenter.cs @@ -10,6 +10,6 @@ namespace Presence.Desktop.Models { public int Id { get; set; } public string Name { get; set; } - public IEnumerable? students = null; + public List? students = null; } } diff --git a/Presence.Desktop/Presence.Desktop.csproj b/Presence.Desktop/Presence.Desktop.csproj index a7928e1..e14f7e3 100644 --- a/Presence.Desktop/Presence.Desktop.csproj +++ b/Presence.Desktop/Presence.Desktop.csproj @@ -10,10 +10,6 @@ - - - - diff --git a/Presence.Desktop/ViewModels/EditWindowViewModel.cs b/Presence.Desktop/ViewModels/EditWindowViewModel.cs new file mode 100644 index 0000000..5baabc0 --- /dev/null +++ b/Presence.Desktop/ViewModels/EditWindowViewModel.cs @@ -0,0 +1,6 @@ +namespace Presence.Desktop.ViewModels; + +public class EditWindowViewModel : ViewModelBase +{ + +} \ No newline at end of file diff --git a/Presence.Desktop/ViewModels/MainWindowViewModel.cs b/Presence.Desktop/ViewModels/MainWindowViewModel.cs index 0648dbe..37b9615 100644 --- a/Presence.Desktop/ViewModels/MainWindowViewModel.cs +++ b/Presence.Desktop/ViewModels/MainWindowViewModel.cs @@ -1,5 +1,6 @@ using domain.UseCase; using Presence.Desktop.Models; +using Presence.Desktop.Helpers; using System; using System.Reactive.Linq; using System.Reactive; @@ -7,8 +8,13 @@ using System.Linq; using ReactiveUI; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO; using System.Windows.Input; using System.Threading.Tasks; +using Avalonia.Controls.Selection; +using domain.Request; +using System.Text; namespace Presence.Desktop.ViewModels { @@ -45,8 +51,32 @@ namespace Presence.Desktop.ViewModels set => this.RaiseAndSetIfChanged(ref _selectedFile, value); } + private bool _MultipleSelected = false; + public bool MultipleSelected { get => _MultipleSelected; set => this.RaiseAndSetIfChanged(ref _MultipleSelected, value); } + public SelectionModel Selection { get; } + + private string _selectedSort = "Релевантность"; + + public string SelectedSort + { + get => _selectedSort; + set => this.RaiseAndSetIfChanged(ref _selectedSort, value); + } + + public List Sorting { get; set; } = + [ + "Релевантность", + "А-Я", + "Я-А" + ]; + public ICommand OpenFileDialog { get; } + public ICommand EditCommand { get; } + public ICommand DeleteCommand { get; } + public ICommand DeleteSelectedCommand { get; } + public ICommand DeleteAllCommand { get; } + public MainWindowViewModel() { _groupService = null; @@ -63,12 +93,25 @@ namespace Presence.Desktop.ViewModels _students = new(); - this.WhenAnyValue(vm => vm.SelectedGroupItem) + this.WhenAnyValue(vm => vm.SelectedGroupItem, + x => x.SelectedSort) .Subscribe(_ => { RefreshGroups(); - SetStudents(); + SetStudents(SelectedSort); }); + + Selection = new SelectionModel(); + Selection.SingleSelect = false; + Selection.SelectionChanged += (sender, args) => + { + MultipleSelected = Selection.SelectedItems.Count > 1; + }; + + DeleteCommand = ReactiveCommand.Create(DeleteStudent); + DeleteSelectedCommand = ReactiveCommand.Create(DeleteStudent); + DeleteAllCommand = ReactiveCommand.Create(DeleteAllStudents); + } private void RefreshGroups() @@ -91,7 +134,7 @@ namespace Presence.Desktop.ViewModels Id = item.Id, Name = item.Name } - }) + }).ToList() }; groupPresenters.Add(groupPresenter); } @@ -99,25 +142,117 @@ namespace Presence.Desktop.ViewModels _groups = new(groupPresenters); } - private void SetStudents() + private void SetStudents(string selectedSort) { if (SelectedGroupItem == null) return; if (SelectedGroupItem.students == null) return; Students.Clear(); - foreach (var student in SelectedGroupItem.students) + List students = new(_groupService.GetGroupsWithStudents() + .Single(g => g.Id == SelectedGroupItem.Id) + .Users + .Select(u => new StudentPresenter + { + Id = u.Id, + FirstName = u.FirstName, + LastName = u.LastName, + Patronymic = u.Patronymic, + Group = new GroupPresenter() + { + Id = SelectedGroupItem.Id, + Name = SelectedGroupItem.Name + } + }) + ); + + students = selectedSort switch + { + "Релевантность" => students.OrderBy(s => s.Id).ToList(), + "А-Я" => students.OrderBy(s => s.LastName).ToList(), + "Я-А" => students.OrderByDescending(s => s.LastName).ToList(), + _ => students + }; + + foreach (var student in students) Students.Add(student); } private async Task SelectFile() { - SelectedFile = await ShowOpenFileDialog.Handle(Unit.Default); - - if (SelectedFile is object) + try { - + SelectedFile = await ShowOpenFileDialog.Handle(Unit.Default); } + catch (Exception) + { + return; + } + + if (SelectedGroupItem == null) return; + + try + { + var studentsToAdd = CsvHelper.ReadCsvStudents(SelectedFile); + + if (studentsToAdd.Any()) + { + _groupService.AddStudentsToGroup(SelectedGroupItem.Id, studentsToAdd); + + foreach (var studentRequest in studentsToAdd) + { + var studentPresenter = new StudentPresenter + { + LastName = studentRequest.LastName, + FirstName = studentRequest.FirstName, + Patronymic = studentRequest.Patronymic, + Group = SelectedGroupItem + }; + + SelectedGroupItem.students.Add(studentPresenter); + Students.Add(studentPresenter); + } + + RefreshGroups(); + SetStudents(SelectedSort); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } + + } + + private void DeleteStudent() + { + if (Selection.SelectedItems == null || Selection.SelectedItems.Count == 0) return; + + var studentIds = Selection.SelectedItems.Select(s => s.Id).ToList(); + _groupService.RemoveStudentsFromGroupByIds(SelectedGroupItem.Id, studentIds); + + foreach (var student in Selection.SelectedItems.ToList()) + { + SelectedGroupItem.students.Remove(student); + Students.Remove(student); + } + + SetStudents(SelectedSort); + RefreshGroups(); + } + + private void DeleteAllStudents() + { + if (Students.Count == 0 && SelectedGroupItem == null) return; + + _groupService.RemoveStudentsFromGroup(SelectedGroupItem.Id); + + SelectedGroupItem.students.Clear(); + Students.Clear(); + + RefreshGroups(); + SetStudents(SelectedSort); + } } diff --git a/Presence.Desktop/Views/EditWindow.axaml b/Presence.Desktop/Views/EditWindow.axaml new file mode 100644 index 0000000..2192aa8 --- /dev/null +++ b/Presence.Desktop/Views/EditWindow.axaml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/Presence.Desktop/Views/EditWindow.axaml.cs b/Presence.Desktop/Views/EditWindow.axaml.cs new file mode 100644 index 0000000..75e5b39 --- /dev/null +++ b/Presence.Desktop/Views/EditWindow.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace Presence.Desktop; + +public partial class EditWindow : Window +{ + public EditWindow() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/Presence.Desktop/Views/MainWindow.axaml b/Presence.Desktop/Views/MainWindow.axaml index febbbe8..0fe8384 100644 --- a/Presence.Desktop/Views/MainWindow.axaml +++ b/Presence.Desktop/Views/MainWindow.axaml @@ -13,28 +13,44 @@ - + - - - - - - - +