| | | 1 | | namespace Estud.Back.Features.Students.GetStudentAgenda; |
| | | 2 | | |
| | 0 | 3 | | public class GetStudentAgendaService(EstudDbContext ctx) : IEstudService |
| | | 4 | | { |
| | | 5 | | public async Task<GetStudentAgendaOut> Get() |
| | | 6 | | { |
| | 0 | 7 | | var studentId = ctx.RequestUser.Id; |
| | 0 | 8 | | var institutionId = ctx.RequestUser.InstitutionId; |
| | | 9 | | |
| | 0 | 10 | | var ids = await ctx.ClassStudents.AsNoTracking() |
| | 0 | 11 | | .Where(x => x.StudentId == studentId && x.Status == StudentClassStatus.Matriculado) |
| | 0 | 12 | | .Select(x => x.ClassId) |
| | 0 | 13 | | .ToListAsync(); |
| | | 14 | | |
| | 0 | 15 | | var classes = await ctx.Classes.AsNoTracking() |
| | 0 | 16 | | .Include(t => t.Discipline) |
| | 0 | 17 | | .Include(t => t.Schedules) |
| | 0 | 18 | | .Where(t => t.InstitutionId == institutionId && ids.Contains(t.Id)) |
| | 0 | 19 | | .ToListAsync(); |
| | | 20 | | |
| | | 21 | | // Pra cada dia, pegar as aulas que acontecem nesse dia, ordenadas pelo horário de início |
| | 0 | 22 | | var days = classes.Select(x => x.Schedules.Select(s => s.Day)).SelectMany(x => x).Distinct().OrderBy(x => x).ToL |
| | 0 | 23 | | var agenda = new List<GetStudentAgendaItemOut>(); |
| | | 24 | | |
| | 0 | 25 | | foreach (var day in days) |
| | | 26 | | { |
| | 0 | 27 | | var dayClasses = classes.Where(c => c.Schedules.Any(s => s.Day == day)).ToList(); |
| | 0 | 28 | | var disciplines = dayClasses.SelectMany(c => c.Schedules.Where(s => s.Day == day).Select(s => new GetStudent |
| | 0 | 29 | | { |
| | 0 | 30 | | ClassId = c.Id, |
| | 0 | 31 | | Name = c.Discipline.Name, |
| | 0 | 32 | | Start = s.Start, |
| | 0 | 33 | | End = s.End |
| | 0 | 34 | | })).OrderBy(d => d.Start).ToList(); |
| | | 35 | | |
| | 0 | 36 | | agenda.Add(new GetStudentAgendaItemOut |
| | 0 | 37 | | { |
| | 0 | 38 | | Day = day, |
| | 0 | 39 | | Disciplines = disciplines |
| | 0 | 40 | | }); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return new GetStudentAgendaOut { Days = agenda }; |
| | 0 | 44 | | } |
| | | 45 | | } |