source: iknow-api/Repository/Implementations/UserRepository.cs@ 2853f2e

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