presence/data/Repository/SQLPresenceRepository.cs

84 lines
2.4 KiB
C#

using data.DAO;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace data.Repository
{
public class SQLPresenceRepository : IPresenceRepository
{
private readonly DatabaseContext _context;
public SQLPresenceRepository(DatabaseContext context)
{
_context = context;
}
public IEnumerable<Diary> GetPresence(
int groupId,
int? subjectId = null,
DateOnly? date = null,
int? studentId = null)
{
var query = _context.Diaries
.Include(p => p.Student)
.Include(p => p.StudentGroupSubject)
.ThenInclude(p => p.Subject)
.Include(p => p.Traffic)
.Where(p => p.Student.GroupId == groupId);
if (subjectId.HasValue)
query = query.Where(p => p.StudentGroupSubject.Subject.Id == subjectId.Value);
if (date.HasValue)
query = query.Where(p => p.Date == date.Value);
if (studentId.HasValue)
query = query.Where(p => p.Student.Id == studentId.Value);
return query.ToList();
}
public void DeleteAllPresence()
{
_context.Diaries.RemoveRange(_context.Diaries);
_context.SaveChanges();
}
public void DeleteGroupPresence(int groupId)
{
var presencesToDelete = _context.Diaries
.Include(p => p.Student)
.Where(p => p.Student.GroupId == groupId);
_context.Diaries.RemoveRange(presencesToDelete);
_context.SaveChanges();
}
public void AddPresenceRecords(List<Diary> items)
{
_context.Diaries.AddRange(items);
_context.SaveChanges();
}
public void UpdatePresenceRecords(List<Diary> items)
{
foreach (var item in items)
{
var existingRecord = _context.Diaries
.FirstOrDefault(p =>
p.Date == item.Date &&
p.NumberSubject == item.NumberSubject &&
p.StudentId == item.StudentId);
if (existingRecord != null)
{
existingRecord.TrafficId = item.TrafficId;
}
}
_context.SaveChanges();
}
}
}