Closed aleeusgr closed 2 months ago
Here is an example of how you can add an HTML page to get a patient by ID:
First, create a new HTML file get-patient.html
in the resources/public
directory:
<!-- resources/public/get-patient.html -->
<!DOCTYPE html>
<html>
<head>
<title>Get Patient by ID</title>
</head>
<body>
<h1>Get Patient by ID</h1>
<form action="/patients/get" method="get">
<label for="id">Patient ID:</label>
<input type="number" id="id" name="id" required>
<button type="submit">Get Patient</button>
</form>
<div id="patient-info"></div>
<script>
const form = document.querySelector('form');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const id = document.querySelector('#id').value;
const response = await fetch(`/patients/get?id=${id}`);
const patient = await response.json();
const patientInfo = document.querySelector('#patient-info');
patientInfo.innerHTML = `
<p>Name: ${patient.name}</p>
<p>Sex: ${patient.sex}</p>
<p>Date of Birth: ${patient.date_of_birth}</p>
<p>Address: ${patient.address}</p>
<p>Social Security Number: ${patient.social_security_number}</p>
`;
});
</script>
</body>
</html>
This HTML page has a form that takes a patient ID as input and submits a GET request to /patients/get
with the ID as a query parameter. The JavaScript code sends the request, parses the response as JSON, and updates the page with the patient's information.
Next, add a new route to your hs-clojure.patients
namespace to handle the GET request:
; src/hs_clojure/patients.clj
(defroutes patient-routes
(GET "/get" [id]
(let [patient (get-patient-by-id db/spec (Integer/parseInt id))]
(if patient
(ring.util.response/response (clojure.data.json/json-str patient))
(ring.util.response/not-found "Patient not found")))))
This route takes the id
parameter from the query string, calls the get-patient-by-id
function to retrieve the patient, and returns the patient's information as JSON if found, or a 404 response if not found.
Finally, add the new route to your hs-clojure.handler
namespace:
; src/hs_clojure/handler.clj
(defroutes app-routes
...
(route/resources "/")
(GET "/" [] (ring.util.response/redirect "/index.html"))
(route/not-found "Not Found")
patient-routes)
Now, when you navigate to http://localhost:3000/get-patient.html
in your browser, you can enter a patient ID and submit the form to retrieve the patient's information.
Congratulations on setting up a basic CRUD API in Clojure! Here are some next steps to consider:
hs_clojure.patients
, but you may want to add error handling and logging for database operations.try
-catch
block or libraries like clj-http
for handling errors.ring-anti-forgery
or clj-http
for authentication and authorization.clojure.test
or midje
. This will help you ensure your API functions correctly and catch any bugs early on.By following these steps, you'll be able to create a robust and maintainable CRUD API in Clojure. Good luck!
version alpha:
closes #4 closes #9 close #18
User testing:
$ ./start_db.sh
$ lein ring server
opens the index page.Acceptance: