Class MedicineRepository
- Namespace
- adas_core.Infrastructure.Repositories
- Assembly
- adas-core.Infrastructure.dll
Repository implementation for managing Medicine entities in MongoDB. Provides CRUD operations, search, pagination, and aggregation capabilities specific to medicines.
public class MedicineRepository : MongoRepository<Medicine>, IMedicineRepository, IMongoRepository<Medicine>
- Inheritance
-
MedicineRepository
- Implements
- Inherited Members
- Extension Methods
Constructors
MedicineRepository(IOptions<ApiSettings>, IMongoDatabase)
Initializes a new instance of the MedicineRepository class.
public MedicineRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database)
Parameters
apiSettingsIOptions<ApiSettings>The application API settings containing configuration values, including the collection name. Cannot be null.
databaseIMongoDatabaseThe MongoDB database instance used to access the collection.
Exceptions
- ArgumentNullException
Thrown when
apiSettingsis null.
Methods
DeleteMedicineById(ObjectId)
Asynchronously deletes a medicine from the database by its unique identifier.
public Task DeleteMedicineById(ObjectId medicineId)
Parameters
medicineIdObjectIdThe MongoDB.Bson.ObjectId of the medicine to delete.
Returns
GetAll()
Asynchronously retrieves all medicines stored in the collection.
public Task<List<Medicine>> GetAll()
Returns
- Task<List<Medicine>>
A Task<TResult> representing the asynchronous operation. The task result contains a list of all Medicine objects. Returns an empty list if the collection is empty.
GetAllGroups()
Retrieves all distinct medicine groups available in the collection.
public Task<List<string>> GetAllGroups()
Returns
- Task<List<string>>
A Task<TResult> representing the asynchronous operation, containing a list of distinct group names as strings.
Exceptions
- NotImplementedException
This method is not yet implemented.
GetCollectionName()
Gets the name of the MongoDB collection used to store medicines. Falls back to the default "medicines" collection name when not configured in the API settings.
public override string GetCollectionName()
Returns
- string
The collection name retrieved from the API settings, or "medicines" if not configured.
GetDistinctFieldDataQuery(string)
Builds an aggregation pipeline that retrieves the distinct values of a specified array field from all medicines. The pipeline unwinds the field, groups by its value, sorts alphabetically, and projects the result with the original field name.
public IAggregateFluent<BsonDocument> GetDistinctFieldDataQuery(string field)
Parameters
fieldstringThe name of the array field on the Medicine document to retrieve distinct values for (for example, "Codes", "Type", or "Group").
Returns
- IAggregateFluent<BsonDocument>
An MongoDB.Driver.IAggregateFluent<TResult> representing the aggregation pipeline that, when executed, yields documents containing the distinct values of the specified field.
GetMedicine(string)
Asynchronously retrieves a medicine whose Codes or Notes collection contains the specified code.
public Task<Medicine?> GetMedicine(string code)
Parameters
codestringThe code or note text to search for within the medicine's codes or notes.
Returns
- Task<Medicine>
A Task<TResult> representing the asynchronous operation. The task result contains the first Medicine matching the criteria, or null if no match is found.
GetMedicineByCodeOrNote(List<string>)
Asynchronously retrieves all medicines whose Codes array contains any of the specified code values.
public Task<List<Medicine>> GetMedicineByCodeOrNote(List<string> codeNotes)
Parameters
codeNotesList<string>A list of code strings to match against the medicine's codes. At least one code must match.
Returns
- Task<List<Medicine>>
A Task<TResult> representing the asynchronous operation. The task result contains a list of matching Medicine objects. Returns an empty list if no matches are found.
GetMedicineById(ObjectId)
Asynchronously retrieves a medicine by its unique identifier.
public Task<Medicine?> GetMedicineById(ObjectId medicineId)
Parameters
medicineIdObjectIdThe MongoDB.Bson.ObjectId of the medicine to retrieve.
Returns
- Task<Medicine>
A Task<TResult> representing the asynchronous operation. The task result contains the Medicine if found; otherwise, null.
GetMedicineByName(string)
Asynchronously retrieves a medicine by its exact name.
public Task<Medicine?> GetMedicineByName(string name)
Parameters
namestringThe exact name of the medicine to search for.
Returns
- Task<Medicine>
A Task<TResult> representing the asynchronous operation. The task result contains the Medicine if found; otherwise, null.
GetPaginatedMedicines(PaginationFilter)
Retrieves a paginated, sorted, and filtered set of medicines based on the provided pagination filter. Results are sorted ascending by name. Supports optional case-insensitive regex match on the medicine name, and exact matches on code, type, and group.
public IFindFluent<Medicine, Medicine> GetPaginatedMedicines(PaginationFilter filter)
Parameters
filterPaginationFilterThe PaginationFilter containing pagination and filtering criteria.
Returns
- IFindFluent<Medicine, Medicine>
An MongoDB.Driver.IFindFluent<TDocument, TProjection> instance that can be used to further refine and execute the query. When no filters are provided, all medicines are returned sorted by name.
PostMedicine(Medicine)
Asynchronously inserts a new medicine into the database and returns the inserted entity (looked up by name).
public Task<Medicine?> PostMedicine(Medicine medicine)
Parameters
Returns
- Task<Medicine>
A Task<TResult> representing the asynchronous operation. The task result contains the newly inserted Medicine retrieved by its name, or null if the lookup fails.
UpdateMedicine(Medicine)
Asynchronously updates an existing medicine in the database and returns the updated entity.
public Task<Medicine?> UpdateMedicine(Medicine medicine)
Parameters
medicineMedicineThe Medicine instance containing the updated values. The MongoDB.Bson.ObjectId is used to identify the document.
Returns
- Task<Medicine>
A Task<TResult> representing the asynchronous operation. The task result contains the same Medicine instance that was passed in, after the update operation has been issued.