# Class MedicineService

Namespace
  adas_core.Application.Services

Assembly
  adas-core.Application.dll

    public class MedicineService : IMedicineService

Inheritance
  object
  MedicineService

Implements
  IMedicineService

Inherited Members
  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

### MedicineService(IMedicineRepository, ITreatmentService, IOptions<ApiSettings>, ILogger<MedicineService>, IHttpContextAccessor, ILocalAuditService)

    public MedicineService(IMedicineRepository medicineRepository, ITreatmentService treatmentService, IOptions<ApiSettings> apiSettings, ILogger<MedicineService> logger, IHttpContextAccessor httpContextAccessor, ILocalAuditService auditService)

#### Parameters

`medicineRepository` IMedicineRepository
`treatmentService` ITreatmentService
`apiSettings` IOptions<ApiSettings>
`logger` ILogger<MedicineService>
`httpContextAccessor` IHttpContextAccessor
`auditService` ILocalAuditService

## Methods

### DeleteMedicineById(ObjectId)

Deletes a medicine identified by its unique identifier and records the operation in the audit log. Captures the existing medicine state prior to deletion to preserve audit trail details.

    public Task DeleteMedicineById(ObjectId medicineId)

#### Parameters

`medicineId` ObjectId

  The unique identifier of the medicine to delete.

#### Returns

Task

### GetActiveMedicinesByPatient(ObjectId)

Retrieves all active medicines associated with a patient by first resolving their active treatments and then collecting the medicines prescribed within them.

    public Task<IEnumerable<Medicine>> GetActiveMedicinesByPatient(ObjectId patientId)

#### Parameters

`patientId` ObjectId

  The unique identifier of the patient whose active medicines are being queried.

#### Returns

Task<IEnumerable<Medicine>>

  A task that represents the asynchronous operation, containing an enumerable collection of Medicine instances linked to the patient's active treatments.

### GetAll()

Retrieves all medicines from the repository asynchronously.

    public Task<List<Medicine>> GetAll()

#### Returns

Task<List<Medicine>>

  A task representing the asynchronous operation, containing a list of all Medicine entities.

### GetAllCodes()

Asynchronously retrieves all distinct code values from the medicine repository. Returns an empty list if an exception occurs during retrieval.

    public Task<List<string>> GetAllCodes()

#### Returns

Task<List<string>>

  A task representing the asynchronous operation, containing a list of code strings, or an empty list if an error occurs.

### GetAllGroups()

Asynchronously retrieves all distinct "group" values from the medicine repository and returns them as a list of strings. Returns an empty list if an error occurs while fetching or converting the data.

    public Task<List<string>> GetAllGroups()

#### Returns

Task<List<string>>

  A task that represents the asynchronous operation, containing a list of distinct group strings, or an empty list if an exception is encountered.

### GetAllNames()

Retrieves all distinct medicine names from the repository asynchronously, returning an empty list if any error occurs during the data retrieval or mapping process.

    public Task<List<string>> GetAllNames()

#### Returns

Task<List<string>>

  A task that represents the asynchronous operation. The task result contains a list of medicine names, or an empty list if the operation fails.

### GetAllTypes()

Asynchronously retrieves all distinct medicine types from the repository. Returns an empty list if an exception occurs during the retrieval process.

    public Task<List<string>> GetAllTypes()

#### Returns

Task<List<string>>

  A task representing the asynchronous operation, containing a list of distinct medicine type strings, or an empty list if an error occurs.

### GetByCode(string)

Retrieves a medicine from the repository by its unique code, returning null if no matching medicine is found.

    public Task<Medicine?> GetByCode(string code)

#### Parameters

`code` string

  The unique code identifier of the medicine to retrieve.

#### Returns

Task<Medicine>

  A Medicine instance if a medicine with the specified code exists; otherwise, null.

### GetByCodeOrNote(List<string>)

Retrieves a list of medicines that match the provided codes or notes by delegating to the medicine repository.

    public Task<List<Medicine>> GetByCodeOrNote(List<string> codeNote)

#### Parameters

`codeNote` List<string>

  A list of strings representing the codes or notes used to search for matching medicines.

#### Returns

Task<List<Medicine>>

  A task that represents the asynchronous operation, containing the list of Medicine objects that match the specified codes or notes.

### GetByName(string)

Retrieves a Medicine from the repository by its name. Returns `null` when no matching medicine is found.

    public Task<Medicine?> GetByName(string name)

#### Parameters

`name` string

  The name of the medicine to look up.

#### Returns

Task<Medicine>

  A Medicine if a match is found; otherwise, `null`.

### GetMedicineById(ObjectId)

Retrieves a medicine by its unique identifier from the repository. Throws a NotFoundException when no medicine is found matching the provided identifier.

    public Task<Medicine?> GetMedicineById(ObjectId medicineId)

#### Parameters

`medicineId` ObjectId

  The unique identifier of the medicine to retrieve.

#### Returns

Task<Medicine>

  The Medicine entity if found; otherwise, the method throws an exception.

#### Exceptions

NotFoundException

  Thrown when no medicine is found for the specified `medicineId`.

### GetMedicinesOfTreatments(IEnumerable<PatientTreatment?>)

Retrieves the medicines associated with a collection of patient treatments, resolving each treatment's requested give codes against the medicine repository. Falls back to parental nutrition calculation when no medicine is found, and to note-based detection when notes indicate medication; medicines of type "Nutrition" returned by the repository are excluded except when produced through the parental nutrition path.

    public Task<IEnumerable<Medicine>> GetMedicinesOfTreatments(IEnumerable<PatientTreatment?> treatments)

#### Parameters

`treatments` IEnumerable<PatientTreatment>

  The patient treatments to resolve medicines for. Null entries are skipped.

#### Returns

Task<IEnumerable<Medicine>>

  An enumerable of medicines resolved from the supplied treatments, aggregating types, codes, groups, and notes when a treatment specifies a `RequestedGiveTreatment` name.

### GetPaginatedMedicines(PaginationFilter)

Retrieves a paginated collection of medicines based on the specified pagination filter, including the total document count for the current query.

    public Task<PaginationResponse<Medicine>> GetPaginatedMedicines(PaginationFilter filter)

#### Parameters

`filter` PaginationFilter

  The pagination filter containing the page number and page size used to determine which subset of medicines to return.

#### Returns

Task<PaginationResponse<Medicine>>

  A task that represents the asynchronous operation. The task result contains a PaginationResponse<T> with the medicines for the requested page, the current page number, the page size, and the total count of medicines matching the query.

### PostMedicine(Medicine)

Creates a new medicine record in the repository and records an audit log entry for the operation. Throws a conflict exception when the repository fails to produce a result.

    public Task<Medicine?> PostMedicine(Medicine medicine)

#### Parameters

`medicine` Medicine

  The medicine entity to create.

#### Returns

Task<Medicine>

  The newly created Medicine entity.

#### Exceptions

ConflictException

  Thrown when the repository returns a null result, indicating the creation failed.

### UpdateMedicine(Medicine)

Updates an existing medicine record and records the change in the audit log.

    public Task<Medicine?> UpdateMedicine(Medicine medicine)

#### Parameters

`medicine` Medicine

  The medicine entity containing the updated information.

#### Returns

Task<Medicine>

  The updated Medicine, or `null` if the update did not produce a result.