| [1b20b22] | 1 | using iknow_api.Data;
|
|---|
| 2 | using iknow_api.Models;
|
|---|
| 3 | using Microsoft.EntityFrameworkCore;
|
|---|
| 4 |
|
|---|
| 5 | namespace iknow_api.Repositories
|
|---|
| 6 | {
|
|---|
| 7 | public class ProfRepository : IProfRepository
|
|---|
| 8 | {
|
|---|
| 9 | private readonly AppDbContext _context;
|
|---|
| 10 |
|
|---|
| 11 | public ProfRepository(AppDbContext context)
|
|---|
| 12 | {
|
|---|
| 13 | _context = context;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | /// <summary>
|
|---|
| 17 | /// A professor teaches a subject for a given enrolment, so the students
|
|---|
| 18 | /// are reached through semesters_subjects.professor_id and the student
|
|---|
| 19 | /// through the enrolment - matching the ER model, which does not link
|
|---|
| 20 | /// SemestersSubjects to a student directly.
|
|---|
| 21 | /// </summary>
|
|---|
| 22 | private IQueryable<SemesterSubject> TaughtBy(int professorId) =>
|
|---|
| 23 | _context.SemesterSubjects
|
|---|
| 24 | .Where(ss => ss.ProfessorId == professorId)
|
|---|
| 25 | .Include(ss => ss.Subject)
|
|---|
| 26 | .Include(ss => ss.PassedSubject)
|
|---|
| 27 | .Include(ss => ss.EnrolledSemester!)
|
|---|
| 28 | .ThenInclude(es => es.User)
|
|---|
| 29 | .Include(ss => ss.EnrolledSemester!)
|
|---|
| 30 | .ThenInclude(es => es.Semester);
|
|---|
| 31 |
|
|---|
| 32 | public async Task<List<SemesterSubject>> GetTaughtSemesterSubjectsAsync(int professorId) =>
|
|---|
| 33 | await TaughtBy(professorId)
|
|---|
| 34 | .AsNoTracking()
|
|---|
| 35 | .AsSplitQuery()
|
|---|
| 36 | .ToListAsync();
|
|---|
| 37 |
|
|---|
| 38 | public async Task<SemesterSubject?> FindTaughtSemesterSubjectAsync(
|
|---|
| 39 | int professorId, int studentId, int subjectId)
|
|---|
| 40 | {
|
|---|
| 41 | var candidates = await TaughtBy(professorId)
|
|---|
| 42 | .Where(ss => ss.SubjectId == subjectId
|
|---|
| 43 | && ss.EnrolledSemester!.UserId == studentId)
|
|---|
| 44 | .AsSplitQuery()
|
|---|
| 45 | .ToListAsync();
|
|---|
| 46 |
|
|---|
| 47 | // Most recent enrolment first. Semesters are labelled Year/Year+1
|
|---|
| 48 | // throughout the app, so within one year summer follows winter.
|
|---|
| 49 | return candidates
|
|---|
| 50 | .OrderByDescending(ss => ss.EnrolledSemester!.Semester!.Year)
|
|---|
| 51 | .ThenByDescending(ss => ss.EnrolledSemester!.Semester!.Type == sType.summer)
|
|---|
| 52 | .FirstOrDefault();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public async Task<PassedSubject?> GetPassedSubjectAsync(int semesterSubjectId) =>
|
|---|
| 56 | await _context.PassedSubjects
|
|---|
| 57 | .FirstOrDefaultAsync(ps => ps.SemesterSubjectId == semesterSubjectId);
|
|---|
| 58 |
|
|---|
| 59 | public async Task AddPassedSubjectAsync(PassedSubject passed)
|
|---|
| 60 | {
|
|---|
| 61 | _context.PassedSubjects.Add(passed);
|
|---|
| 62 | await _context.SaveChangesAsync();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public async Task UpdatePassedSubjectAsync(PassedSubject passed)
|
|---|
| 66 | {
|
|---|
| 67 | _context.PassedSubjects.Update(passed);
|
|---|
| 68 | await _context.SaveChangesAsync();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public async Task RemovePassedSubjectAsync(PassedSubject passed)
|
|---|
| 72 | {
|
|---|
| 73 | _context.PassedSubjects.Remove(passed);
|
|---|
| 74 | await _context.SaveChangesAsync();
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|