129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Linq;
|
|
|
|
namespace spplus;
|
|
|
|
public static class import
|
|
{
|
|
public static List<Student> ImportStudentsFromFile(string path)
|
|
{
|
|
var dict = new Dictionary<string, (string Name, List<string> Courses)>();
|
|
|
|
foreach (var line in File.ReadLines(path).Skip(1)) // Header überspringen
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
continue;
|
|
|
|
var parts = line.Split(',');
|
|
if (parts.Length < 3)
|
|
continue;
|
|
|
|
string nameWithId = parts[0].Trim();
|
|
string course = parts[2].Replace("(2)", "").Replace("(3)", "").Replace("(4)", "").Trim();
|
|
|
|
int open = nameWithId.LastIndexOf('(');
|
|
int close = nameWithId.LastIndexOf(')');
|
|
if (open < 0 || close < 0 || close <= open)
|
|
continue;
|
|
|
|
string name = nameWithId[..open].Trim();
|
|
string id = nameWithId[(open + 1)..close].Trim();
|
|
|
|
if (!dict.ContainsKey(id))
|
|
dict[id] = (name, new List<string>());
|
|
|
|
dict[id].Courses.Add(course);
|
|
}
|
|
|
|
var result = new List<Student>();
|
|
|
|
foreach (var (id, data) in dict)
|
|
{
|
|
var student = new Student(id, data.Name, data.Courses);
|
|
result.Add(student);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static List<Student> ImportResultFromFile(string path)
|
|
{
|
|
var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (var line in File.ReadLines(path).Skip(1)) // Header überspringen
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
continue;
|
|
|
|
var parts = line.Split(',', StringSplitOptions.None);
|
|
if (parts.Length < 5)
|
|
continue;
|
|
|
|
var studentId = parts[0].Trim();
|
|
dict[studentId] = new[]
|
|
{
|
|
parts[1].Trim(),
|
|
parts[2].Trim(),
|
|
parts[3].Trim(),
|
|
parts[4].Trim()
|
|
};
|
|
}
|
|
|
|
return dict
|
|
.Select(entry => new Student(entry.Key, string.Empty, new List<string>())
|
|
{
|
|
Result = entry.Value
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
private sealed class ResultImportEntry
|
|
{
|
|
public int Semester { get; set; }
|
|
public string SportName { get; set; } = string.Empty;
|
|
public List<string> Students { get; set; } = new();
|
|
}
|
|
|
|
public static List<(int Semester, CourseCrafter.CourseInstance Instance)> ImportResultFromJson(string path)
|
|
{
|
|
try
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
var entries = JsonSerializer.Deserialize<List<ResultImportEntry>>(json);
|
|
if (entries == null) return new();
|
|
|
|
var result = new List<(int Semester, CourseCrafter.CourseInstance Instance)>();
|
|
|
|
foreach (var e in entries)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(e.SportName))
|
|
continue;
|
|
|
|
var sport = Settings.Instance.Sports.FirstOrDefault(s =>
|
|
string.Equals(s.Name, e.SportName, StringComparison.OrdinalIgnoreCase) ||
|
|
s.AlternativeNames.Any(a => string.Equals(a, e.SportName, StringComparison.OrdinalIgnoreCase)));
|
|
|
|
if (sport == null)
|
|
continue;
|
|
|
|
var ci = new CourseCrafter.CourseInstance
|
|
{
|
|
Sport = sport,
|
|
Students = e.Students.Distinct().ToList()
|
|
};
|
|
|
|
result.Add((e.Semester, ci));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch
|
|
{
|
|
return new();
|
|
}
|
|
}
|
|
}
|