| | 1 | | using Syki.Back.Features.Teacher.GetTeacherClassStudents; |
| | 2 | |
|
| | 3 | | namespace Syki.Back.Features.Academic.GetAcademicClass; |
| | 4 | |
|
| 18 | 5 | | public class GetAcademicClassService(SykiDbContext ctx, GetTeacherClassStudentsService service) : IAcademicService |
| | 6 | | { |
| | 7 | | public async Task<OneOf<GetAcademicClassOut, SykiError>> Get(Guid institutionId, Guid id) |
| | 8 | | { |
| 18 | 9 | | var @class = await ctx.Classes.AsNoTracking() |
| 18 | 10 | | .Include(t => t.Discipline) |
| 18 | 11 | | .Include(t => t.Teacher) |
| 18 | 12 | | .Include(t => t.Schedules) |
| 18 | 13 | | .Include(t => t.Lessons) |
| 18 | 14 | | .ThenInclude(l => l.Attendances) |
| 18 | 15 | | .Where(c => c.InstitutionId == institutionId && c.Id == id) |
| 18 | 16 | | .FirstOrDefaultAsync(); |
| | 17 | |
|
| 20 | 18 | | if (@class == null) return new ClassNotFound(); |
| | 19 | |
|
| 16 | 20 | | var count = await ctx.ClassesStudents.Where(x => x.ClassId == id).CountAsync(); |
| 16 | 21 | | @class.SetFillRatio(count); |
| | 22 | |
|
| 16 | 23 | | var period = await ctx.EnrollmentPeriods.AsNoTracking().Where(x => x.InstitutionId == institutionId && x.Id == @ |
| 16 | 24 | | if (@class.Status == ClassStatus.OnEnrollment && period?.EndAt < DateTime.UtcNow.ToDateOnly()) |
| | 25 | | { |
| 2 | 26 | | @class.Status = ClassStatus.AwaitingStart; |
| | 27 | | } |
| | 28 | |
|
| 16 | 29 | | var result = @class.ToGetAcademicClassOut(); |
| | 30 | |
|
| 16 | 31 | | var classStudents = (await service.Get(@class.TeacherId!.Value, @class.Id)).Success; |
| | 32 | |
|
| 18 | 33 | | result.Students = classStudents.ConvertAll(x => new AcademicClassStudentOut |
| 18 | 34 | | { |
| 18 | 35 | | Id = x.Id, |
| 18 | 36 | | Name = x.Name, |
| 18 | 37 | | Frequency = x.Frequency, |
| 18 | 38 | | Notes = x.Notes |
| 18 | 39 | | }); |
| | 40 | |
|
| 18 | 41 | | result.Frequency = result.Students.Count > 0 ? result.Students.Average(s => s.Frequency) : 0.00M; |
| | 42 | |
|
| 16 | 43 | | return result; |
| 18 | 44 | | } |
| | 45 | | } |