academia/src/backend/Domain/Services/EnrollmentDomainService.cs

30 lines
860 B
C#

namespace Domain.Services;
using Domain.Entities;
using Domain.Exceptions;
public class EnrollmentDomainService
{
public void ValidateEnrollment(Student student, Subject subject)
{
if (!student.CanEnroll())
throw new MaxEnrollmentsExceededException(student.Id);
if (student.HasProfessor(subject.ProfessorId))
throw new SameProfessorConstraintException(student.Id, subject.ProfessorId);
if (student.Enrollments.Any(e => e.SubjectId == subject.Id))
throw new DuplicateEnrollmentException(student.Id, subject.Id);
}
public Enrollment CreateEnrollment(Student student, Subject subject)
{
ValidateEnrollment(student, subject);
var enrollment = new Enrollment(student.Id, subject.Id);
student.AddEnrollment(enrollment);
return enrollment;
}
}