semesterWork/domain/Service/GroupService.cs
2024-12-12 21:16:01 +03:00

208 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using data.Repository;
using Data.DAO;
using domain.Entity;
using domain.Request;
using domain.UseCase;
using Domain.Entity;
namespace domain.Service
{
public class GroupService : IGroupUseCase
{
private readonly IGroupRepository _groupRepository;
public GroupService(IGroupRepository repositoryGroup)
{
_groupRepository = repositoryGroup;
}
public void AddGroup(AddGroupRequest addGroupRequest)
{
_groupRepository.AddGroup(new Group { GroupName = addGroupRequest.Name });
}
public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudent)
{
Group groups = new Group { GroupName = addGroupWithStudent.addGroupRequest.Name };
List<Student> students = addGroupWithStudent
.addStudentRequests
.Select(it => new Student
{
FirstName = it.FirstName,
LastName = it.LastName,
Patronymic = it.Patronymic
})
.ToList();
_groupRepository.addGroupWithStudent(groups, students);
}
public void AddStudentsToGroup(int groupId, List<AddStudentRequest> students)
{
var group = _groupRepository.GetGroupById(groupId);
if (group == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
var studentEntities = students.Select(student => new Student
{
FirstName = student.FirstName,
LastName = student.LastName,
Patronymic = student.Patronymic,
GroupId = groupId
}).ToList();
_groupRepository.AddStudentsToGroup(studentEntities);
}
public void DeleteGroup(int groupId)
{
_groupRepository.DeleteGroup(groupId);
}
public IEnumerable<GroupEntity> GetGroupsWithStudents()
{
return _groupRepository.GetAllGroup().Select(
group => new GroupEntity
{
GroupId = group.GroupId,
GroupName = group.GroupName,
Students = group.Students.Select(
user => new StudentEntity
{
Id = user.StudentId,
FirstName = user.FirstName,
LastName = user.LastName,
Patronymic = user.Patronymic,
Group = new GroupEntity
{
GroupId = group.GroupId,
GroupName = group.GroupName,
}
}).ToList(),
Subjects = group.GroupSubjects.Select(gs => new SubjectEntity
{
SubjectId = gs.Subject.SubjectId,
SubjectName = gs.Subject.SubjectName
}).ToList()
}).ToList();
}
public List<SubjectEntity> GetSubjectsByGroupId(int groupId)
{
var group = _groupRepository.GetGroupWithSubjects(groupId);
if (group == null)
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
return group.Subjects.Select(subject => new SubjectEntity
{
SubjectId = subject.SubjectId,
SubjectName = subject.SubjectName
}).ToList();
}
public Group GetGroupById(int groupId)
{
var group = _groupRepository.GetGroupById(groupId);
if (group == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
return group;
}
public List<Subject> GetGroupSubjects(int groupId)
{
var subjects = _groupRepository.GetGroupSubjects(groupId);
if (subjects == null || subjects.Count == 0)
{
throw new KeyNotFoundException($"Предметы для группы с ID {groupId} не найдены.");
}
return subjects;
}
public bool AddSubjectToGroup(int groupId, string subjectName)
{
var subject = new Subject
{
SubjectName = subjectName,
};
return _groupRepository.AddSubjectToGroup(groupId, subject);
}
public void DeleteAllAttendances()
{
_groupRepository.DeleteAllAttendances();
}
public void DeleteAttendancesByGroup(int groupId)
{
_groupRepository.DeleteAttendancesByGroup(groupId);
}
public void AddAttendance(Attendance attendance)
{
if (attendance == null)
{
throw new ArgumentNullException(nameof(attendance));
}
_groupRepository.AddAttendance(attendance);
}
public Visit GetVisitById(int visitId)
{
return _groupRepository.GetVisitById(visitId);
}
public int GetGroupIdBySubjectName(string subjectName)
{
return _groupRepository.GetGroupIdBySubjectName(subjectName);
}
public Subject GetSubjectByName(string subjectName)
{
return _groupRepository.GetSubjectByName(subjectName);
}
public IEnumerable<Attendance> GetAttendances(int groupId, string? subject = null, DateTime? date = null, int? studentId = null)
{
return _groupRepository.GetAttendances(groupId, subject, date, studentId);
}
public Attendance GetAttendanceByDateStudentAndLesson(DateTime date, int studentId, int lessonNumber)
{
return _groupRepository.GetAttendanceByDateStudentAndLesson(date, studentId, lessonNumber);
}
public void UpdateAttendance(Attendance attendance)
{
_groupRepository.UpdateAttendance(attendance);
}
public void RemoveStudentsFromGroup(int groupId)
{
_groupRepository.RemoveStudentsFromGroup(groupId);
}
public void RemoveStudentsFromGroupByIds(int groupId, List<int> studentIds)
{
var group = _groupRepository.GetGroupById(groupId);
if (group == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
_groupRepository.RemoveStudentsFromGroupByIds(groupId, studentIds);
}
}
}