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

Last change on this file since c0256f3 was c0256f3, checked in by stefansaveski <stefansaveski19@…>, 11 months ago

Add user-related entities and enforce one-to-one mappings

Enhanced the User model with new entities: ContactInfo,
EnrollmentInfo, and HighSchool, establishing one-to-one
relationships. Updated UserDTO with additional fields and
introduced enums for type, quota, and major. Modified
UserRepository and AuthService to handle new entities.

Updated database schema with unique constraints on UserId
for related tables and added EF Core migration
20251027191028_dbRelations. Adjusted JSON serialization
to prevent circular references.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1using iknow_api.Models;
2using iknow_api.Data;
3using iknow_api.Models;
4using Microsoft.EntityFrameworkCore;
5
6namespace iknow_api.Repositories
7{
8 public class UserRepository : IUserRepository
9 {
10 private readonly AppDbContext _context;
11
12 public UserRepository(AppDbContext context)
13 {
14 _context = context;
15 }
16
17 public async Task<bool> UserExistsAsync(string username)
18 {
19 return await _context.User.AnyAsync(u => u.Email == username);
20 }
21
22 public async Task<User?> GetUserByUsernameAsync(string username)
23 {
24 return await _context.User.FirstOrDefaultAsync(u => u.Email == username);
25 }
26
27 public async Task<User?> GetUserByIdAsync(int id)
28 {
29 return await _context.User
30 .Include(u => u.ContactInfo)
31 .Include(u => u.EnrollmentInfo)
32 .Include(u => u.HighSchool)
33 .FirstOrDefaultAsync(u => u.Id == id);
34 }
35
36
37 public async Task AddUserAsync(User user)
38 {
39 _context.User.Add(user);
40 await _context.SaveChangesAsync();
41 }
42 public async Task AddContactAsync(ContactInfo contactInfo)
43 {
44 _context.ContactInfo.Add(contactInfo);
45 await _context.SaveChangesAsync();
46 }
47 public async Task AddEnrollmentAsync(EnrollmentInfo enrollment)
48 {
49 _context.EnrollmentInfo.Add(enrollment);
50 await _context.SaveChangesAsync();
51 }
52 public async Task AddHighSchoolAsync(HighSchool highSchool)
53 {
54 _context.HighSchool.Add(highSchool);
55 await _context.SaveChangesAsync();
56 }
57
58 public async Task<int> GetUsersCountAsync()
59 {
60 return await _context.User.CountAsync();
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.