2024-12-12 07:21:45 +00:00
|
|
|
|
using Data.DAO;
|
2024-12-04 21:58:43 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-16 21:45:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace data.Repository
|
|
|
|
|
{
|
|
|
|
|
public class SQLGroupRepository : IGroupRepository
|
2024-12-12 07:21:45 +00:00
|
|
|
|
|
2024-11-16 21:45:57 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
private readonly RemoteDatabaseContext _dbContext;
|
|
|
|
|
public SQLGroupRepository(RemoteDatabaseContext remoteDatabaseContext) { _dbContext = remoteDatabaseContext; }
|
|
|
|
|
|
|
|
|
|
public bool AddGroup(Group group)
|
2024-11-16 21:45:57 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
var maxGroupId = _dbContext.Groups.Max(g => (int?)g.GroupId) ?? 0;
|
|
|
|
|
|
|
|
|
|
group.GroupId = maxGroupId + 1;
|
|
|
|
|
|
|
|
|
|
_dbContext.Groups.Add(group);
|
|
|
|
|
return _dbContext.SaveChanges() > 0;
|
2024-11-16 21:45:57 +00:00
|
|
|
|
}
|
2024-12-12 07:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool addGroupWithStudent(Group groups, IEnumerable<Student> students)
|
2024-11-16 21:45:57 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
using var transaction = _dbContext.Database.BeginTransaction();
|
2024-11-16 21:45:57 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
_dbContext.Groups.Add(groups);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
_dbContext.Groups.Add(groups);
|
|
|
|
|
foreach (var item in students)
|
|
|
|
|
{
|
|
|
|
|
item.Group = groups;
|
|
|
|
|
_dbContext.Students.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return true;
|
2024-11-16 21:45:57 +00:00
|
|
|
|
}
|
2024-12-12 07:21:45 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2024-11-16 21:45:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-12 07:21:45 +00:00
|
|
|
|
public void AddStudentsToGroup(List<Student> students)
|
2024-11-16 21:45:57 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
var maxStudentId = _dbContext.Students.Max(s => (int?)s.StudentId) ?? 0;
|
|
|
|
|
|
|
|
|
|
foreach (var student in students)
|
|
|
|
|
{
|
|
|
|
|
student.StudentId = ++maxStudentId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dbContext.Students.AddRange(students);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool DeleteGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
using var transaction = _dbContext.Database.BeginTransaction();
|
2024-11-16 21:56:50 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
var group = _dbContext.Groups
|
|
|
|
|
.Include(g => g.Students)
|
|
|
|
|
.FirstOrDefault(g => g.GroupId == groupId);
|
|
|
|
|
|
|
|
|
|
if (group != null)
|
|
|
|
|
{
|
|
|
|
|
_dbContext.Students.RemoveRange(group.Students);
|
|
|
|
|
_dbContext.Groups.Remove(group);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2024-11-16 21:56:50 +00:00
|
|
|
|
}
|
2024-12-12 07:21:45 +00:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2024-11-16 21:56:50 +00:00
|
|
|
|
}
|
2024-11-16 21:45:57 +00:00
|
|
|
|
}
|
2024-11-18 16:36:42 +00:00
|
|
|
|
|
2024-12-12 07:21:45 +00:00
|
|
|
|
public IEnumerable<Group> GetAllGroup()
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Groups
|
|
|
|
|
.Include(group => group.Students)
|
|
|
|
|
.Include(group => group.GroupSubjects)
|
|
|
|
|
.ThenInclude(gs => gs.Subject)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Group GetGroupById(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Groups
|
|
|
|
|
.Include(group => group.Students)
|
|
|
|
|
.Include(group => group.Subjects)
|
|
|
|
|
.FirstOrDefault(group => group.GroupId == groupId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Group GetGroupWithSubjects(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Groups
|
|
|
|
|
.Include(group => group.Subjects)
|
|
|
|
|
.FirstOrDefault(group => group.GroupId == groupId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddSubjectsToGroup(List<GroupSubject> groupSubjects)
|
2024-11-18 16:36:42 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
using var transaction = _dbContext.Database.BeginTransaction();
|
2024-11-18 16:36:42 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
foreach (var groupSubject in groupSubjects)
|
2024-11-18 16:36:42 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
var group = _dbContext.Groups.Include(g => g.Subjects).FirstOrDefault(g => g.GroupId == groupSubject.GroupId);
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"Group with ID {groupSubject.GroupId} not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var subject = _dbContext.Subjects.FirstOrDefault(s => s.SubjectId == groupSubject.SubjectId);
|
|
|
|
|
if (subject == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"Group with ID {groupSubject.SubjectId} not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!group.Subjects.Any(s => s.SubjectId == subject.SubjectId))
|
|
|
|
|
{
|
|
|
|
|
group.Subjects.Add(subject);
|
|
|
|
|
}
|
2024-11-18 16:36:42 +00:00
|
|
|
|
}
|
2024-12-12 07:21:45 +00:00
|
|
|
|
|
|
|
|
|
_dbContext.SaveChanges();
|
2024-11-18 16:36:42 +00:00
|
|
|
|
transaction.Commit();
|
2024-12-12 07:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
2024-11-18 16:36:42 +00:00
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
2024-12-12 07:21:45 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Subject GetSubjectById(int subjectId)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Subjects.FirstOrDefault(s => s.SubjectId == subjectId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Subject GetSubjectByName(string subjectName)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Subjects.FirstOrDefault(s => s.SubjectName == subjectName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddSubject(Subject subject)
|
|
|
|
|
{
|
|
|
|
|
_dbContext.Subjects.Add(subject);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Subject> GetGroupSubjects(int groupId)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Groups
|
|
|
|
|
.Where(group => group.GroupId == groupId)
|
|
|
|
|
.SelectMany(group => group.Subjects)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Subject GetSubjectByGroup(int subjectId)
|
|
|
|
|
{
|
|
|
|
|
var groupSubject = _dbContext.GroupSubjects
|
|
|
|
|
.Include(gs => gs.Subject)
|
|
|
|
|
.FirstOrDefault(gs => gs.SubjectId == subjectId);
|
|
|
|
|
|
|
|
|
|
if (groupSubject == null || groupSubject.Subject == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"Предмет с ID {subjectId} не найден.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupSubject.Subject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Subject> GetSubjectsByGroupId(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var group = _dbContext.Groups
|
|
|
|
|
.Include(g => g.GroupSubjects)
|
|
|
|
|
.ThenInclude(gs => gs.Subject)
|
|
|
|
|
.FirstOrDefault(g => g.GroupId == groupId);
|
|
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return group.GroupSubjects
|
|
|
|
|
.Select(gs => gs.Subject)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddSubjectToGroup(int groupId, Subject subject)
|
|
|
|
|
{
|
|
|
|
|
using (var transaction = _dbContext.Database.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var group = _dbContext.Groups.Include(g => g.Subjects).FirstOrDefault(g => g.GroupId == groupId);
|
|
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxSubjectId = _dbContext.Subjects.Any()
|
|
|
|
|
? _dbContext.Subjects.Max(s => s.SubjectId)
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
subject.SubjectId = maxSubjectId + 1;
|
|
|
|
|
subject.Group = group;
|
|
|
|
|
|
|
|
|
|
_dbContext.Subjects.Add(subject);
|
|
|
|
|
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteAllAttendances()
|
|
|
|
|
{
|
|
|
|
|
using var context = new RemoteDatabaseContext();
|
|
|
|
|
var allAttendances = context.Attendances.ToList();
|
|
|
|
|
context.Attendances.RemoveRange(allAttendances);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteAttendancesByGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RemoteDatabaseContext();
|
|
|
|
|
var attendancesByGroup = context.Attendances.Where(a => a.GroupId == groupId).ToList();
|
|
|
|
|
context.Attendances.RemoveRange(attendancesByGroup);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddAttendance(Attendance attendance)
|
|
|
|
|
{
|
|
|
|
|
_dbContext.Attendances.Add(attendance);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Visit GetVisitById(int visitId)
|
|
|
|
|
{
|
|
|
|
|
return _dbContext.Visits.FirstOrDefault(v => v.VisitId == visitId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetGroupIdBySubjectName(string subjectName)
|
|
|
|
|
{
|
|
|
|
|
var subject = _dbContext.Subjects.Include(s => s.Group)
|
|
|
|
|
.FirstOrDefault(s => s.SubjectName == subjectName);
|
|
|
|
|
|
|
|
|
|
if (subject == null || subject.Group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Предмет с названием '{subjectName}' или его группа не найдены.");
|
2024-11-18 16:36:42 +00:00
|
|
|
|
}
|
2024-12-12 07:21:45 +00:00
|
|
|
|
|
|
|
|
|
return subject.Group.GroupId;
|
2024-11-18 16:36:42 +00:00
|
|
|
|
}
|
2024-12-07 15:33:26 +00:00
|
|
|
|
|
2024-12-12 07:21:45 +00:00
|
|
|
|
public IEnumerable<Attendance> GetAttendances(int groupId, string subject = null, DateTime? date = null, int? studentId = null)
|
2024-12-07 15:33:26 +00:00
|
|
|
|
{
|
2024-12-12 07:21:45 +00:00
|
|
|
|
var query = _dbContext.Attendances.AsQueryable();
|
|
|
|
|
|
|
|
|
|
query = query.Where(a => a.GroupId == groupId);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(subject))
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(a => a.GroupSubject.Subject.SubjectName == subject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (date.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var dateOnly = DateOnly.FromDateTime(date.Value);
|
|
|
|
|
query = query.Where(a => a.Date == dateOnly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (studentId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(a => a.StudentId == studentId.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Attendance GetAttendanceByDateStudentAndLesson(DateTime date, int studentId, int lessonNumber)
|
|
|
|
|
{
|
|
|
|
|
var dateOnly = DateOnly.FromDateTime(date);
|
|
|
|
|
return _dbContext.Attendances
|
|
|
|
|
.FirstOrDefault(a => a.Date == dateOnly && a.StudentId == studentId && a.LessonNumber == lessonNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateAttendance(Attendance attendance)
|
|
|
|
|
{
|
|
|
|
|
_dbContext.Attendances.Update(attendance);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveStudentsFromGroup(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var students = _dbContext.Students.Where(s => s.GroupId == groupId).ToList();
|
|
|
|
|
_dbContext.Students.RemoveRange(students);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RemoveStudentsFromGroupByIds(int groupId, List<int> studentIds)
|
|
|
|
|
{
|
|
|
|
|
var studentsToRemove = _dbContext.Students
|
|
|
|
|
.Where(s => s.GroupId == groupId && studentIds.Contains(s.StudentId))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (studentsToRemove.Any())
|
|
|
|
|
{
|
|
|
|
|
_dbContext.Students.RemoveRange(studentsToRemove);
|
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
|
}
|
2024-12-07 15:33:26 +00:00
|
|
|
|
}
|
2024-11-16 21:45:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|