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

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

Add getSemesters endpoint and related data access

Introduce a new getSemesters endpoint in UserController.cs to retrieve user semester data using JWT authentication.

Add DbSet<EnrolledSemesters> to AppDbContext.cs for database interaction.

Implement GetUserSemestersAsync in UserRepository.cs to fetch semesters by user ID, and update IUserRepository.cs to declare this method.

Add GetUserSemesters method in UserService.cs to extract user ID from JWT and retrieve semesters, updating IUserService.cs accordingly.

  • Property mode set to 100644
File size: 2.2 KB
Line 
1using iknow_api.Models;
2using iknow_api.Data;
3using Microsoft.EntityFrameworkCore;
4
5namespace 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.EnrollmentInfo)
31 .Include(u => u.HighSchool)
32 .FirstOrDefaultAsync(u => u.Id == id);
33 }
34
35
36 public async Task<User?> GetOnlyUserByIdAsync(int id)
37 {
38 return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
39 }
40 public async Task AddUserAsync(User user)
41 {
42 _context.User.Add(user);
43 await _context.SaveChangesAsync();
44 }
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 }
60
61 public async Task<int> GetUsersCountAsync()
62 {
63 return await _context.User.CountAsync();
64 }
65 public async Task<List<EnrolledSemesters>> GetUserSemestersAsync(int id)
66 {
67 return await _context.EnrolledSemesters
68 .Include(es => es.Major)
69 .Where(es => es.UserId == id)
70 .ToListAsync();
71 }
72
73 }
74}
Note: See TracBrowser for help on using the repository browser.