add logic in vm

This commit is contained in:
adm 2024-11-25 13:47:17 +03:00
parent 7b2b8b08ed
commit 5115ae0e87
11 changed files with 147 additions and 11 deletions

View File

@ -16,10 +16,11 @@ namespace Presence.Api.Controllers
}
[HttpGet("/group")]
public ActionResult<GroupResponse> GetAllGroups() {
public async Task<ActionResult<GroupResponse>> 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);

View File

@ -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<UserPresenter>? users { get; set; } = null;
}
}

View File

@ -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; }
}
}

View File

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

View File

@ -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) {
private 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 ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _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<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);
}
}
public string Greeting { get; } = "Welcome to Avalonia!";
}
}

View File

@ -22,12 +22,26 @@
DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock Text="Combobox ->"/>
<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"/>
<ListBox Background="Bisque" ItemsSource="{Binding Users}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Border>
</DockPanel>
</Window>

View File

@ -10,6 +10,7 @@ namespace data.Repository
public interface IGroupRepository
{
public IEnumerable<GroupDAO> getAllGroup();
public Task<IEnumerable<GroupDAO>> getAllGroupAsync();
public bool addGroup(GroupDAO group);
public bool addGroupWithStudents(GroupDAO groupDAO, IEnumerable<UserDAO> userDAOs);

View File

@ -46,5 +46,10 @@ namespace data.Repository
{
return _dbContext.groups.Include(group => group.Users).ToList();
}
public async Task<IEnumerable<GroupDAO>> getAllGroupAsync()
{
return await _dbContext.groups.Include(group => group.Users).ToListAsync();
}
}
}

View File

@ -10,6 +10,6 @@ namespace domain.Entity
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<UserEntity>? Users { get; set; } = null;
public List<UserEntity>? Users { get; set; } = null;
}
}

View File

@ -55,5 +55,28 @@ namespace domain.Service
}).ToList()
}).ToList();
}
public async Task<IEnumerable<GroupEntity>> 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();
}
}
}

View File

@ -11,6 +11,7 @@ namespace domain.UseCase
public interface IGroupUseCase
{
public IEnumerable<GroupEntity> GetGroupsWithStudents();
public Task<IEnumerable<GroupEntity>> GetGroupsWithStudentsAsync();
public void AddGroup(AddGroupRequest addGroupRequest);
public void AddGroupWithSutdents(AddGroupWithStudentsRequest addGroupWithStudents);
}