| 1 | using iknow_api.Models;
|
|---|
| 2 | using iknow_api.Data;
|
|---|
| 3 | using Microsoft.EntityFrameworkCore;
|
|---|
| 4 |
|
|---|
| 5 | namespace 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 | } |
|---|