Class UnitRepository
- Namespace
- adas_core.Infrastructure.Repositories
- Assembly
- adas-core.Infrastructure.dll
Represents a MongoDB-backed repository for Unit entities, inheriting persistence functionality from MongoRepository<T> and implementing the IUnitRepository contract.
public class UnitRepository : MongoRepository<Unit>, IUnitRepository, IMongoRepository<Unit>
- Inheritance
-
UnitRepository
- Implements
- Inherited Members
- Extension Methods
Constructors
UnitRepository(IOptions<ApiSettings>, IMongoDatabase)
public UnitRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)
Parameters
apiSettingsIOptions<ApiSettings>databaseIMongoDatabase
Methods
CountUnitsByMasterListId(ObjectId, MasterListType)
Asynchronously counts the number of Unit documents associated with the specified master list.
The filter property is dynamically constructed based on the masterListType (e.g., "{Type}Id"), so the matching field depends on the provided master list type.
public Task<long> CountUnitsByMasterListId(ObjectId id, MasterListType masterListType)
Parameters
idObjectIdThe MongoDB.Bson.ObjectId of the master list used to match units.
masterListTypeMasterListTypeThe type of master list, which determines the property name used in the filter.
Returns
DeleteAsync(ObjectId)
Deletes the unit identified by the specified identifier, returning the deleted entity. If the operation fails, the exception is logged and null is returned.
public Task<Unit?> DeleteAsync(ObjectId id)
Parameters
idObjectIdThe identifier of the unit to delete.
Returns
FindById(object)
Asynchronously finds a Unit by its identifier in the underlying collection. Returns null when no matching document is found.
public Task<Unit?> FindById(object id)
Parameters
Returns
FindById(string)
Asynchronously retrieves a Unit by its unique identifier, returning null when no matching unit is found.
public Task<Unit?> FindById(string id)
Parameters
idstringThe unique identifier of the unit to look up.
Returns
- Task<Unit>
A task that represents the asynchronous operation. The task result contains the matching Unit, or
nullif no unit is found with the specified identifier.
Exceptions
- NotImplementedException
The method is not yet implemented.
FindByMasterListId(ObjectId)
Retrieves all Unit documents that reference the specified master list identifier in any of their associated list properties (e.g., doctor, allergy, destination, diagnosis, procedure, test, service, treatment, language barrier, and similar lists). Uses an OR-based filter to match the identifier against every list id field, returning matching units; if an error occurs during the query, an empty collection is returned after logging the exception.
public Task<IEnumerable<Unit>> FindByMasterListId(ObjectId id)
Parameters
idObjectIdThe MongoDB.Bson.ObjectId of the master list to search for across the unit's list reference fields.
Returns
- Task<IEnumerable<Unit>>
A task that yields an IEnumerable<T> containing the matching units, or an empty list if no matches are found or an error occurs.
FindByMasterListId(ObjectId, MasterListType)
Retrieves the collection of units associated with the specified master list identifier and master list type.
public Task<IEnumerable<Unit>> FindByMasterListId(ObjectId id, MasterListType masterListType)
Parameters
idObjectIdThe unique identifier of the master list whose units should be retrieved.
masterListTypeMasterListTypeThe type of the master list used to filter or scope the unit lookup.
Returns
- Task<IEnumerable<Unit>>
A task that represents the asynchronous operation, containing the collection of units linked to the specified master list.
FindByName(string)
Asynchronously finds and returns a Unit matching the specified name from the collection, or null if no matching document is found.
public Task<Unit?> FindByName(string unitName)
Parameters
unitNamestringThe name of the unit to look up using an exact equality match.
Returns
- Task<Unit>
A Task<TResult> containing the matching Unit if found; otherwise,
null.
GetAll()
Asynchronously retrieves all Unit records from the underlying collection.
public Task<List<Unit>> GetAll()
Returns
- Task<List<Unit>>
A task that represents the asynchronous operation, containing a list of all Unit documents found in the collection.
GetCollectionName()
Gets the collection name for units, returning the configured value from API settings or falling back to the default "units" when not specified.
public override string GetCollectionName()
Returns
- string
The collection name for units, or "units" if no custom value is configured in the API settings.
GetPaginatedUnits(PaginationFilter)
Retrieves a paginated, sortable query of Unit documents, optionally filtered by a case-insensitive text search applied to the Name and Title fields.
public IFindFluent<Unit, Unit> GetPaginatedUnits(PaginationFilter filter)
Parameters
filterPaginationFilterThe pagination filter containing the optional text search criteria used to narrow the result set.
Returns
- IFindFluent<Unit, Unit>
An MongoDB.Driver.IFindFluent<TDocument, TProjection> representing the sorted and filtered query of units; if no filter request is supplied, an unfiltered query sorted by title is returned.
InsertOneUnit(Unit)
Inserts a new Unit into the underlying collection and returns the persisted instance retrieved by its identifier.
If the insertion fails, the error is logged and the method returns null as a fallback.
public Task<Unit?> InsertOneUnit(Unit unit)
Parameters
Returns
- Task<Unit>
The inserted Unit as returned by the lookup by identifier, or
nullif an exception occurred during insertion.
UpdateConfiguration(ObjectId, UnitConfiguration)
Asynchronously updates the configuration of an existing unit identified by its ID.
Returns true if the document was modified, or false if the update failed or no document matched.
public Task<bool> UpdateConfiguration(ObjectId unitIdParsed, UnitConfiguration unitConfiguration)
Parameters
unitIdParsedObjectIdThe MongoDB.Bson.ObjectId of the unit whose configuration will be updated.
unitConfigurationUnitConfigurationThe new UnitConfiguration to apply to the unit.
Returns
- Task<bool>
A task that resolves to
truewhen the update modified a document; otherwisefalse(including when the operation throws and the error is logged).
UpdateUnit(Unit)
Updates an existing unit document in the collection, returning the updated document. Uses a filter on the unit's identifier and sets the updatable fields; returns null when no matching document is found.
public Task<Unit?> UpdateUnit(Unit unit)
Parameters
unitUnitThe unit containing the identifier and the new field values to be persisted.
Returns
UpdateUnitInfo(ObjectId, string, string)
Updates the Name and Title fields of the unit identified by unitId.
Returns the updated unit document, or null if no unit with the specified id exists in the collection.
public Task<Unit?> UpdateUnitInfo(ObjectId unitId, string name, string title)
Parameters
unitIdObjectIdThe unique identifier of the unit to update.
namestringThe new name to assign to the unit.
titlestringThe new title to assign to the unit.
Returns
UpdateUnitMasterList(UpdateUnitIdListDto)
Updates the master list references on a Unit document identified by the supplied id,
applying the provided MasterListId values to the appropriate fields based on each entry's
MasterListType. Returns the updated Unit when at least one update is applied,
or null when no valid master list entries are provided.
public Task<Unit?> UpdateUnitMasterList(UpdateUnitIdListDto updateUnitListDto)
Parameters
updateUnitListDtoUpdateUnitIdListDtoThe DTO containing the target
UnitIdand the collection of master list entries to update.