| | 1 | | namespace Syki.Back.Features.Academic.GetAcademicClasses; |
| | 2 | |
|
| 4 | 3 | | public class GetAcademicClassesService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<List<ClassOut>> Get(Guid institutionId, GetAcademicClassesIn query) |
| | 6 | | { |
| 4 | 7 | | var classes = await ctx.Classes.AsNoTracking() |
| 4 | 8 | | .Include(t => t.Discipline) |
| 4 | 9 | | .Include(t => t.Teacher) |
| 4 | 10 | | .Include(t => t.Schedules) |
| 4 | 11 | | .Include(t => t.Lessons) |
| 4 | 12 | | .ThenInclude(l => l.Attendances) |
| 4 | 13 | | .Where(c => c.InstitutionId == institutionId) |
| 4 | 14 | | .ToListAsync(); |
| | 15 | |
|
| 4 | 16 | | var periods = await ctx.EnrollmentPeriods.AsNoTracking().Where(x => x.InstitutionId == institutionId).ToListAsyn |
| | 17 | |
|
| 8 | 18 | | var ids = classes.ConvertAll(x => x.Id); |
| 4 | 19 | | var links = await ctx.ClassesStudents.AsNoTracking().Where(x => ids.Contains(x.ClassId)).ToListAsync(); |
| 16 | 20 | | foreach (var @class in classes) |
| | 21 | | { |
| 5 | 22 | | var count = links.Count(x => x.ClassId == @class.Id); |
| 4 | 23 | | @class.SetFillRatio(count); |
| | 24 | |
|
| 6 | 25 | | var period = periods.FirstOrDefault(x => x.Id == @class.PeriodId); |
| 4 | 26 | | if (@class.Status == ClassStatus.OnEnrollment && period?.EndAt < DateTime.Now.ToDateOnly()) |
| | 27 | | { |
| 1 | 28 | | @class.Status = ClassStatus.AwaitingStart; |
| | 29 | | } |
| | 30 | | } |
| | 31 | |
|
| 4 | 32 | | var status = query?.Status ?? null; |
| 11 | 33 | | return classes.Where(x => status == null || x.Status == status).Select(t => t.ToOut()).ToList(); |
| 4 | 34 | | } |
| | 35 | | } |