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

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

Refactor and enhance application structure

Enhanced error handling in AuthController with detailed exception handling for user registration. Updated UserDTO to improve JSON serialization and renamed enums for clarity. Introduced new entities (Major, PassedSubject, etc.) and seeded initial data in AppDbContext.

Replaced legacy migrations with 20251208203846_InitialCreate, reflecting the updated schema. Fixed typographical errors and improved JSON serialization in Program.cs with case-insensitive property matching and enum handling.

Refactored repository interfaces for better null safety. Improved debugging in AuthService and updated connection strings. Added the PassedSubject entity and cleaned up redundant code and unused imports for better maintainability.

  • Property mode set to 100644
File size: 2.0 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
66 }
67}
Note: See TracBrowser for help on using the repository browser.