# 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
  object
  MongoRepository<Unit>
  UnitRepository

Implements
  IUnitRepository
  IMongoRepository<Unit>

Inherited Members
  MongoRepository<Unit>.Db
  MongoRepository<Unit>.Collection
  MongoRepository<Unit>.InsertOneAsync(Unit)
  MongoRepository<Unit>.UpdateOneAsync(ObjectId, Unit)
  MongoRepository<Unit>.CreateIndexes()
  MongoRepository<Unit>.InsertInitialLoad()
  MongoRepository<Unit>.InsertManyAsync(List<Unit>)
  MongoRepository<Unit>.UpdateManyObjectIdAsync(string, ObjectId, ObjectId)
  MongoRepository<Unit>.DeleteAsync(string)
  MongoRepository<Unit>.CollectionExists(string)
  object.Equals(object)
  object.Equals(object, object)
  object.GetHashCode()
  object.GetType()
  object.MemberwiseClone()
  object.ReferenceEquals(object, object)
  object.ToString()

Extension Methods
  NumberUtils.IsNumber(object)
  NumberUtils.ToDouble(object)
  ObjectUtils.ToStr(object?)

## Constructors

### UnitRepository(IOptions<ApiSettings>, IMongoDatabase)

    public UnitRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)

#### Parameters

`apiSettings` IOptions<ApiSettings>
`database` IMongoDatabase

## 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

`id` ObjectId

  The MongoDB.Bson.ObjectId of the master list used to match units.

`masterListType` MasterListType

  The type of master list, which determines the property name used in the filter.

#### Returns

Task<long>

  The total number of Unit documents that match the filter.

### 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

`id` ObjectId

  The identifier of the unit to delete.

#### Returns

Task<Unit>

  The deleted Unit if found and removed; otherwise, `null` when an error occurs.

### 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

`id` object

  The identifier of the Unit to locate.

#### Returns

Task<Unit>

  A Unit instance if a match is found; otherwise, null.

### 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

`id` string

  The 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 `null` if 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

`id` ObjectId

  The 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

`id` ObjectId

  The unique identifier of the master list whose units should be retrieved.

`masterListType` MasterListType

  The 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

`unitName` string

  The 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

`filter` PaginationFilter

  The 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

`unit` Unit

  The Unit to be inserted into the collection.

#### Returns

Task<Unit>

  The inserted Unit as returned by the lookup by identifier, or `null` if 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

`unitIdParsed` ObjectId

  The MongoDB.Bson.ObjectId of the unit whose configuration will be updated.

`unitConfiguration` UnitConfiguration

  The new UnitConfiguration to apply to the unit.

#### Returns

Task<bool>

  A task that resolves to `true` when the update modified a document; otherwise `false` (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

`unit` Unit

  The unit containing the identifier and the new field values to be persisted.

#### Returns

Task<Unit>

  The updated unit after the operation, or null if no document matched the filter.

### 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

`unitId` ObjectId

  The unique identifier of the unit to update.

`name` string

  The new name to assign to the unit.

`title` string

  The new title to assign to the unit.

#### Returns

Task<Unit>

  The updated Unit after the modification, or `null` if no matching unit was found.

### 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

`updateUnitListDto` UpdateUnitIdListDto

  The DTO containing the target `UnitId` and the collection of master list entries to update.

#### Returns

Task<Unit>

  The updated Unit after the modifications, or `null` if no updates were performed.