Class UserRepository
- Namespace
- Builvero.Infrastructure.Repositories
- Assembly
- Builvero.Infrastructure.dll
Repository implementation for user data access operations using Entity Framework Core.
public class UserRepository : IUserRepository
- Inheritance
-
UserRepository
- Implements
- Inherited Members
Remarks
This repository provides methods for querying, creating, updating, and managing users and their related entities. All queries use eager loading (Include) to fetch related entities (Profile, Educations, Experiences, Skills, BuilderTags) to avoid N+1 query problems.
Constructors
UserRepository(ApplicationDbContext)
Initializes a new instance of the UserRepository class.
public UserRepository(ApplicationDbContext context)
Parameters
contextApplicationDbContextThe Entity Framework database context for data access.
Methods
CreateAsync(User, CancellationToken)
Creates a new user entity in the database.
public Task<User> CreateAsync(User user, CancellationToken cancellationToken = default)
Parameters
userUserThe user entity to create. Related entities (Profile, Educations, etc.) are also persisted if included.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
This method adds the user to the context and saves changes. If the user includes related entities (Profile, Educations, Experiences, etc.), they are also persisted in the same transaction.
EmailExistsAsync(string, CancellationToken)
Checks if a user with the specified email address already exists in the database.
public Task<bool> EmailExistsAsync(string email, CancellationToken cancellationToken = default)
Parameters
emailstringThe email address to check.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
Email comparison is case-sensitive. This method is typically used during user registration to enforce email uniqueness.
GetByEmailAsync(string, CancellationToken)
Retrieves a user by their email address with profile loaded.
public Task<User?> GetByEmailAsync(string email, CancellationToken cancellationToken = default)
Parameters
emailstringThe email address of the user to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
Email comparison is case-sensitive. Only the Profile navigation property is eagerly loaded.
GetByEmailWithProfileAsync(string, CancellationToken)
Retrieves a user by their email address with complete profile information loaded.
public Task<User?> GetByEmailWithProfileAsync(string email, CancellationToken cancellationToken = default)
Parameters
emailstringThe email address of the user to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<User>
The user entity with Profile, Educations, Experiences, UserBuilderTags, and UserSkills loaded, or null if not found.
Remarks
This method is similar to GetByIdAsync(Guid, CancellationToken) but queries by email instead of ID. Uses eager loading to include all related entities for complete profile information.
GetByIdAsNoTrackingAsync(Guid, CancellationToken)
Retrieves a user by their unique identifier without tracking (AsNoTracking).
public Task<User?> GetByIdAsNoTrackingAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe unique identifier of the user to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<User>
The user entity with all related entities loaded, but not tracked by EF Core, or null if not found.
Remarks
This method is useful for reading fresh data after SaveChanges when you need to ensure navigation properties reflect the latest database state without EF Core change tracking interference. Uses the same eager loading as GetByIdAsync but with AsNoTracking().
GetByIdAsync(Guid, CancellationToken)
Retrieves a user by their unique identifier with all related entities loaded.
public Task<User?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
Parameters
idGuidThe unique identifier of the user to retrieve.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
- Task<User>
The user entity with Profile, Educations, Experiences, UserBuilderTags, and UserSkills loaded, or null if not found.
Remarks
Uses eager loading to include:
- Profile (one-to-one relationship)
- Educations collection
- Experiences collection
- UserBuilderTags with BuilderTag navigation property
- UserSkills with Skill navigation property
GetSearchUsersCountAsync(string?, UserCategory?, List<Guid>?, CancellationToken)
Gets the total count of users matching the specified search criteria.
public Task<int> GetSearchUsersCountAsync(string? text, UserCategory? category, List<Guid>? skillIds, CancellationToken cancellationToken = default)
Parameters
textstringOptional search text for filtering users by name or profile summary.
categoryUserCategory?Optional user category filter (e.g., SoftwareEngineer, Designer).
skillIdsList<Guid>Optional list of skill IDs to filter users by.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
GetUsersAsync(int, int, string?, UserStatus?, UserRole?, CancellationToken)
Retrieves a paginated list of users with optional filtering by search term, status, and role.
public Task<List<User>> GetUsersAsync(int page, int pageSize, string? search = null, UserStatus? status = null, UserRole? role = null, CancellationToken cancellationToken = default)
Parameters
pageintThe page number (1-based) to retrieve.
pageSizeintThe number of users per page.
searchstringOptional search term to filter users by email or profile name (case-insensitive).
statusUserStatus?Optional status filter to include only users with the specified status.
roleUserRole?Optional role filter to include only users with the specified role.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
This method uses eager loading to include:
- Profile (for name search and display)
- Invitation (for invitation code display in admin views)
Search is performed on both email addresses and profile full names (case-insensitive contains match). Results are ordered by creation date in descending order (newest users first).
GetUsersByRolesAsync(List<UserRole>, CancellationToken)
Gets all active users whose role is in the specified list of roles. Used for sending bulk admin updates to specific user groups.
public Task<List<User>> GetUsersByRolesAsync(List<UserRole> roles, CancellationToken cancellationToken = default)
Parameters
rolesList<UserRole>List of user roles to filter by.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
This method is used by the admin bulk email feature to retrieve users by role for sending administrative updates. Only active users are returned.
GetUsersCountAsync(string?, UserStatus?, UserRole?, CancellationToken)
Gets the total count of users matching the specified filters.
public Task<int> GetUsersCountAsync(string? search = null, UserStatus? status = null, UserRole? role = null, CancellationToken cancellationToken = default)
Parameters
searchstringOptional search term to filter users by email or name.
statusUserStatus?Optional status filter (e.g., Active, Blocked).
roleUserRole?Optional role filter (e.g., User, Admin, Moderator).
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
SearchUsersAsync(string?, UserCategory?, List<Guid>?, int, int, CancellationToken)
Searches for users matching the specified criteria with pagination support.
public Task<List<User>> SearchUsersAsync(string? text, UserCategory? category, List<Guid>? skillIds, int page, int pageSize, CancellationToken cancellationToken = default)
Parameters
textstringOptional search text for filtering users by name or profile summary.
categoryUserCategory?Optional user category filter (e.g., SoftwareEngineer, Designer).
skillIdsList<Guid>Optional list of skill IDs to filter users by.
pageintThe page number (1-based) to retrieve.
pageSizeintThe number of users per page.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
UpdateAsync(User, CancellationToken)
Updates an existing user entity in the database.
public Task<User> UpdateAsync(User user, CancellationToken cancellationToken = default)
Parameters
userUserThe user entity to update. All properties are updated.
cancellationTokenCancellationTokenCancellation token to cancel the operation.
Returns
Remarks
This method marks the user as modified and saves changes. Related entities are not automatically updated - they must be managed separately if changes are needed.