Class LocalLoginRepository
- Namespace
- adas_core.LocalLogin
- Assembly
- adas-core.LocalLogin.dll
Repository for handling user data in the local login system. This repository provides methods for inserting, updating, and retrieving user information from a MongoDB collection. It also ensures that a system user is created during the initial load if it does not already exist.
public class LocalLoginRepository : MongoRepository<User>, ILocalLoginRepository, IMongoRepository<User>
- Inheritance
-
LocalLoginRepository
- Implements
- Inherited Members
- Extension Methods
Constructors
LocalLoginRepository(IOptions<ApiSettings>, IMongoDatabase)
Constructor for the LocalLoginRepository. It initializes the repository with the provided API settings and MongoDB database. The API settings are used to determine the collection name for user data, and the database is passed to the base MongoRepository class for handling database operations.
public LocalLoginRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)
Parameters
apiSettingsIOptions<ApiSettings>The API settings containing configuration for the MongoDB collection name and other related settings.
databaseIMongoDatabaseThe MongoDB database instance used for database operations.
Methods
GetAll()
Retrieves all users from the MongoDB collection. This method executes a query to find all documents in the collection and returns a list of user objects.
public Task<List<User>> GetAll()
Returns
GetByEmail(string)
Retrieves a user from the MongoDB collection by their email address. This method constructs a filter to find the user by their email and returns the first matching user if found; otherwise, it returns null.
public Task<User?> GetByEmail(string email)
Parameters
emailstringThe email address of the user to retrieve.
Returns
GetById(ObjectId)
Retrieves a user from the MongoDB collection by their unique identifier (ObjectId). This method constructs a filter to find the user by their ID and returns the first matching user if found; otherwise, it returns null.
public Task<User?> GetById(ObjectId id)
Parameters
idObjectIdThe unique identifier of the user to retrieve.
Returns
GetByUsername(string)
Retrieves a user from the MongoDB collection by their username. This method constructs a filter to find the user by their username and returns the first matching user if found; otherwise, it returns null.
public Task<User?> GetByUsername(string username)
Parameters
usernamestringThe username of the user to retrieve.
Returns
GetCollectionName()
Gets the name of the MongoDB collection for user data. This method overrides the base method from MongoRepository and returns the collection name specified in the API settings.
public override string GetCollectionName()
Returns
- string
The name of the MongoDB collection for user data.
InsertInitialLoad()
Inserts initial load data into the MongoDB collection. This method is called during the initial setup of the repository to ensure that a system user is created if it does not already exist. It calls the GetOrCreateSystemUser method to check for the existence of the system user and insert it if necessary.
public override sealed Task InsertInitialLoad()
Returns
InsertUserOneAsync(User)
Inserts a new user into the MongoDB collection. This method takes a User object as input and attempts to insert it into the collection. If the insertion is successful, it returns true; otherwise, it logs the error and returns false.
public Task<bool> InsertUserOneAsync(User user)
Parameters
userUserThe user object to insert into the collection.
Returns
UpdateOneUserAsync(ObjectId, User)
Updates an existing user in the MongoDB collection. This method takes an ObjectId representing the user's ID and a User object containing the updated information. It constructs a filter to find the user by ID and an update definition to set the new values for the user's properties. If the update is successful, it returns true; otherwise, it logs the error and returns false.
public Task<bool> UpdateOneUserAsync(ObjectId id, User user)
Parameters
idObjectIdThe unique identifier of the user to update.
userUserThe user object containing the updated information.