Building a Simple Hospital Management Service with Ballerina
In today’s healthcare sector, managing patient information efficiently is critical. To address this, we can build a simple hospital management service using Ballerina, a cloud-native programming language designed to simplify the creation of services and APIs. In this article, we’ll walk through how to create a RESTful API using Ballerina to manage patient data in a hospital setting.
What You Will Learn
Setting up a Ballerina project.
Creating RESTful services for managing patient data.
Testing the service with sample data.
Why Ballerina?
Ballerina is an ideal choice for developing services due to its seamless support for handling HTTP, JSON, and REST-based APIs. Its intuitive syntax and in-built networking capabilities make it easy to quickly build scalable services.
Step 1: Setting up Ballerina
Before we start building the hospital management service, you need to have Ballerina installed on your machine. You can download it from ballerina.io, and follow the instructions for your operating system.
Once installed, verify the installation by running the following command in your terminal,
bal version
Step 2: Creating a New Ballerina Project
Let’s begin by setting up the project. Open a terminal and create a new Ballerina project using the following command,
bal new hospital-service
cd hospital-service
This creates a new directory named hospital-service with the necessary project structure.
Step 3: Defining the Hospital Service
In this service, we’ll implement basic operations such as adding a new patient, viewing a patient by ID, and listing all patients. For simplicity, we will use an in-memory data store.
Create the main.bal File
import ballerina/http;
type Patient record {
int id;
string name;
int age;
string disease;
};
map<Patient> patients = {};
int patientIdCounter = 1;
service /hospital on new http:Listener(8080) {
// Add a new patient
resource function post patients(http:Caller caller, http:Request req) returns error? {
json payload = check req.getJsonPayload();
Patient newPatient = check payload.cloneWithType(Patient);
newPatient.id = patientIdCounter;
patients[patientIdCounter.toString()] = newPatient;
patientIdCounter += 1;
check caller->respond({message: "Patient added successfully", id: newPatient.id});
}
// View patient by ID
resource function get patients/[int id](http:Caller caller, http:Request req) returns error? {
Patient? patient = patients[id.toString()];
if patient is Patient {
check caller->respond(patient);
} else {
check caller->respond({message: "Patient not found"});
}
}
// List all patients
resource function get patients(http:Caller caller, http:Request req) returns error? {
Patient[] patientList = [];
foreach var patient in patients {
patientList.push(patient);
}
check caller->respond(patientList);
}
}
Code Breakdown:
Data Type Definition:
The Patient record defines the structure for patient information (ID, name, age, disease).
In-Memory Data Store:
A map<Patient> is used as a simple in-memory database to store patient details.
Patient Operations:
POST /patients: Adds a new patient.
GET /patients/[id]: Fetches a patient’s details by their ID.
GET /patients: Lists all registered patients.
Step 4: Running the Service
After writing the service, you can run it using the following command from the hospital-service directory,
bal run
This will start the service on port 8080. You can access the hospital management API locally at http://localhost:8080/hospital.
Step 5: Testing the Service
To test the hospital service, you can use curl or Postman to make HTTP requests to the API.
Adding a New Patient
You can add a patient using the following curl command,
curl --location 'http://localhost:8080/hospital/patients' \
--header 'Content-Type: application/json' \
--data '{"id": 1, "name": "John Doe", "age": 35, "disease": "Flu"}'
This will create a new patient with the provided details. You should get a response like,
{
"message": "Patient added successfully",
"id": 1
}
Viewing a Patient by ID
To retrieve details of a specific patient (for example, patient with ID 1), use,
curl http://localhost:8080/hospital/patients/1
The response will be something like,
{
"id": 1,
"name": "John Doe",
"age": 35,
"disease": "Flu"
}
Listing All Patients
To list all registered patients, use,
curl http://localhost:8080/hospital/patients
The response will contain a list of all patients stored in the system.
Conclusion
In this article, we have built a simple RESTful hospital management service using Ballerina. This service allows us to perform operations like adding patients, viewing patient details, and listing all patients. Ballerina’s simplicity and ease of use for building cloud-native services makes it an excellent choice for such applications.
Next Steps
Explore Ballerina’s powerful in-built connectors for database access.
Add security features, such as authentication and authorization.
Scale your service by deploying it in a cloud-native environment using Docker or Kubernetes.