| | 1 | | namespace Syki.Back.Features.Academic.GetCampi; |
| | 2 | |
|
| 8 | 3 | | public class GetCampiService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<List<CampusOut>> Get(Guid institutionId) |
| | 6 | | { |
| 8 | 7 | | var campi = await ctx.Campi.AsNoTracking() |
| 8 | 8 | | .Where(c => c.InstitutionId == institutionId) |
| 8 | 9 | | .OrderBy(c => c.Name) |
| 8 | 10 | | .ToListAsync(); |
| | 11 | |
|
| 8 | 12 | | FormattableString sql = $@" |
| 8 | 13 | | SELECT |
| 8 | 14 | | c.id AS id, |
| 8 | 15 | | count(DISTINCT s.id) AS students, |
| 8 | 16 | | count(DISTINCT tc.syki_teacher_id) AS teachers |
| 8 | 17 | | FROM |
| 8 | 18 | | syki.campi c |
| 8 | 19 | | LEFT JOIN |
| 8 | 20 | | syki.teachers__campi tc ON tc.campus_id = c.id |
| 8 | 21 | | LEFT JOIN |
| 8 | 22 | | syki.course_offerings co ON co.campus_id = c.id |
| 8 | 23 | | LEFT JOIN |
| 8 | 24 | | syki.students s ON s.course_offering_id = co.id |
| 8 | 25 | | WHERE |
| 8 | 26 | | c.institution_id = {institutionId} |
| 8 | 27 | | GROUP BY |
| 8 | 28 | | c.id |
| 8 | 29 | | "; |
| 8 | 30 | | var totals = await ctx.Database.SqlQuery<CampusEnrollmentDto>(sql).ToListAsync(); |
| | 31 | |
|
| 8 | 32 | | var result = campi.ConvertAll(x => |
| 8 | 33 | | { |
| 8 | 34 | | var campusOut = x.ToOut(); |
| 18 | 35 | | campusOut.Students = totals.FirstOrDefault(t => t.Id == x.Id)?.Students ?? 0; |
| 18 | 36 | | campusOut.Teachers = totals.FirstOrDefault(t => t.Id == x.Id)?.Teachers ?? 0; |
| 8 | 37 | | campusOut.FillRate = campusOut.Capacity > 0 ? Math.Round(100M * (1M * campusOut.Students / (1M * campusOut.C |
| 8 | 38 | | return campusOut; |
| 8 | 39 | | }); |
| | 40 | |
|
| 8 | 41 | | return result; |
| 8 | 42 | | } |
| | 43 | |
|
| | 44 | | private class CampusEnrollmentDto |
| | 45 | | { |
| 20 | 46 | | public Guid Id { get; set; } |
| 8 | 47 | | public int Students { get; set; } |
| 8 | 48 | | public int Teachers { get; set; } |
| | 49 | | } |
| | 50 | | } |