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

Last change on this file since aee2b88 was aee2b88, checked in by Stefan-Saveski <stefansaveski19@…>, 9 months ago

Upgraded database, added get subjects and get passed subjects.

  • Property mode set to 100644
File size: 3.2 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.EnrollmentInfo)
31 .Include(u => u.HighSchool)
32 .FirstOrDefaultAsync(u => u.Id == id);
[cab02ad]33 }
34
[9a57e89]35
[c155364]36 public async Task<User?> GetOnlyUserByIdAsync(int id)
37 {
38 return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
39 }
[5b42c8d]40 public async Task AddUserAsync(User user)
41 {
42 _context.User.Add(user);
43 await _context.SaveChangesAsync();
44 }
[b38c39c]45 public async Task AddContactAsync(ContactInfo contactInfo)
46 {
47 _context.ContactInfo.Add(contactInfo);
48 await _context.SaveChangesAsync();
49 }
50 public async Task AddEnrollmentAsync(EnrollmentInfo enrollment)
51 {
52 _context.EnrollmentInfo.Add(enrollment);
53 await _context.SaveChangesAsync();
54 }
55 public async Task AddHighSchoolAsync(HighSchool highSchool)
56 {
57 _context.HighSchool.Add(highSchool);
58 await _context.SaveChangesAsync();
59 }
[5b42c8d]60
61 public async Task<int> GetUsersCountAsync()
62 {
63 return await _context.User.CountAsync();
64 }
[691f152]65 public async Task<List<EnrolledSemesters>> GetUserSemestersAsync(int id)
66 {
67 return await _context.EnrolledSemesters
[aee2b88]68 .AsNoTracking()
[691f152]69 .Include(es => es.Major)
[aee2b88]70 .Include(es => es.Semester)
71 .Include(es => es.SemesterSubjects)
72 .ThenInclude(ss => ss.Subject)
73 .Include(es => es.SemesterSubjects)
74 .ThenInclude(ss => ss.Professor)
[691f152]75 .Where(es => es.UserId == id)
[aee2b88]76 .AsSplitQuery() // This helps avoid cartesian explosion
77 .ToListAsync();
78 }
79
80 public async Task<List<PassedSubject>> GetUserPassedSubjectsAsync(int id)
81 {
82 return await _context.PassedSubjects
83 .AsNoTracking()
84 .Include(ps => ps.SemesterSubject)
85 .ThenInclude(ss => ss.Subject)
86 .Include(ps => ps.SemesterSubject)
87 .ThenInclude(ss => ss.EnrolledSemester)
88 .ThenInclude(es => es.Semester)
89 .Include(ps => ps.SemesterSubject)
90 .ThenInclude(ss => ss.Professor)
91 .Where(ps => ps.SemesterSubject.UserId == id)
[691f152]92 .ToListAsync();
93 }
[c2f79d7]94
[9a57e89]95 }
96}
Note: See TracBrowser for help on using the repository browser.