slarny4/Demo1/Data/Repository/SQLPresenceRepository.cs
2024-12-02 13:24:02 +03:00

113 lines
3.8 KiB
C#

using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Domain.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo.Data.Repository
{
public class SQLPresenceRepository : IPresenceRepository
{
private readonly RemoteDatabaseContext _context;
public SQLPresenceRepository(RemoteDatabaseContext context)
{
_context = context;
}
public void AddPresence(Presence presence)
{
presence.Date = DateTime.SpecifyKind(presence.Date, DateTimeKind.Utc);
_context.Presence.Add(new Data.RemoteData.RemoteDataBase.DAO.Presence
{
Id = presence.Id,
Date = presence.Date,
LessonNumber = presence.LessonNumber,
IsAttendance = presence.IsAttendance,
UserId = presence.UserId
});
_context.SaveChanges();
}
public IEnumerable<Presence> GetPresenceByGroup(int groupId)
{
return _context.Presence
.Where(p => _context.Users.Any(u => u.Id == p.UserId && u.GroupID == groupId))
.Select(p => new Presence
{
Id = p.Id,
Date = p.Date,
LessonNumber = p.LessonNumber,
IsAttendance = p.IsAttendance,
UserId = p.UserId
})
.ToList();
}
public IEnumerable<Presence> GetPresenceByGroupAndDate(int groupId, DateTime date)
{
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
return _context.Presence
.Where(p => _context.Users.Any(u => u.Id == p.UserId && u.GroupID == groupId) && p.Date == date)
.Select(p => new Presence
{
Id = p.Id,
Date = p.Date,
LessonNumber = p.LessonNumber,
IsAttendance = p.IsAttendance,
UserId = p.UserId
})
.ToList();
}
public void MarkUserAsAbsent(Guid userId, int lessonNumber, DateTime date)
{
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
var presence = _context.Presence
.FirstOrDefault(p => p.UserId == userId && p.LessonNumber == lessonNumber && p.Date == date);
if (presence != null)
{
presence.IsAttendance = false;
_context.SaveChanges();
}
}
public IEnumerable<Presence> GetAllPresence()
{
return _context.Presence
.Select(p => new Presence
{
Id = p.Id,
Date = p.Date,
LessonNumber = p.LessonNumber,
IsAttendance = p.IsAttendance,
UserId = p.UserId
})
.ToList();
}
public void DeletePresence(Guid id)
{
var presence = _context.Presence.Find(id);
if (presence != null)
{
_context.Presence.Remove(presence);
_context.SaveChanges();
}
}
public void UpdatePresence(Presence presence)
{
var existingPresence = _context.Presence.Find(presence.Id);
if (existingPresence != null)
{
existingPresence.Date = presence.Date;
existingPresence.LessonNumber = presence.LessonNumber;
existingPresence.IsAttendance = presence.IsAttendance;
existingPresence.UserId = presence.UserId;
_context.SaveChanges();
}
}
}
}