semesterWork/Presence.Desktop/ViewModels/PresenceViewModel.cs

126 lines
3.8 KiB
C#
Raw Normal View History

2024-12-13 05:47:52 +00:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using data.Repository;
using Data.DAO;
using domain.Service;
using domain.UseCase;
using Presence.Desktop.Models;
using Presense.Desktop.Models;
using ReactiveUI;
2024-12-08 15:00:30 +00:00
namespace Presence.Desktop.ViewModels;
2024-12-13 05:47:52 +00:00
public class PresenceViewModel : ViewModelBase
2024-12-08 15:00:30 +00:00
{
2024-12-13 05:47:52 +00:00
private readonly PresenceService _presenceService;
private readonly IGroupUseCase _groupService;
private ObservableCollection<PresencePresenter> _presences;
private ObservableCollection<GroupPresenter> _groups;
private GroupPresenter? _selectedGroupItem;
private string? _selectedAttendanceType;
public ObservableCollection<PresencePresenter> Presences
{
get => _presences;
set => this.RaiseAndSetIfChanged(ref _presences, value);
}
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem
{
get => _selectedGroupItem;
set
{
if (_selectedGroupItem != value)
{
_selectedGroupItem = value;
this.RaisePropertyChanged();
UpdatePresences();
}
}
}
public string[] AttendanceTypes { get; } = { "Был", "Не был", "Болеет" };
public string? SelectedAttendanceType
{
get => _selectedAttendanceType;
set
{
this.RaiseAndSetIfChanged(ref _selectedAttendanceType, value);
FilterPresences();
}
}
public PresenceViewModel(IGroupUseCase groupService, PresenceService presenceService)
{
_groupService = groupService;
_presenceService = presenceService;
_presences = new ObservableCollection<PresencePresenter>();
_groups = new ObservableCollection<GroupPresenter>();
LoadGroups();
}
private void LoadGroups()
{
var groups = _groupService.GetGroupsWithStudents(); // Получаем все группы
_groups.Clear();
foreach (var group in groups)
{
_groups.Add(new GroupPresenter { GroupId = group.GroupId, GroupName = group.GroupName });
}
// Выбираем первую группу по умолчанию
if (_groups.Any())
{
SelectedGroupItem = _groups.First();
}
}
private void UpdatePresences()
{
if (SelectedGroupItem == null) return;
var PresencesListHere = _presenceService.GetAttendancesRelativeWithGroup(SelectedGroupItem.GroupId);
ObservableCollection<PresencePresenter> convertedList = new ObservableCollection<PresencePresenter>();
_presences.Clear();
foreach (var attendance in PresencesListHere)
{
convertedList.Add(convertToPresencePresenter(attendance));
}
Presences = convertedList;
}
private PresencePresenter convertToPresencePresenter(Attendance attendance)
{
return new PresencePresenter()
{
AttendanceId = attendance.AttendanceId,
StudentFullName = $"{attendance.Student.FirstName} {attendance.Student.LastName} {attendance.Student.Patronymic}",
Subject = attendance.GroupSubject.Subject.SubjectName,
Date = attendance.Date,
GroupName = attendance.Group.GroupName,
LessonNumber = attendance.LessonNumber,
PresenceType = attendance.Visit.VisitName
};
}
private void FilterPresences()
{
if (string.IsNullOrEmpty(SelectedAttendanceType)) return;
var filteredPresences = _presences.Where(a => a.PresenceType == SelectedAttendanceType).ToList();
_presences.Clear();
foreach (var attendance in filteredPresences)
{
_presences.Add(attendance);
}
}
2024-12-08 15:00:30 +00:00
}