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

Last change on this file since f844b7e was e423ff3, checked in by GitHub <noreply@…>, 11 months ago

Merge branch 'Dev' into feature/getUser

  • Property mode set to 100644
File size: 2.0 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<User?> GetOnlyUserByIdAsync(int id)
38 {
39 return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
40 }
41 public async Task AddUserAsync(User user)
42 {
43 _context.User.Add(user);
44 await _context.SaveChangesAsync();
45 }
46 public async Task AddContactAsync(ContactInfo contactInfo)
47 {
48 _context.ContactInfo.Add(contactInfo);
49 await _context.SaveChangesAsync();
50 }
51 public async Task AddEnrollmentAsync(EnrollmentInfo enrollment)
52 {
53 _context.EnrollmentInfo.Add(enrollment);
54 await _context.SaveChangesAsync();
55 }
56 public async Task AddHighSchoolAsync(HighSchool highSchool)
57 {
58 _context.HighSchool.Add(highSchool);
59 await _context.SaveChangesAsync();
60 }
61
62 public async Task<int> GetUsersCountAsync()
63 {
64 return await _context.User.CountAsync();
65 }
66 }
67}
Note: See TracBrowser for help on using the repository browser.