Skip to content

Instantly share code, notes, and snippets.

@f4ww4z
Last active April 23, 2021 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save f4ww4z/db919414c34275831988ce954d940f7e to your computer and use it in GitHub Desktop.
Save f4ww4z/db919414c34275831988ce954d940f7e to your computer and use it in GitHub Desktop.
Golang-3 task files for GCI 2019 with OpenMRS

GoLang-3: Clean Architecture

NOTE: You should have completed GoLang-2 task. Using the same repo, create a new branch and follow these steps:

Task

  1. Read up on Clean Architecture using Golang, and see its example repository.
  2. Create models directory, and create the model structs Patient, Location and Hospital inside this package.
  3. Create a folder called patient , location and hospital in the root directory. These will be the base endpoints. Then create repository, usecase and delivery folder inside each endpoint folder.
  4. Inside each endpoint folder, create usecase.go and repository.go files. These will be the interfaces you will use.
  5. In each usecase.go, create the interface Usecase with 3 methods:
    • Fetch() ([]models.<model_name_here>, error)
    • GetById(id int64) (models.<model_name_here>, error)
    • New(<model_name> models.<model_name_here>) error
    • where <model_name_here is the models you created inside models folder.
  6. In each repository.go, create an interface Repository with 3 identical methods to Usecase interface.
  7. Inside each endpoint's repository folder, create the file postgresql_<model_name>.go (e.g. postgresql_patient.go). In the file, create the struct <model_name>Repository which has only a db *sqlx.DB attribute.
  8. Create function New<ModelName>Repository(db *sqlx.DB) <model_name>.Repository (e.g. NewPatientRepository(db *sqlx.DB) patient.Repository and return the newly created struct in step 7.
  9. Implement the interface methods defined in the Repository interface.
  10. In each endpoint's usecase folder, create <model_name>_usecase.go. Inside it, create a struct <model_name>Usecase with <model_name>.Repository as an attribute of it.
  11. Create function New<ModelName>Usecase(repository <model_name>.Repository) <model_name>.Usecase (e.g. NewPatientUsecase(repository patient.Repository) patient.Usecase) and return the newly created struct in step 10.
  12. Implement the interface methods defined in the Usecase interface. It should just return the repository's respective methods. (Hint: use the struct's repository attribute to access the methods).
  13. In each endpoint's delivery folder, create file handler_<model_name>.go. Inside it, create a struct <ModelName>Handler with the Usecase interface as an attribute.
  14. Create method New<ModelName>Handler(router *gin.RouterGroup, usecase <model_name>.Usecase). Inside the method, create an instance of the <ModelName>Handler. Then, create endpoints using the router parameter, to point to the usecase methods you defined earlier. This will be grouped later, so just create:
    • GET , to fetch all the instances of that model type from the database.
    • GET /:id, to fetch a specific model using its id.
    • POST , to create a new instance of the model type. Response should be a success message.
  15. In main.go, remove every global vars and any methods except the main function.
  16. Inside that function, create variable db *sqlx.DB and r *gin.Engine, and initialize them appropriately. db should connect to ElephantSQL instance.
  17. Create 3 variables, 1 for each repository instance. Use the New<ModelName>Repository method.
  18. Create 3 variables, 1 for each usecase instance. Use the New<ModelName>Usecase method.
  19. Create 3 variables, 1 for each endpoint group. E.g. patientGroup := r.Group("/patients")
  20. Call the 3New<ModelName>Handler's. Assign the router group vars you defined earlier as parameters.
  21. Run the router like normal. Commit and push.

You should return all errors so that it can be shown in the json response, if there is any. E.g. {"message": <error_message>}

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

Submission steps

  1. Your PR link

  2. 6 screenshots showing the responses in Postman:

    • /patients , /patients/:id
    • /locations, /locations/:id
    • /hospitals , /hospitals/:id

    pretty format.

References

@prathamesh-mutkure
Copy link

Hii @f4ww4z, in the implementation of inerface method New(models.Hospital) error, do we need to insert data into database?

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