aleeusgr / hs-clojure

Other
0 stars 0 forks source link

pre-alpha #16

Closed aleeusgr closed 2 months ago

aleeusgr commented 3 months ago

closes #4 closes #9 close #18

User testing:

  1. start the db using $ ./start_db.sh
  2. $ lein ring server opens the index page.
  3. List all: http://localhost:3000/patients
  4. Search: http://localhost:3000/patients/search?social-security-number=123-4323
  5. Filter http://localhost:3000/patients/sex/Female

Acceptance:

aleeusgr commented 3 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.

aleeusgr commented 3 months ago

Congratulations on setting up a basic CRUD API in Clojure! Here are some next steps to consider:

  1. Database setup: Make sure your database is properly configured and connected to your application. You've already defined the database functions in hs_clojure.patients, but you may want to add error handling and logging for database operations.
  2. API documentation: Consider generating API documentation using tools like Swagger or ReDoc. This will help you and others understand the API's endpoints, parameters, and response formats.
  3. Error handling: Implement proper error handling for your API endpoints. This includes handling invalid input, database errors, and other exceptions. You can use Clojure's built-in try-catch block or libraries like clj-http for handling errors.
  4. Input validation: https://github.com/aleeusgr/hs-clojure/issues/17
  5. Security: Consider implementing security measures like authentication and authorization for your API. You can use libraries like ring-anti-forgery or clj-http for authentication and authorization.
  6. Testing: Write unit tests for your API endpoints using a testing framework like clojure.test or midje. This will help you ensure your API functions correctly and catch any bugs early on.
  7. Deployment: Once your API is stable, consider deploying it to a production environment. You can use a web server like Apache or Nginx, or a cloud platform like Heroku or AWS.
  8. Monitoring and logging: Set up monitoring and logging tools to track API performance, errors, and usage. This will help you identify issues and optimize your API for better performance.
  9. API documentation and client libraries: Consider creating client libraries for your API in popular programming languages like JavaScript, Python, or Java. This will make it easier for developers to integrate your API into their applications.
  10. Maintenance and updates: Regularly update your API to fix bugs, add new features, and improve performance. This will ensure your API remains stable and reliable for your users.

By following these steps, you'll be able to create a robust and maintainable CRUD API in Clojure. Good luck!

aleeusgr commented 3 months ago

version alpha:

2

1