| 1 | using iknow_api.Models;
|
|---|
| 2 | using iknow_api.Data;
|
|---|
| 3 | using Microsoft.EntityFrameworkCore;
|
|---|
| 4 |
|
|---|
| 5 | namespace iknow_api.Repositories
|
|---|
| 6 | {
|
|---|
| 7 | public class UserRepository : IUserRepository
|
|---|
| 8 | {
|
|---|
| 9 | private readonly AppDbContext _context;
|
|---|
| 10 |
|
|---|
| 11 | public UserRepository(AppDbContext context)
|
|---|
| 12 | {
|
|---|
| 13 | _context = context;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | public async Task<bool> UserExistsAsync(string username)
|
|---|
| 17 | {
|
|---|
| 18 | return await _context.User.AnyAsync(u => u.Email == username);
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | public async Task<User?> GetUserByUsernameAsync(string username)
|
|---|
| 22 | {
|
|---|
| 23 | return await _context.User.FirstOrDefaultAsync(u => u.Email == username);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public async Task<User?> GetUserByIdAsync(int id)
|
|---|
| 27 | {
|
|---|
| 28 | return await _context.User
|
|---|
| 29 | .Include(u => u.ContactInfo)
|
|---|
| 30 | .Include(u => u.HighSchool)
|
|---|
| 31 | // The study programme is reached through the enrolment, since
|
|---|
| 32 | // Major hangs off EnrolledSemesters rather than off Users.
|
|---|
| 33 | .Include(u => u.Enrolments!)
|
|---|
| 34 | .ThenInclude(es => es.Major)
|
|---|
| 35 | .Include(u => u.Enrolments!)
|
|---|
| 36 | .ThenInclude(es => es.Semester)
|
|---|
| 37 | .AsSplitQuery()
|
|---|
| 38 | .FirstOrDefaultAsync(u => u.Id == id);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | public async Task<User?> GetOnlyUserByIdAsync(int id)
|
|---|
| 43 | {
|
|---|
| 44 | return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
|
|---|
| 45 | }
|
|---|
| 46 | public async Task AddUserAsync(User user)
|
|---|
| 47 | {
|
|---|
| 48 | _context.User.Add(user);
|
|---|
| 49 | await _context.SaveChangesAsync();
|
|---|
| 50 | }
|
|---|
| 51 | public async Task AddContactAsync(ContactInfo contactInfo)
|
|---|
| 52 | {
|
|---|
| 53 | _context.ContactInfo.Add(contactInfo);
|
|---|
| 54 | await _context.SaveChangesAsync();
|
|---|
| 55 | }
|
|---|
| 56 | public async Task AddHighSchoolAsync(HighSchool highSchool)
|
|---|
| 57 | {
|
|---|
| 58 | _context.HighSchool.Add(highSchool);
|
|---|
| 59 | await _context.SaveChangesAsync();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public async Task<int> GetUsersCountAsync()
|
|---|
| 63 | {
|
|---|
| 64 | return await _context.User.CountAsync();
|
|---|
| 65 | }
|
|---|
| 66 | public async Task<List<EnrolledSemesters>> GetUserSemestersAsync(int id)
|
|---|
| 67 | {
|
|---|
| 68 | return await _context.EnrolledSemesters
|
|---|
| 69 | .AsNoTracking()
|
|---|
| 70 | .Include(es => es.Major)
|
|---|
| 71 | .Include(es => es.Semester)
|
|---|
| 72 | .Include(es => es.SemesterSubjects)
|
|---|
| 73 | .ThenInclude(ss => ss.Subject)
|
|---|
| 74 | .Include(es => es.SemesterSubjects)
|
|---|
| 75 | .ThenInclude(ss => ss.Professor)
|
|---|
| 76 | .Where(es => es.UserId == id)
|
|---|
| 77 | .AsSplitQuery() // This helps avoid cartesian explosion
|
|---|
| 78 | .ToListAsync();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public async Task<List<PassedSubject>> GetUserPassedSubjectsAsync(int id)
|
|---|
| 82 | {
|
|---|
| 83 | return await _context.PassedSubjects
|
|---|
| 84 | .AsNoTracking()
|
|---|
| 85 | .Include(ps => ps.SemesterSubject)
|
|---|
| 86 | .ThenInclude(ss => ss.Subject)
|
|---|
| 87 | .Include(ps => ps.SemesterSubject)
|
|---|
| 88 | .ThenInclude(ss => ss.EnrolledSemester)
|
|---|
| 89 | .ThenInclude(es => es.Semester)
|
|---|
| 90 | .Include(ps => ps.SemesterSubject)
|
|---|
| 91 | .ThenInclude(ss => ss.Professor)
|
|---|
| 92 | // The ER model reaches the student through the enrolment,
|
|---|
| 93 | // not through a user_id on semesters_subjects.
|
|---|
| 94 | .Where(ps => ps.SemesterSubject.EnrolledSemester.UserId == id)
|
|---|
| 95 | .ToListAsync();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | }
|
|---|
| 99 | } |
|---|