| | 1 | | namespace Syki.Back.Features.Student.CreateStudentEnrollment; |
| | 2 | |
|
| 304 | 3 | | public class CreateStudentEnrollmentService(SykiDbContext ctx) : IStudentService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<SykiSuccess, SykiError>> Create(Guid institutionId, Guid userId, Guid courseCurriculumId, Cr |
| | 6 | | { |
| 38 | 7 | | var today = DateOnly.FromDateTime(DateTime.Now); |
| 38 | 8 | | var enrollmentPeriod = await ctx.EnrollmentPeriods.AsNoTracking() |
| 38 | 9 | | .Where(p => p.InstitutionId == institutionId && p.StartAt <= today && p.EndAt >= today) |
| 38 | 10 | | .FirstOrDefaultAsync(); |
| 39 | 11 | | if (enrollmentPeriod == null) return new EnrollmentPeriodNotFound(); |
| | 12 | |
|
| 37 | 13 | | var courseCurriculum = await ctx.CourseCurriculums.Where(g => g.Id == courseCurriculumId).AsNoTracking() |
| 37 | 14 | | .Include(g => g.Links) |
| 37 | 15 | | .FirstAsync(); |
| 481 | 16 | | var disciplines = courseCurriculum.Links.ConvertAll(d => d.DisciplineId); |
| 37 | 17 | | var classes = await ctx.Classes.AsNoTracking() |
| 37 | 18 | | .Where(t => t.InstitutionId == institutionId && t.PeriodId == enrollmentPeriod.Id && t.Status == ClassStatus |
| 37 | 19 | | .ToListAsync(); |
| 357 | 20 | | var ids = classes.Where(t => data.Classes.Contains(t.Id)).Select(t => t.Id).ToList(); |
| | 21 | |
|
| 37 | 22 | | var classesStudents = await ctx.ClassesStudents.Where(x => x.SykiStudentId == userId && x.StudentDisciplineStatu |
| 37 | 23 | | ctx.RemoveRange(classesStudents); |
| 196 | 24 | | ids.ForEach(id => ctx.Add(new ClassStudent(id, userId))); |
| | 25 | |
|
| 37 | 26 | | await ctx.SaveChangesAsync(); |
| | 27 | |
|
| 37 | 28 | | return new SykiSuccess(); |
| 38 | 29 | | } |
| | 30 | | } |