presence_new/data/Repository/PresenceRepositoryImpl.cs
2024-11-18 18:54:36 +03:00

125 lines
4.3 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using presence.data.LocalData;
using presence.data.LocalData.Entity;
using presence.data.RemoteData.RemoteDataBase;
using presence.data.RemoteData.RemoteDataBase.DAO;
namespace presence.data.Repository
{
public class SQLPresenceRepositoryImpl: IPresenceRepository
{
private readonly RemoteDataBaseContext _remoteDatabaseContext;
public SQLPresenceRepositoryImpl(RemoteDataBaseContext remoteDatabaseContext)
{
_remoteDatabaseContext = remoteDatabaseContext;
}
public bool AddPresence(PresenceDao presence)
{
var presenceDao = new PresenceDao
{
Date = presence.Date,
ClassNumber = presence.ClassNumber,
UserId = presence.UserId,
User = presence.User
};
_remoteDatabaseContext.Presences.Add(presenceDao);
_remoteDatabaseContext.SaveChanges();
return true;
}
public List<PresenceDao> GetPresence(int GroupId, DateOnly startData, DateOnly endData, int UserId)
{
var presenceList = _remoteDatabaseContext.Presences.Where(presence => presence.GroupId == GroupId
&& presence.UserId == UserId && presence.Date >= startData && presence.Date <= endData).ToList();
return presenceList;
}
public List<PresenceDao> GetPresenceByGroup(int groupId)
{
var listPresences = _remoteDatabaseContext.Presences
.Where(x => x.GroupId == groupId).ToList();
return listPresences;
}
public List<PresenceDao> GetPresenceByGroupAndDate(int groupId, DateOnly date)
{
var listPresences = _remoteDatabaseContext.Presences
.Where(x => x.GroupId == groupId && x.Date == date).ToList();
return listPresences;
}
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;
}
public bool DeletePresenceByGroup(int groupId)
{
var presenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.GroupId == groupId).ToList();
if (presenceToDelete.Count > 0)
{
foreach (var presence in presenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
public bool DeletePresenceByUser(int userId)
{
var presenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.UserId == userId).ToList();
if (presenceToDelete.Count > 0)
{
foreach (var presence in presenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
public bool DeletePresenceByDate(DateOnly startData, DateOnly endData)
{
var PresenceToDelete = _remoteDatabaseContext.Presences.Where(x => x.Date == startData).ToList();
for (var i = startData; i < endData; i = i.AddDays(1))
{
PresenceToDelete.AddRange(_remoteDatabaseContext.Presences.Where(x => x.Date == i).ToList());
}
if (PresenceToDelete.Count > 0)
{
foreach (var presence in PresenceToDelete)
{
_remoteDatabaseContext.Presences.Remove(presence);
}
_remoteDatabaseContext.SaveChanges();
return true;
}
return false;
}
}
}