add-vm-logic
This commit is contained in:
parent
b8e7249ec6
commit
4617716a9f
@ -16,18 +16,19 @@ namespace Presence.Api.Controllers
|
||||
_groupService = groupService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<GroupResponse> GetAllGroups()
|
||||
[HttpGet("/group")]
|
||||
public async Task<ActionResult<GroupResponse>> GetAllGroups()
|
||||
{
|
||||
var result = _groupService
|
||||
.GetGroupsWithStudents()
|
||||
.GetGroupsWithStudents();
|
||||
var response = result
|
||||
.Select(group => new GroupResponse {
|
||||
Id = group.Id,
|
||||
Name = group.Name,
|
||||
Users = group.Users.Select(user => new UserResponse {
|
||||
Guid = user.Guid,
|
||||
Name = user.Name
|
||||
}).Take(10).ToList(),
|
||||
}).ToList(),
|
||||
}).ToList();
|
||||
return Ok(new GroupResponse());
|
||||
}
|
||||
|
17
Presence.Desktop/Models/GroupPresenter.cs
Normal file
17
Presence.Desktop/Models/GroupPresenter.cs
Normal 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;
|
||||
}
|
||||
}
|
15
Presence.Desktop/Models/UserPresenter.cs
Normal file
15
Presence.Desktop/Models/UserPresenter.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
<AvaloniaResource Include="Assets\**" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,15 +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 groupService)
|
||||
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>();
|
||||
private ObservableCollection<GroupPresenter> _groups;
|
||||
public ObservableCollection<GroupPresenter> Groups => _groups;
|
||||
|
||||
public GroupPresenter? SelectedGroupItem
|
||||
{
|
||||
_groupService = groupService;
|
||||
get => _selectedGroupItem;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
||||
}
|
||||
|
||||
public string Greeting { get; } = "Welcome to Avalonia!";
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,12 +9,40 @@
|
||||
Icon="/Assets/avalonia-logo.ico"
|
||||
Title="Presence.Desktop">
|
||||
|
||||
<Design.DataContext>
|
||||
<!-- This only sets the DataContext for the previewer in an IDE,
|
||||
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
|
||||
<vm:MainWindowViewModel/>
|
||||
</Design.DataContext>
|
||||
|
||||
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
||||
<Design.DataContext>
|
||||
<vm:MainWindowViewModel/>
|
||||
</Design.DataContext>
|
||||
<DockPanel Background="Azure">
|
||||
<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>
|
||||
<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>
|
||||
|
@ -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 group, IEnumerable<UserDAO> userDAOs);
|
||||
}
|
||||
|
@ -59,5 +59,10 @@ namespace data.Repository
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GroupDAO>> getAllGroupAsync()
|
||||
{
|
||||
return await dbContext.groups.Include(group => group.Users).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ 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;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -52,5 +52,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudents);
|
||||
}
|
||||
|
@ -7,7 +7,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "console_ui", "console_ui\co
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "data", "data\data.csproj", "{28FE5F1E-5D2B-4750-AE24-A15BB8A5972D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "domain", "domain\domain.csproj", "{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "domain", "domain\domain.csproj", "{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Presence.Api", "Presence.Api\Presence.Api.csproj", "{0493354A-C0C0-49FC-A143-28F2268309E2}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63} = {78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Presence.Desktop", "Presence.Desktop\Presence.Desktop.csproj", "{D646BC7E-8025-4985-AF9B-5E4474AD7F66}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -27,6 +34,14 @@ Global
|
||||
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{78C8AC9B-2B4D-4C71-8469-C6DAE16C9A63}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0493354A-C0C0-49FC-A143-28F2268309E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0493354A-C0C0-49FC-A143-28F2268309E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0493354A-C0C0-49FC-A143-28F2268309E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0493354A-C0C0-49FC-A143-28F2268309E2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D646BC7E-8025-4985-AF9B-5E4474AD7F66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D646BC7E-8025-4985-AF9B-5E4474AD7F66}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D646BC7E-8025-4985-AF9B-5E4474AD7F66}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D646BC7E-8025-4985-AF9B-5E4474AD7F66}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
Reference in New Issue
Block a user