NOTE: You should have completed GoLang-2 task. Using the same repo, create a new branch and follow these steps:
- Read up on Clean Architecture using Golang, and see its example repository.
- Create
models
directory, and create the modelstructs
Patient
,Location
andHospital
inside this package. - Create a folder called
patient
,location
andhospital
in the root directory. These will be the base endpoints. Then createrepository
,usecase
anddelivery
folder inside each endpoint folder. - Inside each endpoint folder, create
usecase.go
andrepository.go
files. These will be the interfaces you will use. - In each
usecase.go
, create the interfaceUsecase
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 insidemodels
folder.
- In each
repository.go
, create an interfaceRepository
with 3 identical methods toUsecase
interface. - Inside each endpoint's
repository
folder, create the filepostgresql_<model_name>.go
(e.g.postgresql_patient.go
). In the file, create thestruct
<model_name>Repository
which has only adb *sqlx.DB
attribute. - 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. - Implement the interface methods defined in the
Repository
interface. - 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. - 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. - Implement the interface methods defined in the
Usecase
interface. It should just return the repository's respective methods. (Hint: use the struct'srepository
attribute to access the methods). - In each endpoint's
delivery
folder, create filehandler_<model_name>.go
. Inside it, create a struct<ModelName>Handler
with theUsecase
interface as an attribute. - 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 therouter
parameter, to point to the usecase methods you defined earlier. This will be grouped later, so just create:- GET
- GET
/:id
, to fetch a specific model using itsid
. - POST
- GET
- In
main.go
, remove every global vars and any methods except themain
function. - Inside that function, create variable
db *sqlx.DB
andr *gin.Engine
, and initialize them appropriately.db
should connect to ElephantSQL instance. - Create 3 variables, 1 for each repository instance. Use the
New<ModelName>Repository
method. - Create 3 variables, 1 for each usecase instance. Use the
New<ModelName>Usecase
method. - Create 3 variables, 1 for each endpoint group. E.g.
patientGroup := r.Group("/patients")
- Call the 3
New<ModelName>Handler
's. Assign the router group vars you defined earlier as parameters. - 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>
-
Your PR link
-
6 screenshots showing the responses in Postman:
/patients
,/patients/:id
/locations
,/locations/:id
/hospitals
,/hospitals/:id
pretty format.
Hii @f4ww4z, in the implementation of inerface method
New(models.Hospital) error
, do we need to insert data into database?