93 lines
3.6 KiB
C#
93 lines
3.6 KiB
C#
using Demo.Data.LocalData;
|
|
using Demo.Data.RemoteData.RemoteDataBase;
|
|
using Demo.Data.RemoteData.RemoteDataBase.DAO;
|
|
using Demo.Domain.Models;
|
|
|
|
namespace Demo.Data.Repository
|
|
{
|
|
public class SQLPresenceRepositoryImpl : IPresenceRepository
|
|
{
|
|
private readonly RemoteDatabaseContext _remoteDatabaseContext;
|
|
|
|
public SQLPresenceRepositoryImpl(RemoteDatabaseContext remoteDatabaseContext){
|
|
_remoteDatabaseContext = remoteDatabaseContext;
|
|
GetAllPresence = _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList();
|
|
}
|
|
public List<PresenceLocalEntity> GetAllPresence = new List<PresenceLocalEntity>{};
|
|
|
|
public List<PresenceLocalEntity> GetAllPresences(){
|
|
return _remoteDatabaseContext.PresenceDaos.Select(x => new PresenceLocalEntity{UserGuid = x.UserGuid, Date = x.Date, LessonNumber = x.LessonNumber, IsAttedance = x.IsAttedance}).ToList();
|
|
}
|
|
|
|
public List<PresenceLocalEntity> GeneratePresence(List<PresenceLocalEntity> presenceLocalEntities)
|
|
{
|
|
var presences = presenceLocalEntities.Select(x => new PresenceDAO
|
|
{
|
|
UserGuid = x.UserGuid,
|
|
IsAttedance = x.IsAttedance,
|
|
LessonNumber = x.LessonNumber,
|
|
Date = x.Date,
|
|
userDAO = _remoteDatabaseContext.Users.FirstOrDefault(u => u.Guid == x.UserGuid)
|
|
}).ToList();
|
|
|
|
_remoteDatabaseContext.PresenceDaos.AddRange(presences);
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return presenceLocalEntities;
|
|
}
|
|
|
|
public bool DeletePresence(){
|
|
var allRecords = _remoteDatabaseContext.PresenceDaos.ToList();
|
|
_remoteDatabaseContext.PresenceDaos.RemoveRange(allRecords);
|
|
_remoteDatabaseContext.SaveChanges();
|
|
return true;
|
|
}
|
|
|
|
|
|
public bool DeletePresenceByUserGuid(Guid UserGuid) {
|
|
var delRecords = _remoteDatabaseContext.PresenceDaos
|
|
.Where(x => x.UserGuid == UserGuid).ToList();
|
|
|
|
_remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords);
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool DeletePresenceByGroup(int groupID) {
|
|
var delRecords = _remoteDatabaseContext.PresenceDaos
|
|
.Where(y => y.userDAO.GroupID == groupID);
|
|
|
|
_remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords);
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool DeletePresenceByDateRange(DateOnly startDate, DateOnly endDate) {
|
|
var delRecords = _remoteDatabaseContext.PresenceDaos
|
|
.Where(x => x.Date >= startDate && x.Date <= endDate);
|
|
|
|
_remoteDatabaseContext.PresenceDaos.RemoveRange(delRecords);
|
|
_remoteDatabaseContext.SaveChanges();
|
|
|
|
return true;
|
|
}
|
|
|
|
public void IsAttedance(int firstLesson, int lastLesson, DateOnly date, Guid UserGuid){
|
|
var presencesToUpdate = _remoteDatabaseContext.PresenceDaos
|
|
.Where(x => x.LessonNumber >= firstLesson
|
|
&& x.LessonNumber <= lastLesson
|
|
&& x.Date == date
|
|
&& x.UserGuid == UserGuid)
|
|
.ToList();
|
|
|
|
foreach(var presence in presencesToUpdate){
|
|
presence.IsAttedance = false;
|
|
}
|
|
|
|
_remoteDatabaseContext.SaveChanges();
|
|
}
|
|
|
|
}
|
|
} |