feat: Teilnehmerlisten in Lerngruppen importieren
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Platform.Storage;
|
||||
using LehrerApp.Core.Importing;
|
||||
using LehrerApp.Core.Interfaces;
|
||||
using LehrerApp.Core.Models;
|
||||
using LehrerApp.Core.Services;
|
||||
using LehrerApp.Desktop.ViewModels.Groups;
|
||||
using LehrerApp.Desktop.ViewModels.Students;
|
||||
using LehrerApp.Desktop.Services;
|
||||
using LehrerApp.Desktop.Views.Shared;
|
||||
using LehrerApp.Desktop.Views.Students;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace LehrerApp.Desktop.Views.Groups;
|
||||
|
||||
public partial class GroupDetailView : UserControl
|
||||
{
|
||||
private const int MaximumImportFileSize = 20 * 1024 * 1024;
|
||||
|
||||
public GroupDetailView() => InitializeComponent();
|
||||
|
||||
protected override void OnDataContextChanged(EventArgs e)
|
||||
@@ -60,6 +68,61 @@ public partial class GroupDetailView : UserControl
|
||||
return await dialog.ShowDialog<bool>(owner);
|
||||
}
|
||||
|
||||
private async void OnImportParticipantsClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var owner = TopLevel.GetTopLevel(this) as Window;
|
||||
if (owner is null || DataContext is not GroupDetailViewModel vm || vm.Group is null) return;
|
||||
|
||||
var service = App.Services.GetRequiredService<StudentImportService>();
|
||||
var patterns = service.SupportedExtensions.Select(extension => $"*{extension}").ToArray();
|
||||
var files = await owner.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
||||
{
|
||||
Title = $"Teilnehmer für {vm.Group.Name} importieren",
|
||||
AllowMultiple = false,
|
||||
FileTypeFilter =
|
||||
[
|
||||
new FilePickerFileType("Unterstützte Schülerlisten")
|
||||
{
|
||||
Patterns = patterns.Length > 0 ? patterns : ["*.csv"],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (files.Count == 0) return;
|
||||
|
||||
try
|
||||
{
|
||||
await using var source = await files[0].OpenReadAsync();
|
||||
if (source.CanSeek && source.Length > MaximumImportFileSize)
|
||||
throw new InvalidDataException("Die Importdatei ist größer als 20 MB.");
|
||||
|
||||
using var buffer = new MemoryStream();
|
||||
await source.CopyToAsync(buffer);
|
||||
if (buffer.Length > MaximumImportFileSize)
|
||||
throw new InvalidDataException("Die Importdatei ist größer als 20 MB.");
|
||||
|
||||
var importFile = new ImportFile(files[0].Name, buffer.ToArray());
|
||||
var preview = await Task.Run(async () =>
|
||||
await service.AnalyzeAsync(importFile, vm.Group.Id).ConfigureAwait(false));
|
||||
var dialogVm = new StudentImportDialogViewModel(service, preview, files[0].Name);
|
||||
var dialog = new StudentImportDialog { DataContext = dialogVm };
|
||||
if (!await dialog.ShowDialog<bool>(owner)) return;
|
||||
|
||||
vm.LoadStudents();
|
||||
vm.ParticipationTab.RefreshCurrentGrid();
|
||||
var result = dialogVm.Result!;
|
||||
App.Services.GetRequiredService<NotificationService>().ShowSuccess(
|
||||
$"Teilnehmerimport abgeschlossen: {result.CreatedStudents} neu, "
|
||||
+ $"{result.UpdatedStudents} ergänzt, {result.CreatedMemberships} zugeordnet.");
|
||||
}
|
||||
catch (Exception ex) when (ex is ImportFormatException
|
||||
or InvalidDataException
|
||||
or IOException
|
||||
or UnauthorizedAccessException)
|
||||
{
|
||||
App.Services.GetRequiredService<NotificationService>().ShowError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> ShowWithdrawStudentDialog(StudentSummary student)
|
||||
{
|
||||
if (DataContext is not GroupDetailViewModel vm || vm.Group is null) return false;
|
||||
|
||||
Reference in New Issue
Block a user