slarny4/Demo1/Data/Repository/PresenceRepositoryImpl.cs

70 lines
2.4 KiB
C#
Raw Normal View History

2024-10-24 08:50:32 +00:00
using AttendanceApp.Data.Exceptions;
using AttendanceApp.Data.LocalData;
using AttendanceApp.Domain.Models;
using System.Collections.Generic;
using System.Linq;
namespace AttendanceApp.Data.Repository
{
public class PresenceRepositoryImpl : IPresenceRepository
{
private readonly List<LocalPresence> _presences = LocalStaticData.Presences;
public IEnumerable<Presence> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
var usersInGroup = LocalStaticData.Users.Where(u => u.GroupID == groupId).Select(u => u.Id);
return _presences
.Where(p => usersInGroup.Contains(p.UserId) && p.ClassDate.Date == date.Date)
.Select(p => new Presence
{
ClassDate = p.ClassDate,
ClassNumber = p.ClassNumber,
IsPresent = p.IsPresent,
UserId = p.UserId
});
}
public IEnumerable<Presence> GetPresenceByGroup(int groupId)
{
var usersInGroup = LocalStaticData.Users
.Where(u => u.GroupID == groupId)
.Select(u => u.Id);
return _presences
.Where(p => usersInGroup.Contains(p.UserId))
.Select(p => new Presence
{
ClassDate = p.ClassDate,
ClassNumber = p.ClassNumber,
IsPresent = p.IsPresent,
UserId = p.UserId
})
.OrderBy(p => p.ClassDate)
.ThenBy(p => p.ClassNumber);
}
public void AddPresence(Presence presence)
{
_presences.Add(new LocalPresence
{
ClassDate = presence.ClassDate,
ClassNumber = presence.ClassNumber,
IsPresent = presence.IsPresent,
UserId = presence.UserId
});
}
public void UpdatePresence(Presence presence)
{
var localPresence = _presences.FirstOrDefault(p =>
p.UserId == presence.UserId &&
p.ClassDate.Date == presence.ClassDate.Date &&
p.ClassNumber == presence.ClassNumber);
if (localPresence == null)
throw new RepositoryException("Посещаемость не найдена.");
localPresence.IsPresent = presence.IsPresent;
}
}
}