presence/Presence.Desktop/ViewModels/GroupViewModel.cs

148 lines
4.8 KiB
C#
Raw Normal View History

2024-12-05 08:26:58 +00:00
using Demo.Domain.UseCase;
using Presence.Desktop.Models;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Avalonia.Controls;
using System.IO;
using Demo.Domain.Models;
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
private GroupPresenter? _selectedGroupItem;
public ReactiveCommand<Unit, Unit> DeleteUsersByGroupCommand { get; }
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
{
foreach (var item in groupUseCase.GetAllGroupsWithUsers())
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.ID,
Name = item.Name,
users = item.Users?.Select(user => new UserPresenter
{
Name = user.FIO,
Guid = user.Guid,
Group = new GroupPresenter { Id = item.ID, Name = item.Name }
}
).ToList()
};
_groupPresentersDataSource.Add(groupPresenter);
}
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID(userUseCase));
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () =>
{
var openFileDialog = new OpenFileDialog
{
Filters = new List<FileDialogFilter>
{
new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } }
},
AllowMultiple = false
};
var result = await openFileDialog.ShowAsync(new Window());
if (result?.Length > 0) {
var filePath = result[0];
ImportStudents(filePath, userUseCase);
}
});
}
private void ImportStudents(string filePath, IUserUseCase userUseCase)
{
if (SelectedGroupItem == null || SelectedGroupItem.Id == null)
{
return;
}
var lines = File.ReadAllLines(filePath);
foreach (var line in lines.Skip(1))
{
var columns = line.Split(',');
if (columns.Length < 1) continue;
var fio = columns[0];
var newUser = new User
{
FIO = fio,
Group = new Group
{
Name = SelectedGroupItem.Name,
ID = SelectedGroupItem.Id
}
};
var result = userUseCase.CreateUser(newUser);
if (result)
{
var userPresenter = new UserPresenter
{
Name = fio,
Guid = Guid.NewGuid(),
Group = SelectedGroupItem
};
_users.Add(userPresenter);
}
}
}
private void DeleteUsersByGroupID(IUserUseCase userUseCase) {
if (SelectedGroupItem == null)
return;
var groupID = SelectedGroupItem.Id;
bool res = userUseCase.RemoveUsersByGroupID(groupID);
if (res) {
Users.Clear();
}
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
}
}
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}
}