semesterWork/Presence.Desktop/ViewModels/PresenceViewModel.cs
2024-12-13 10:34:58 +03:00

127 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
namespace Presence.Desktop.ViewModels;
public class PresenceViewModel : ViewModelBase
{
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;
UpdatePresences();
var filteredPresences = _presences.Where(a => a.PresenceType == SelectedAttendanceType).ToList();
_presences.Clear();
foreach (var attendance in filteredPresences)
{
_presences.Add(attendance);
}
}
}