source: iknow-api/Repository/Implementations/UserRepository.cs@ 1b20b22

Last change on this file since 1b20b22 was 1b20b22, checked in by Stefan-Saveski <stefansaveski19@…>, 8 days ago
  • ready for presentation
  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[5eb7847]1using iknow_api.Models;
2using iknow_api.Data;
[fc7b4b4]3using Microsoft.EntityFrameworkCore;
[9694de9]4
[271178d]5namespace iknow_api.Repositories
[9694de9]6{
7 public class UserRepository : IUserRepository
8 {
9 private readonly AppDbContext _context;
10
11 public UserRepository(AppDbContext context)
12 {
13 _context = context;
14 }
15
[fc7b4b4]16 public async Task<bool> UserExistsAsync(string username)
17 {
18 return await _context.User.AnyAsync(u => u.Email == username);
19 }
[9694de9]20
[fc7b4b4]21 public async Task<User?> GetUserByUsernameAsync(string username)
[9694de9]22 {
[fc7b4b4]23 return await _context.User.FirstOrDefaultAsync(u => u.Email == username);
[9694de9]24 }
[4f5d8fd]25
[c0256f3]26 public async Task<User?> GetUserByIdAsync(int id)
[4f5d8fd]27 {
[c0256f3]28 return await _context.User
29 .Include(u => u.ContactInfo)
30 .Include(u => u.HighSchool)
[1b20b22]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()
[c0256f3]38 .FirstOrDefaultAsync(u => u.Id == id);
[4f5d8fd]39 }
40
[9694de9]41
[1e58098]42 public async Task<User?> GetOnlyUserByIdAsync(int id)
43 {
44 return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
45 }
[fc7b4b4]46 public async Task AddUserAsync(User user)
47 {
48 _context.User.Add(user);
49 await _context.SaveChangesAsync();
50 }
[c0256f3]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 }
[fc7b4b4]61
62 public async Task<int> GetUsersCountAsync()
63 {
64 return await _context.User.CountAsync();
65 }
[615f18d]66 public async Task<List<EnrolledSemesters>> GetUserSemestersAsync(int id)
67 {
68 return await _context.EnrolledSemesters
[2e17229]69 .AsNoTracking()
[615f18d]70 .Include(es => es.Major)
[2e17229]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)
[615f18d]76 .Where(es => es.UserId == id)
[2e17229]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)
[1b20b22]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)
[615f18d]95 .ToListAsync();
96 }
[f0fc386]97
[9694de9]98 }
99}
Note: See TracBrowser for help on using the repository browser.