semesterWork/domain/Service/GroupService.cs

208 lines
6.7 KiB
C#
Raw Normal View History

2024-12-12 18:16:01 +00:00
using data.Repository;
using Data.DAO;
2024-12-04 21:58:43 +00:00
using domain.Entity;
using domain.Request;
using domain.UseCase;
2024-12-12 18:16:01 +00:00
using Domain.Entity;
namespace domain.Service
{
public class GroupService : IGroupUseCase
{
private readonly IGroupRepository _groupRepository;
2024-12-12 18:16:01 +00:00
public GroupService(IGroupRepository repositoryGroup)
{
2024-12-12 18:16:01 +00:00
_groupRepository = repositoryGroup;
}
2024-12-12 18:16:01 +00:00
public void AddGroup(AddGroupRequest addGroupRequest)
{
2024-12-12 18:16:01 +00:00
_groupRepository.AddGroup(new Group { GroupName = addGroupRequest.Name });
}
2024-12-12 18:16:01 +00:00
public void AddGroupWithStudents(AddGroupWithStudentsRequest addGroupWithStudent)
{
2024-12-12 18:16:01 +00:00
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();
2024-12-12 18:16:01 +00:00
_groupRepository.addGroupWithStudent(groups, students);
}
2024-12-04 21:58:43 +00:00
2024-12-12 18:16:01 +00:00
public void AddStudentsToGroup(int groupId, List<AddStudentRequest> students)
2024-12-04 21:58:43 +00:00
{
2024-12-12 18:16:01 +00:00
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);
2024-12-04 21:58:43 +00:00
}
2024-12-07 15:33:26 +00:00
2024-12-12 18:16:01 +00:00
public void DeleteGroup(int groupId)
2024-12-07 15:33:26 +00:00
{
2024-12-12 18:16:01 +00:00
_groupRepository.DeleteGroup(groupId);
}
2024-12-07 15:33:26 +00:00
2024-12-12 18:16:01 +00:00
public IEnumerable<GroupEntity> GetGroupsWithStudents()
{
return _groupRepository.GetAllGroup().Select(
2024-12-07 15:33:26 +00:00
group => new GroupEntity
{
2024-12-12 18:16:01 +00:00
GroupId = group.GroupId,
GroupName = group.GroupName,
Students = group.Students.Select(
user => new StudentEntity
2024-12-07 15:33:26 +00:00
{
2024-12-12 18:16:01 +00:00
Id = user.StudentId,
FirstName = user.FirstName,
LastName = user.LastName,
Patronymic = user.Patronymic,
2024-12-07 15:33:26 +00:00
Group = new GroupEntity
{
2024-12-12 18:16:01 +00:00
GroupId = group.GroupId,
GroupName = group.GroupName,
2024-12-07 15:33:26 +00:00
}
2024-12-12 18:16:01 +00:00
}).ToList(),
Subjects = group.GroupSubjects.Select(gs => new SubjectEntity
{
SubjectId = gs.Subject.SubjectId,
SubjectName = gs.Subject.SubjectName
}).ToList()
2024-12-07 15:33:26 +00:00
}).ToList();
}
2024-12-12 18:16:01 +00:00
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);
}
}
}