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
modelsdirectory, and create the modelstructsPatient,LocationandHospitalinside this package. - Create a folder called
patient,locationandhospitalin the root directory. These will be the base endpoints. Then createrepository,usecaseanddeliveryfolder inside each endpoint folder. - Inside each endpoint folder, create
usecase.goandrepository.gofiles. These will be the interfaces you will use. - In each
usecase.go, create the interfaceUsecasewith 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_hereis the models you created insidemodelsfolder.
- In each
repository.go, create an interfaceRepositorywith 3 identical methods toUsecaseinterface. - Inside each endpoint's
repositoryfolder, create the filepostgresql_<model_name>.go(e.g.postgresql_patient.go). In the file, create thestruct<model_name>Repositorywhich has only adb *sqlx.DBattribute. - Create function
New<ModelName>Repository(db *sqlx.DB) <model_name>.Repository(e.g.NewPatientRepository(db *sqlx.DB) patient.Repositoryand return the newly created struct in step 7. - Implement the interface methods defined in the
Repositoryinterface. - In each endpoint's
usecasefolder, create<model_name>_usecase.go. Inside it, create a struct<model_name>Usecasewith<model_name>.Repositoryas 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
Usecaseinterface. It should just return the repository's respective methods. (Hint: use the struct'srepositoryattribute to access the methods). - In each endpoint's
deliveryfolder, create filehandler_<model_name>.go. Inside it, create a struct<ModelName>Handlerwith theUsecaseinterface 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 therouterparameter, 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 itsid. - POST
, to create a new instance of the model type. Response should be a success message.
- GET
- In
main.go, remove every global vars and any methods except themainfunction. - Inside that function, create variable
db *sqlx.DBandr *gin.Engine, and initialize them appropriately.dbshould connect to ElephantSQL instance. - Create 3 variables, 1 for each repository instance. Use the
New<ModelName>Repositorymethod. - Create 3 variables, 1 for each usecase instance. Use the
New<ModelName>Usecasemethod. - 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?