Mega_New_Presence/presence_new/Data/Repository/SQLPresenceRepositoryImpl.cs

88 lines
3.4 KiB
C#
Raw Normal View History

2024-11-01 07:40:53 +00:00
using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using Demo.domain.Models;
namespace Demo.Data.Repository
{
public class SQLPresenceRepositoryImpl : IPresenceRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext;
public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public void SavePresence(List<PresenceLocalEntity> presences)
{
_remoteDatabaseContext.PresenceDaos.AddRange(presences.Select(it => new PresenceDao {
Date = DateOnly.FromDateTime(it.Date),
IsAttedance = it.IsAttedance,
LessonNumber = it.LessonNumber,
UserGuid = it.UserGuid }));
_remoteDatabaseContext.SaveChanges();
}
public void AddPresence(PresenceLocalEntity presence)
{
if (presence == null) throw new ArgumentNullException(nameof(presence));
var newPresence = new PresenceDao
{
Date = DateOnly.FromDateTime(presence.Date),
UserGuid = presence.UserGuid,
LessonNumber = presence.LessonNumber,
IsAttedance = presence.IsAttedance
};
_remoteDatabaseContext.PresenceDaos.Add(newPresence);
}
public List<PresenceLocalEntity> GetPresenceByGroup(int groupId)
{
return _remoteDatabaseContext.PresenceDaos
.Where(p => p.UserDao != null && p.UserDao.GroupID == groupId) // Проверяем на null перед использованием
.Select(p => new PresenceLocalEntity
{
Date = p.Date.ToDateTime(TimeOnly.MinValue),
UserGuid = p.UserGuid,
LessonNumber = p.LessonNumber,
IsAttedance = p.IsAttedance
})
.ToList();
}
public List<PresenceLocalEntity> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
return _remoteDatabaseContext.PresenceDaos
.Where(p => p.UserDao != null && p.UserDao.GroupID == groupId && p.Date == DateOnly.FromDateTime(date))
.Select(p => new PresenceLocalEntity
{
Date = p.Date.ToDateTime(TimeOnly.MinValue),
UserGuid = p.UserGuid,
LessonNumber = p.LessonNumber,
IsAttedance = p.IsAttedance
})
.ToList();
}
public void MarkUserAsAbsent(Guid userGuid, int groupId, int firstLessonNumber, int lastLessonNumber)
{
foreach (var lesson in Enumerable.Range(firstLessonNumber, lastLessonNumber - firstLessonNumber + 1))
{
var presence = _remoteDatabaseContext.PresenceDaos.FirstOrDefault(p =>
p.UserGuid == userGuid &&
p.UserDao != null && p.UserDao.GroupID == groupId &&
p.LessonNumber == lesson);
if (presence != null)
{
presence.IsAttedance = false; // Помечаем как отсутствующего
}
}
}
}
}