import users, delete users

This commit is contained in:
1billy17 2024-12-05 11:26:58 +03:00
parent 380963926e
commit 5626470f65
20 changed files with 316 additions and 147 deletions

View File

@ -1,14 +1,8 @@
<Application xmlns="https://github.com/avaloniaui" <Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Presence.Desktop.App" x:Class="Presence.Desktop.App"
xmlns:local="using:Presence.Desktop"
RequestedThemeVariant="Default"> RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles> <Application.Styles>
<FluentTheme /> <FluentTheme />
</Application.Styles> </Application.Styles>

View File

@ -20,13 +20,13 @@ namespace Presence.Desktop
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddCommonService(); serviceCollection.AddCommonService();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
var mainViewModel = services.GetRequiredService<MainWindowViewModel>(); var mainViewModel = services.GetRequiredService<GroupViewModel>();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
desktop.MainWindow = new MainWindow desktop.MainWindow = new MainWindow()
{ {
DataContext = mainViewModel, DataContext = new MainWindowViewModel(services),
}; };
} }

View File

@ -21,7 +21,7 @@ namespace Presence.Desktop.DI
.AddSingleton<IUserRepository, SQLUserRepositoryImpl>() .AddSingleton<IUserRepository, SQLUserRepositoryImpl>()
.AddTransient<IGroupUseCase, GroupUseCase>() .AddTransient<IGroupUseCase, GroupUseCase>()
.AddTransient<IUserUseCase, UserUseCase>() .AddTransient<IUserUseCase, UserUseCase>()
.AddTransient<MainWindowViewModel>(); .AddTransient<GroupViewModel>();
} }
} }
} }

View File

@ -9,7 +9,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" /> <AvaloniaResource Include="Assets\**" />
</ItemGroup> </ItemGroup>

View File

@ -2,31 +2,18 @@ using Avalonia.Controls;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Presence.Desktop.ViewModels; using Presence.Desktop.ViewModels;
using System; using System;
using Presence.Desktop.Views;
using ReactiveUI;
namespace Presence.Desktop namespace Presence.Desktop
{ {
public class ViewLocator : IDataTemplate public class ViewLocator : IViewLocator
{ {
public IViewFor? ResolveView<T>(T? viewModel, string? contract = null) => viewModel switch
public Control? Build(object? param)
{ {
if (param is null) GroupViewModel groupViewModel => new GroupView{DataContext = groupViewModel},
return null; PresenceViewModel presenceViewModel => new PresenceView{DataContext = presenceViewModel},
_ => throw new ArgumentOutOfRangeException(nameof(viewModel))
var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal); };
var type = Type.GetType(name);
if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
return new TextBlock { Text = "Not Found: " + name };
}
public bool Match(object? data)
{
return data is ViewModelBase;
}
} }
} }

View File

@ -0,0 +1,148 @@
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; }
}
}

View File

@ -1,72 +1,16 @@
using Demo.Domain.UseCase; using System;
using DynamicData; using Microsoft.Extensions.DependencyInjection;
using DynamicData.Binding;
using Presence.Desktop.Models;
using ReactiveUI; using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using Tmds.DBus.Protocol;
namespace Presence.Desktop.ViewModels namespace Presence.Desktop.ViewModels;
public class MainWindowViewModel: ViewModelBase, IScreen
{ {
public class MainWindowViewModel : ViewModelBase public RoutingState Router { get; } = new RoutingState();
public MainWindowViewModel(IServiceProvider serviceProvider)
{ {
private readonly IGroupUseCase _groupService; var groupViewModel = serviceProvider.GetRequiredService<GroupViewModel>();
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>(); Router.Navigate.Execute(groupViewModel);
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
private GroupPresenter? _selectedGroupItem;
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
public MainWindowViewModel(IGroupUseCase groupUseCase)
{
_groupService = groupUseCase;
foreach (var item in _groupService.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>();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
}
}
} }
} }

View File

@ -0,0 +1,9 @@
using ReactiveUI;
namespace Presence.Desktop.ViewModels;
public class PresenceViewModel: ViewModelBase, IRoutableViewModel
{
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}

View File

@ -0,0 +1,46 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Presence.Desktop.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Presence.Desktop.Views.GroupView"
x:DataType="vm:GroupViewModel">
<Design.DataContext>
<vm:GroupViewModel/>
</Design.DataContext>
<DockPanel Background="Black">
<StackPanel DockPanel.Dock="Bottom">
<TextBlock Text="List ↑" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel
Spacing="10"
HorizontalAlignment="Center"
DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock Text="Combobox ->"/>
<ComboBox ItemsSource="{Binding Groups}" SelectedValue="{Binding SelectedGroupItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Delete Users"
Command="{Binding DeleteUsersByGroupCommand}"/>
<Button Content="Import Students" Command="{Binding ImportStudentsCommand}"/>
</StackPanel>
<Border>
<ListBox Background="Black" ItemsSource="{Binding Users}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</DockPanel>
</UserControl>

View File

@ -0,0 +1,17 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Presence.Desktop.ViewModels;
using ReactiveUI;
namespace Presence.Desktop.Views
{
public partial class GroupView : ReactiveUserControl<GroupViewModel>
{
public GroupView()
{
this.WhenActivated(disposables => { });
AvaloniaXamlLoader.Load(this);
}
}
}

View File

@ -1,47 +1,19 @@
<Window xmlns="https://github.com/avaloniaui" <Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:Presence.Desktop.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:app="clr-namespace:Presence.Desktop"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Presence.Desktop.ViewModels"
xmlns:reactiveUi="http://reactiveui.net"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Presence.Desktop.Views.MainWindow" x:Class="Presence.Desktop.Views.MainWindow"
x:DataType="vm:MainWindowViewModel" x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico" Title="MainWindow">
Title="Presence.Desktop"> <DockPanel>
<reactiveUi:RoutedViewHost Router="{Binding Router}" DockPanel.Dock="Right" Background="Black">
<Design.DataContext> <reactiveUi:RoutedViewHost.ViewLocator>
<vm:MainWindowViewModel/> <app:ViewLocator/>
</Design.DataContext> </reactiveUi:RoutedViewHost.ViewLocator>
<DockPanel Background="Azure"> </reactiveUi:RoutedViewHost>
<StackPanel DockPanel.Dock="Bottom"> </DockPanel>
<TextBlock Text="List ↑" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel
Spacing="10"
HorizontalAlignment="Center"
DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock Text="Combobox ->"/>
<ComboBox ItemsSource="{Binding Groups}" SelectedValue="{Binding SelectedGroupItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox/>
<ComboBox/>
</StackPanel>
<Border>
<ListBox Background="Bisque" ItemsSource="{Binding Users}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</DockPanel>
</Window> </Window>

View File

@ -1,12 +1,18 @@
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Presence.Desktop.ViewModels;
using ReactiveUI;
namespace Presence.Desktop.Views namespace Presence.Desktop.Views;
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{ {
public partial class MainWindow : Window public MainWindow()
{ {
public MainWindow() this.WhenActivated(disposables => { });
{ AvaloniaXamlLoader.Load(this);
InitializeComponent();
}
} }
} }

View File

@ -0,0 +1,8 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Presence.Desktop.Views.PresenceView">
Welcome to Avalonia!
</UserControl>

View File

@ -0,0 +1,16 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Presence.Desktop.ViewModels;
using ReactiveUI;
namespace Presence.Desktop.Views;
public partial class PresenceView : ReactiveUserControl<PresenceViewModel>
{
public PresenceView()
{
this.WhenActivated(disposables => { });
AvaloniaXamlLoader.Load(this); }
}

View File

@ -8,6 +8,7 @@ namespace Demo.Data.Repository
UserLocalEntity? GetUserByGuid(Guid guid); UserLocalEntity? GetUserByGuid(Guid guid);
List<UserLocalEntity> GetUsersByGroupID(int groupID); List<UserLocalEntity> GetUsersByGroupID(int groupID);
bool RemoveUserByGuid(Guid guid); bool RemoveUserByGuid(Guid guid);
bool RemoveUsersByGroupID(int GroupID);
public UserLocalEntity? CreateUser(string FIO, string GroupName); public UserLocalEntity? CreateUser(string FIO, string GroupName);
UserLocalEntity? UpdateUser(UserLocalEntity updatedUser); UserLocalEntity? UpdateUser(UserLocalEntity updatedUser);
} }

View File

@ -47,6 +47,20 @@ namespace Demo.Data.Repository
return true; return true;
} }
public bool RemoveUsersByGroupID(int GroupID){
var delRecords = _remoteDatabaseContext.Users
.Where(x => x.GroupID == GroupID).ToList();
_remoteDatabaseContext.Users.RemoveRange(delRecords);
_remoteDatabaseContext.SaveChanges();
if (delRecords.Count == 0) {
return false;
}
return true;
}
public UserLocalEntity? CreateUser(string FIO, string GroupName){ public UserLocalEntity? CreateUser(string FIO, string GroupName){
var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Name == GroupName); var groupDAO = _remoteDatabaseContext.Groups.FirstOrDefault(x => x.Name == GroupName);
UserDAO userDAO = new UserDAO{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID}; UserDAO userDAO = new UserDAO{FIO = FIO, Guid = Guid.NewGuid(), GroupID = groupDAO.ID};

View File

@ -6,6 +6,7 @@ namespace Demo.Domain.UseCase
{ {
List<User> GetAllUsers(); List<User> GetAllUsers();
bool RemoveUserByGuid(Guid userGuid); bool RemoveUserByGuid(Guid userGuid);
bool RemoveUsersByGroupID(int GroupID);
bool CreateUser(User user); bool CreateUser(User user);
User UpdateUser(User user); User UpdateUser(User user);
User GetUserByGuid(Guid userGuid); User GetUserByGuid(Guid userGuid);

View File

@ -70,6 +70,10 @@ namespace Demo.Domain.UseCase
return _repositoryUserImpl.RemoveUserByGuid(userGuid); return _repositoryUserImpl.RemoveUserByGuid(userGuid);
} }
public bool RemoveUsersByGroupID(int GroupID) {
return _repositoryUserImpl.RemoveUsersByGroupID(GroupID);
}
public User UpdateUser(User user) public User UpdateUser(User user)
{ {
UserLocalEntity userLocalEntity = new UserLocalEntity UserLocalEntity userLocalEntity = new UserLocalEntity

3
students.csv Normal file
View File

@ -0,0 +1,3 @@
FIO
Zamir Bezgeev
Yasha Lava
1 FIO
2 Zamir Bezgeev
3 Yasha Lava