From 5115ae0e87cc434a905799c38fb1a5e9f85bc39d Mon Sep 17 00:00:00 2001 From: adm Date: Mon, 25 Nov 2024 13:47:17 +0300 Subject: [PATCH] add logic in vm --- Presence.Api/Controllers/GroupController.cs | 9 +-- Presence.Desktop/Models/GroupPresenter.cs | 17 +++++ Presence.Desktop/Models/UserPresenter.cs | 15 +++++ Presence.Desktop/Presence.Desktop.csproj | 1 - .../ViewModels/MainWindowViewModel.cs | 66 ++++++++++++++++++- Presence.Desktop/Views/MainWindow.axaml | 18 ++++- data/Repository/IGroupRepository.cs | 1 + data/Repository/SQLGroupRepository.cs | 5 ++ domain/Entity/GroupEntity.cs | 2 +- domain/Service/GroupService.cs | 23 +++++++ domain/UseCase/IGroupUseCase.cs | 1 + 11 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 Presence.Desktop/Models/GroupPresenter.cs create mode 100644 Presence.Desktop/Models/UserPresenter.cs diff --git a/Presence.Api/Controllers/GroupController.cs b/Presence.Api/Controllers/GroupController.cs index 2902cdf..b748f37 100644 --- a/Presence.Api/Controllers/GroupController.cs +++ b/Presence.Api/Controllers/GroupController.cs @@ -16,10 +16,11 @@ namespace Presence.Api.Controllers } [HttpGet("/group")] - public ActionResult GetAllGroups() { + public async Task> GetAllGroups() { - var result = _groupService - .GetGroupsWithStudents() + var result = await _groupService + .GetGroupsWithStudentsAsync(); + var response = result .Select(group => new GroupResponse { Id = group.Id, Name = group.Name, @@ -27,7 +28,7 @@ namespace Presence.Api.Controllers user => new UserResponse { Guid = user.Guid, Name = user.Name, - }).Take(10).ToList() + }).ToList() }).ToList(); return Ok(result); diff --git a/Presence.Desktop/Models/GroupPresenter.cs b/Presence.Desktop/Models/GroupPresenter.cs new file mode 100644 index 0000000..3cc3cf8 --- /dev/null +++ b/Presence.Desktop/Models/GroupPresenter.cs @@ -0,0 +1,17 @@ +using Avalonia.Controls; +using ReactiveUI; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Presence.Desktop.Models +{ + public class GroupPresenter + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable? users { get; set; } = null; + } +} diff --git a/Presence.Desktop/Models/UserPresenter.cs b/Presence.Desktop/Models/UserPresenter.cs new file mode 100644 index 0000000..385a029 --- /dev/null +++ b/Presence.Desktop/Models/UserPresenter.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Presence.Desktop.Models +{ + public class UserPresenter + { + public Guid Guid { get; set; } + public string Name { get; set; } + public GroupPresenter Group { get; set; } + } +} diff --git a/Presence.Desktop/Presence.Desktop.csproj b/Presence.Desktop/Presence.Desktop.csproj index 92a908c..33feb38 100644 --- a/Presence.Desktop/Presence.Desktop.csproj +++ b/Presence.Desktop/Presence.Desktop.csproj @@ -9,7 +9,6 @@ - diff --git a/Presence.Desktop/ViewModels/MainWindowViewModel.cs b/Presence.Desktop/ViewModels/MainWindowViewModel.cs index 801aa69..8eb6331 100644 --- a/Presence.Desktop/ViewModels/MainWindowViewModel.cs +++ b/Presence.Desktop/ViewModels/MainWindowViewModel.cs @@ -1,13 +1,73 @@ using domain.UseCase; +using DynamicData; +using DynamicData.Binding; +using Presence.Desktop.Models; +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 { public class MainWindowViewModel : ViewModelBase { private readonly IGroupUseCase _groupService; - public MainWindowViewModel(IGroupUseCase groupUseCase) { - _groupService = groupUseCase; + private List groupPresentersDataSource = new List(); + private ObservableCollection _groups; + public ObservableCollection Groups => _groups; + + public GroupPresenter? SelectedGroupItem { + get => _selectedGroupItem; + set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } + + private GroupPresenter? _selectedGroupItem; + + + + public ObservableCollection Users { get => _users;} + public ObservableCollection _users; + public MainWindowViewModel(IGroupUseCase groupUseCase) + { + _groupService = groupUseCase; + + foreach (var item in _groupService.GetGroupsWithStudents()) + { + GroupPresenter groupPresenter = new GroupPresenter + { + Id = item.Id, + Name = item.Name, + users = item.Users?.Select(user => new UserPresenter + { + Name = user.Name, + Guid = user.Guid, + Group = new GroupPresenter { Id = item.Id, Name = item.Name } + } + ).ToList() + }; + groupPresentersDataSource.Add(groupPresenter); + } + _groups = new ObservableCollection(groupPresentersDataSource); + + _users = new ObservableCollection(); + + 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); + } } - public string Greeting { get; } = "Welcome to Avalonia!"; } } + diff --git a/Presence.Desktop/Views/MainWindow.axaml b/Presence.Desktop/Views/MainWindow.axaml index 96fe971..e2c94fa 100644 --- a/Presence.Desktop/Views/MainWindow.axaml +++ b/Presence.Desktop/Views/MainWindow.axaml @@ -22,12 +22,26 @@ DockPanel.Dock="Top" Orientation="Horizontal"> - + + + + + + + - + + + + + + + + + diff --git a/data/Repository/IGroupRepository.cs b/data/Repository/IGroupRepository.cs index b430e37..275a3d0 100644 --- a/data/Repository/IGroupRepository.cs +++ b/data/Repository/IGroupRepository.cs @@ -10,6 +10,7 @@ namespace data.Repository public interface IGroupRepository { public IEnumerable getAllGroup(); + public Task> getAllGroupAsync(); public bool addGroup(GroupDAO group); public bool addGroupWithStudents(GroupDAO groupDAO, IEnumerable userDAOs); diff --git a/data/Repository/SQLGroupRepository.cs b/data/Repository/SQLGroupRepository.cs index a9eaffe..41bdc6e 100644 --- a/data/Repository/SQLGroupRepository.cs +++ b/data/Repository/SQLGroupRepository.cs @@ -46,5 +46,10 @@ namespace data.Repository { return _dbContext.groups.Include(group => group.Users).ToList(); } + + public async Task> getAllGroupAsync() + { + return await _dbContext.groups.Include(group => group.Users).ToListAsync(); + } } } diff --git a/domain/Entity/GroupEntity.cs b/domain/Entity/GroupEntity.cs index 0751b78..4ace0cf 100644 --- a/domain/Entity/GroupEntity.cs +++ b/domain/Entity/GroupEntity.cs @@ -10,6 +10,6 @@ namespace domain.Entity { public int Id { get; set; } public string Name { get; set; } - public IEnumerable? Users { get; set; } = null; + public List? Users { get; set; } = null; } } diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs index 3a456dc..bed4174 100644 --- a/domain/Service/GroupService.cs +++ b/domain/Service/GroupService.cs @@ -55,5 +55,28 @@ namespace domain.Service }).ToList() }).ToList(); } + + public async Task> GetGroupsWithStudentsAsync() + { + var result = await _groupRepository.getAllGroupAsync(); + + return result.Select( + group => new GroupEntity + { + Id = group.Id, + Name = group.Name, + Users = group.Users.Select( + user => new UserEntity + { + Guid = user.Guid, + Name = user.Name, + Group = new GroupEntity + { + Id = group.Id, + Name = group.Name, + } + }).ToList() + }).ToList(); + } } } diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs index 3c12149..7c624c3 100644 --- a/domain/UseCase/IGroupUseCase.cs +++ b/domain/UseCase/IGroupUseCase.cs @@ -11,6 +11,7 @@ namespace domain.UseCase public interface IGroupUseCase { public IEnumerable GetGroupsWithStudents(); + public Task> GetGroupsWithStudentsAsync(); public void AddGroup(AddGroupRequest addGroupRequest); public void AddGroupWithSutdents(AddGroupWithStudentsRequest addGroupWithStudents); }