presence/domain/Service/GroupService.cs
2024-12-12 10:20:21 +03:00

108 lines
3.9 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.DAO;
using data.Repository;
using domain.Entity;
using domain.Request;
using domain.UseCase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace domain.Service
{
public class GroupService : IGroupUseCase
{
private readonly IGroupRepository _groupRepository;
public GroupService(IGroupRepository groupRepository)
{
_groupRepository = groupRepository;
}
public bool AddGroup(AddGroupRequest addGroupRequest)
=> _groupRepository.CreateGroup(new Group { Name = addGroupRequest.Name });
public void AddGroupWithStudents(AddGroupWithStudentRequest addGroupWithStudentRequest)
{
Group group = new Group { Name = addGroupWithStudentRequest.AddGroupRequest.Name };
List<Student> students = addGroupWithStudentRequest
.AddStudentRequests
.Select(it => new Student { FirstName = it.FirstName, LastName = it.LastName, Patronymic = it.Patronymic})
.ToList();
_groupRepository.AddGroupWithStudents(group, students);
}
public void AddStudentsToGroup(int id, IEnumerable<AddStudentRequest> students)
{
List<Student> studentList = students
.Select(it => new Student { FirstName = it.FirstName, LastName = it.LastName, Patronymic = it.Patronymic })
.ToList();
_groupRepository.AddStudentsToGroup(id, studentList);
}
public void EditGroup(EditGroupRequest editGroupRequest)
=> _groupRepository.UpdateGroup(editGroupRequest.GroupId, editGroupRequest.GroupName);
public IEnumerable<GroupEntity> GetGroupsWithStudents()
{
return _groupRepository.GetAllGroups().Select(
group => new GroupEntity
{
Id = group.Id,
Name = group.Name,
Users = group.Students.Select(
user => new UserEntity
{
Id = user.Id,
LastName = user.LastName,
FirstName = user.FirstName,
Patronymic = user.Patronymic,
Group = new GroupEntity
{
Id = group.Id,
Name = group.Name
}
})
});
}
public bool RemoveGroup(RemoveGroupRequest removeGroupRequest)
=> _groupRepository.DeleteGroup(removeGroupRequest.GroupId);
public GroupSubjectEntity GetGroupSubject(int id)
{
return _groupRepository.GetAllGroups().Select(
group => new GroupSubjectEntity
{
Id = group.Id,
Name = group.Name,
Subjects = group.StudentGroupsSubjects.Select(
subject => new SubjectEntity
{
Id = subject.Subject.Id,
Name = subject.Subject.Name,
})
}).Single(g => g.Id == id);
}
public void RemoveStudentsFromGroup(int groupId)
{
_groupRepository.RemoveStudentsFromGroup(groupId);
}
public void RemoveStudentsFromGroupByIds(int groupId, IEnumerable<int> studentIds)
{
var group = _groupRepository.GetAllGroups().FirstOrDefault(g => g.Id == groupId);
if (group == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
_groupRepository.RemoveStudentsFromGroupByIds(groupId, studentIds);
}
}
}