0% found this document useful (0 votes)
19 views3 pages

Service 1

The MedicationServices class provides methods for managing medication records in a healthcare database. It includes functionalities to retrieve medications by various criteria such as patient ID, patient name, and disease name, as well as to add new medications. Error handling is implemented to return appropriate messages when no medications are found or when invalid data is entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Service 1

The MedicationServices class provides methods for managing medication records in a healthcare database. It includes functionalities to retrieve medications by various criteria such as patient ID, patient name, and disease name, as well as to add new medications. Error handling is implemented to return appropriate messages when no medications are found or when invalid data is entered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using HealthCare.

Data;
using HealthCare.Domain;
using HealthCare.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HealthCare.Services.Services
{
public class MedicationServices : IMedicationServices
{
private HealthCareDbContext _dbContext;
public MedicationServices(HealthCareDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task<object> GetMedications()


{
try
{
List<Medication> resMedications = await _dbContext.Medications
.Include(med => med.Patient)
.ToListAsync();
if (resMedications.Count == 0) { return "No Medications found"; }
return resMedications;
}
catch (Exception ex)
{
return "No Medications found";
}
}

public async Task<object> GetMedications(int patientId)


{
try
{
List<Medication> resMedications = _dbContext.Medications
.Include(med => med.Patient)
.Where(med => med.PatientId == patientId)
.ToList();
if (resMedications.Count == 0) { return $"No Medications with
patient id {patientId}"; }
return resMedications;
}
catch (Exception ex)
{
return "No Medications";
}
}

public async Task<object> GetMedicationsByPatientName(string patientName)


{
try
{
List<Medication> resMedications = await _dbContext.Medications
.Include(med => med.Patient)
.Where(med => med.Patient.Name.Contains(patientName))
.ToListAsync();
if (resMedications.Count == 0) { return $"No Medications with
patient name {patientName}"; }
return resMedications;
}
catch (Exception ex)
{
return "No Medications";
}
}

public async Task<object> GetMedications(string patientType)


{
try
{
List<Medication> resMedications = _dbContext.Medications
.Include(med => med.Patient)
.Where(med => med.PatientType.Contains(patientType))
.ToList();
if (resMedications.Count == 0) { return $"No Medications with
patient type {patientType}"; }
return resMedications;
}
catch (Exception ex)
{
return "No Medications";
}
}

public async Task<object> AddMedication(Medication medication)


{
try
{
_dbContext.Medications.Add(medication);
_dbContext.SaveChanges();
return "Medication added successfully!!";
}
catch
{
return "Enter valid data";
}
}

public async Task<object> GetMedicationByDisease(string diseaseName)


{
try
{
List<Medication> resMedication = _dbContext.Medications
.Include(med => med.Patient)
.Where(med => med.DiseaseIdentified.Contains(diseaseName))
.ToList();
if (resMedication.Count == 0) { return $"No medications for Disease
{diseaseName}"; }
return resMedication;
}
catch (Exception ex)
{
return "Enter Valid disease name";
}
}
}
}

You might also like