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

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

Merge branch 'Dev' into feature/getUser

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[e49c3ed]1using iknow_api.Models;
2using iknow_api.Data;
3using iknow_api.Models;
[5b42c8d]4using Microsoft.EntityFrameworkCore;
[9a57e89]5
[277bd72]6namespace iknow_api.Repositories
[9a57e89]7{
8 public class UserRepository : IUserRepository
9 {
10 private readonly AppDbContext _context;
11
12 public UserRepository(AppDbContext context)
13 {
14 _context = context;
15 }
16
[5b42c8d]17 public async Task<bool> UserExistsAsync(string username)
18 {
19 return await _context.User.AnyAsync(u => u.Email == username);
20 }
[9a57e89]21
[5b42c8d]22 public async Task<User?> GetUserByUsernameAsync(string username)
[9a57e89]23 {
[5b42c8d]24 return await _context.User.FirstOrDefaultAsync(u => u.Email == username);
[9a57e89]25 }
[cab02ad]26
[b38c39c]27 public async Task<User?> GetUserByIdAsync(int id)
[cab02ad]28 {
[b38c39c]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);
[cab02ad]34 }
35
[9a57e89]36
[c155364]37 public async Task<User?> GetOnlyUserByIdAsync(int id)
38 {
39 return await _context.User.FirstOrDefaultAsync(u => u.Id == id);
40 }
[5b42c8d]41 public async Task AddUserAsync(User user)
42 {
43 _context.User.Add(user);
44 await _context.SaveChangesAsync();
45 }
[b38c39c]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 }
[5b42c8d]61
62 public async Task<int> GetUsersCountAsync()
63 {
64 return await _context.User.CountAsync();
65 }
[9a57e89]66 }
67}
Note: See TracBrowser for help on using the repository browser.