Skip to content

Instantly share code, notes, and snippets.

@f4ww4z
Last active January 15, 2020 11:08
Show Gist options
  • Save f4ww4z/1033d9a55cf7afe090a87ddafef26b39 to your computer and use it in GitHub Desktop.
Save f4ww4z/1033d9a55cf7afe090a87ddafef26b39 to your computer and use it in GitHub Desktop.
GoLang-2 task files for GCI 2019 with OpenMRS
CREATE TABLE hospital
(
id SERIAL PRIMARY KEY,
name VARCHAR(100),
max_patient_amount INT
);
CREATE TABLE location
(
id SERIAL PRIMARY KEY,
name VARCHAR(100),
hospital_id INT,
FOREIGN KEY (hospital_id) REFERENCES hospital (id)
);
CREATE TABLE patient
(
id SERIAL PRIMARY KEY,
name VARCHAR(100),
illness VARCHAR(200),
birth_date DATE,
location_id INT NOT NULL,
FOREIGN KEY (location_id) REFERENCES location (id)
);

GoLang-2: Connect to Database

NOTE: You should have completed GoLang-1.

Setup

  1. Create an account on ElephantSQL (use your GitHub or gmail account).
  2. Create a new PostgresSQL instance, and name appropriately. Screenshot
  3. Go to the newly created instance. Take note of the URL.
  4. Download and install PostgreSQL.
  5. Use GoLand IDE if possible. If not, no problem, install PgAdmin4 - will be used for connecting to the cloud db instance.
  6. In your repository, get sqlx package by running go get github.com/jmoiron/sqlx
  7. Get the sql driver by running go get github.com/lib/pq

Task

  1. In your go repository, create folder scripts. Inside it, add this PostgresSQL schema file.
  2. Connect to your db instance. Use GoLand or PgAdmin4 as mentioned above. See this tutorial.
  3. Run the script using PgAdmin's Query Tool.
  4. Insert 2 hospitals in hospital table.
  5. Insert at least 4 locations in location table (can use locations shown here). 2 locations for each hospital.
  6. Insert 9 patients in patient table, with at least 1 patient in each location.
  7. In main.go, create a function initDatabase() which initializes a global variable db *sql.DB . Hint: use sql.Open() and db.Ping().
  8. Move all endpoint creation to function initAPI(), with router *gin.Engine as a global variable.
  9. Create an endpoint /patient/all . It should display all patients in the db, in JSON format. Name the function GetAllPatients(c *gin.Context)
  10. Create endpoint /location/all to display all locations in JSON format. Name the function GetAllLocations(c *gin.Context)
  11. Create endpoint /hospital/all to display all hospitals in JSON format. Name the function GetAllHospitals(c *gin.Context)
  12. In main() function, call the init methods and run the router .
  13. Commit and create a new PR for this task.

INSERT operations must be shown in the .sql file

Commit message should be GCI-<gci-id>: <your_message>

Submission steps

  1. Your PR link
  2. Three screenshots showing the responses in Postman for /patient/all , /location/all and /hospital/all endpoints, pretty format.
@prathamesh-mutkure
Copy link

prathamesh-mutkure commented Jan 11, 2020

Hii everybody, the link to demo server is not working right now, you can try this link https://refapp.openmrs.org/openmrs/login.htm to see location util demo server is back

@f4ww4z
Copy link
Author

f4ww4z commented Jan 11, 2020

Hii everybody, the link to demo server is not working right now, you can try this link https://qa-refapp.openmrs.org/openmrs/login.htm util demo server is back

@PrathameshMutkure Is that relevant here?

@prathamesh-mutkure
Copy link

Hii @f4ww4z, do I need to write all the INSERT operations in the same schema file given above?

@prathamesh-mutkure
Copy link

prathamesh-mutkure commented Jan 11, 2020

@PrathameshMutkure Is that relevant here?

Yes, its the reference application server which also shows the same location as demo server (which is currently down)

@f4ww4z
Copy link
Author

f4ww4z commented Jan 12, 2020

Hii @f4ww4z, do I need to write all the INSERT operations in the same schema file given above?

Yes @PrathameshMutkure, you can either create a new .sql file or place it in the schema file.

Yes, its the reference application server which also shows the same location as demo server (which is currently down)

Ok, I believe it's fixed now: https://demo.openmrs.org/openmrs/login.htm

@ajeyprasand
Copy link

Hello i think the link linked to the text " locations shown here" is not working??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment