2024-12-23 09:12:26 +00:00
using System ;
2024-12-19 17:36:57 +00:00
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
2024-12-23 09:12:26 +00:00
using Microsoft.EntityFrameworkCore ;
using presence.data.LocalData ;
using presence.data.LocalData.Entity ;
using presence.data.RemoteData.RemoteDataBase ;
using presence.data.RemoteData.RemoteDataBase.DAO ;
2024-12-19 17:36:57 +00:00
2024-12-23 09:12:26 +00:00
namespace presence.data.Repository
2024-12-19 17:36:57 +00:00
{
public class SQLPresenceRepositoryImpl : IPresenceRepository
{
private readonly RemoteDatabaseContext _remoteDatabaseContext ;
public SQLPresenceRepositoryImpl ( RemoteDatabaseContext remoteDatabaseContext )
{
_remoteDatabaseContext = remoteDatabaseContext ;
}
2024-12-23 09:12:26 +00:00
public bool AddPresence ( PresenceDao presence ) //Метод добавления посещения
2024-12-19 17:36:57 +00:00
{
var presenceDao = new PresenceDao
{
Date = presence . Date ,
ClassNumber = presence . ClassNumber ,
UserId = presence . UserId ,
GroupId = presence . GroupId
} ;
2024-12-23 09:12:26 +00:00
_remoteDatabaseContext . Presences . Add ( presenceDao ) ;
2024-12-19 17:36:57 +00:00
_remoteDatabaseContext . SaveChanges ( ) ;
return true ;
}
2024-12-23 09:12:26 +00:00
public List < PresenceDao > GetPresence ( int GroupId , DateOnly startData , DateOnly endData , int UserId ) // Метод для получения присутствий по группе и диапазону дат
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var presenceList = _remoteDatabaseContext . Presences . Where ( presence = > presence . GroupId = = GroupId
2024-12-19 17:36:57 +00:00
& & presence . UserId = = UserId & & presence . Date > = startData & & presence . Date < = endData ) . ToList ( ) ;
return presenceList ;
}
2024-12-23 09:12:26 +00:00
public List < PresenceDao > GetPresenceByGroup ( int groupId ) // Метод для получения всех присутствий по ID группы
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var listPresences = _remoteDatabaseContext . Presences
2024-12-19 17:36:57 +00:00
. Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
return listPresences ;
}
2024-12-23 09:12:26 +00:00
public List < PresenceDao > GetPresenceByGroupAndDate ( int groupId , DateOnly date ) // Метод для получения присутствий по группе и дате
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var listPresences = _remoteDatabaseContext . Presences
2024-12-19 17:36:57 +00:00
. Where ( x = > x . GroupId = = groupId & & x . Date = = date ) . ToList ( ) ;
return listPresences ;
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByGroup ( int groupId ) // Метод для удаления всех присутствий по ID группы
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var presenceToDelete = _remoteDatabaseContext . Presences . Where ( x = > x . GroupId = = groupId ) . ToList ( ) ;
2024-12-19 17:36:57 +00:00
if ( presenceToDelete . Count > 0 )
{
foreach ( var presence in presenceToDelete )
{
2024-12-23 09:12:26 +00:00
_remoteDatabaseContext . Presences . Remove ( presence ) ;
2024-12-19 17:36:57 +00:00
}
_remoteDatabaseContext . SaveChanges ( ) ;
return true ;
}
return false ;
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByUser ( int userId ) // Метод для удаления присутствий по ID пользователя
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var presenceToDelete = _remoteDatabaseContext . Presences . Where ( x = > x . UserId = = userId ) . ToList ( ) ;
2024-12-19 17:36:57 +00:00
if ( presenceToDelete . Count > 0 )
{
foreach ( var presence in presenceToDelete )
{
2024-12-23 09:12:26 +00:00
_remoteDatabaseContext . Presences . Remove ( presence ) ;
2024-12-19 17:36:57 +00:00
}
_remoteDatabaseContext . SaveChanges ( ) ;
return true ;
}
return false ;
}
2024-12-23 09:12:26 +00:00
public bool DeletePresenceByDate ( DateOnly startData , DateOnly endData ) // Метод для удаления присутствий в заданном диапазоне дат
2024-12-19 17:36:57 +00:00
{
2024-12-23 09:12:26 +00:00
var PresenceToDelete = _remoteDatabaseContext . Presences . Where ( x = > x . Date = = startData ) . ToList ( ) ;
2024-12-19 17:36:57 +00:00
for ( var i = startData ; i < endData ; i = i . AddDays ( 1 ) )
{
2024-12-23 09:12:26 +00:00
PresenceToDelete . AddRange ( _remoteDatabaseContext . Presences . Where ( x = > x . Date = = i ) . ToList ( ) ) ;
2024-12-19 17:36:57 +00:00
}
if ( PresenceToDelete . Count > 0 )
{
foreach ( var presence in PresenceToDelete )
{
2024-12-23 09:12:26 +00:00
_remoteDatabaseContext . Presences . Remove ( presence ) ;
2024-12-19 17:36:57 +00:00
}
_remoteDatabaseContext . SaveChanges ( ) ;
return true ;
}
return false ;
}
2024-12-23 09:12:26 +00:00
public bool UncheckAttendence ( int firstClass , int lastClass , DateOnly date , int userId ) // Метод для отмены регистрации посещаемости в заданном диапазоне классов
{
var presToUpdate = _remoteDatabaseContext . Presences
. Where ( x = > x . UserId = = userId & & x . ClassNumber > = firstClass
& & x . ClassNumber < = lastClass & & x . Date = = date ) . ToList ( ) ;
foreach ( var pres in presToUpdate )
{
pres . IsAttendence = false ;
}
_remoteDatabaseContext . SaveChanges ( ) ;
return true ;
}
2024-12-19 17:36:57 +00:00
}
2024-12-23 09:12:26 +00:00
}