Files
chapter-organizer/WebApp/Services/MeetingScheduleStateService.cs
poprhythm ddb743847d Add meeting schedule state management services and related models
This commit introduces several new services and models to manage the meeting schedule state within localStorage. The MeetingScheduleState class is created to track scheduled teams, absent students, time slot counts, extended teams, and excluded students. Additionally, the IMeetingScheduleStateService interface and its implementation, MeetingScheduleStateService, are added to handle loading and saving state data. The MeetingScheduleClipboardService and MeetingScheduleDataService are also introduced to facilitate clipboard operations and data loading from the database, respectively. These enhancements improve the overall functionality and user experience of the meeting scheduling feature.
2026-01-19 23:02:55 -05:00

106 lines
3.8 KiB
C#

using Core.Entities;
using WebApp.Models;
namespace WebApp.Services;
internal record ExcludedStudent(int TeamId, int TimeSlotIndex, int StudentId);
/// <summary>
/// Service for managing meeting schedule state persistence in localStorage.
/// </summary>
public class MeetingScheduleStateService : IMeetingScheduleStateService
{
private readonly LocalStorageService _localStorage;
private const string ScheduledTeamsKey = "MeetingSchedule_ScheduledTeams";
private const string AbsentStudentsKey = "MeetingSchedule_AbsentStudents";
private const string TimeSlotCountKey = "MeetingSchedule_TimeSlotCount";
private const string ExtendedTeamsKey = "MeetingSchedule_ExtendedTeams";
private const string ExcludedStudentsKey = "MeetingSchedule_ExcludedStudents";
public MeetingScheduleStateService(LocalStorageService localStorage)
{
_localStorage = localStorage;
}
public async Task<IEnumerable<Team>> LoadScheduledTeamsAsync(Team[] allTeams)
{
var teamIds = await _localStorage.GetIntArrayAsync(ScheduledTeamsKey);
if (teamIds.Length > 0)
{
return allTeams.Where(t => teamIds.Contains(t.Id)).ToArray();
}
return [];
}
public async Task SaveScheduledTeamsAsync(IEnumerable<Team> scheduledTeams)
{
var teamIds = scheduledTeams.Select(t => t.Id).ToArray();
await _localStorage.SetIntArrayAsync(ScheduledTeamsKey, teamIds);
}
public async Task<IEnumerable<Student>> LoadAbsentStudentsAsync(Student[] allStudents)
{
var studentIds = await _localStorage.GetIntArrayAsync(AbsentStudentsKey);
if (studentIds.Length > 0)
{
return allStudents.Where(s => studentIds.Contains(s.Id)).ToArray();
}
return [];
}
public async Task SaveAbsentStudentsAsync(IEnumerable<Student> absentStudents)
{
var studentIds = absentStudents.Select(s => s.Id).ToArray();
await _localStorage.SetIntArrayAsync(AbsentStudentsKey, studentIds);
}
public async Task<int> LoadTimeSlotCountAsync(int defaultValue = 2)
{
var timeSlots = await _localStorage.GetIntAsync(TimeSlotCountKey, defaultValue);
return timeSlots > 0 ? timeSlots : defaultValue;
}
public async Task SaveTimeSlotCountAsync(int timeSlotCount)
{
await _localStorage.SetIntAsync(TimeSlotCountKey, timeSlotCount);
}
public async Task<IEnumerable<Team>> LoadExtendedTeamsAsync(Team[] allTeams)
{
var teamIds = await _localStorage.GetIntArrayAsync(ExtendedTeamsKey);
if (teamIds.Length > 0)
{
return allTeams.Where(t => teamIds.Contains(t.Id)).ToArray();
}
return [];
}
public async Task SaveExtendedTeamsAsync(IEnumerable<Team> extendedTeams)
{
var teamIds = extendedTeams.Select(t => t.Id).ToArray();
await _localStorage.SetIntArrayAsync(ExtendedTeamsKey, teamIds);
}
public async Task<Dictionary<(int teamId, int timeSlotIndex, int studentId), bool>> LoadExcludedStudentsAsync()
{
var exclusions = await _localStorage.GetJsonAsync<ExcludedStudent[]>(ExcludedStudentsKey);
if (exclusions != null && exclusions.Length > 0)
{
return exclusions.ToDictionary(
e => (e.TeamId, e.TimeSlotIndex, e.StudentId),
_ => true);
}
return new Dictionary<(int teamId, int timeSlotIndex, int studentId), bool>();
}
public async Task SaveExcludedStudentsAsync(Dictionary<(int teamId, int timeSlotIndex, int studentId), bool> excludedStudents)
{
var exclusions = excludedStudents.Keys
.Where(k => excludedStudents[k])
.Select(k => new ExcludedStudent(k.teamId, k.timeSlotIndex, k.studentId))
.ToArray();
await _localStorage.SetJsonAsync(ExcludedStudentsKey, exclusions);
}
}