This is a simple REST API built with Golang (Gin framework) that allows users to create, read, update, and delete collection in database.
This project requires the following dependencies to be installed:
- Golang (https://go.dev/)
- Gin framewrok (for handling HTTP requests) (https://github.com/gin-gonic/gin)
- MongoDB Go driver (for connecting to MongoDB database) (https://github.com/mongodb/mongo-go-driver)
-
Clone this repository to your local machine
-
Install the dependencies using go modules. Run the following command in the root directory of the project:
go get ./...
-
Create a
.env
file in the root directory of the project and add the following variables:DB_DATABASE=<your_mongodb_database> DB_USERNAME=<your_mongodb_username> DB_PASSWORD=<your_mongodb_password> MONGO_URI=<your_mongodb_uri> PORT=<port_number>
Replace
<your_mongodb_uri>
with the URI of your MongoDB database, and<port_number>
with your desired port number. -
Whenever you make a
.env
file update, use the following command to activate the latest values:source .env
-
Run the server by running the following command in the root directory of the project:
go run main.go
-
Create a student by sending a POST request to
http://localhost:<port_number>/api/students
Sample request:
{ "code": "20520514", "name": "eGreenBin", "numOfCorrect": 25, "numOfWrong": 19, "imageAvatarUrl": "https://afamilycdn.com/2019/9/25/photo-1-15693889404421259167070.jpg?fbclid=IwAR2YuYMfdc_RazmNjtgWKej14GDwFMn4xnjzu-cWmy5lRN2eLhXEgp-SkQc", "parentEmail": "[email protected]", "Note": "" }
Sample response:
{ "data": { "id": "6416c301131dc564c6c32r11", "code": "20520514", "name": "eGreenBin", "numOfCorrect": 25, "numOfWrong": 19, "imageAvatarUrl": "https://afamilycdn.com/2019/9/25/photo-1-15693889404421259167070.jpg?fbclid=IwAR2YuYMfdc_RazmNjtgWKej14GDwFMn4xnjzu-cWmy5lRN2eLhXEgp-SkQc", "parentEmail": "[email protected]", "note": "" } }
-
Retrieve a student by sending a GET request to
http://localhost:<port_number>/api/students/{id}
Sample response:
{ "data": { "id": "6416c301131dc564c6c32r11", "code": "20520514", "name": "eGreenBin", "numOfCorrect": 25, "numOfWrong": 19, "imageAvatarUrl": "https://afamilycdn.com/2019/9/25/photo-1-15693889404421259167070.jpg?fbclid=IwAR2YuYMfdc_RazmNjtgWKej14GDwFMn4xnjzu-cWmy5lRN2eLhXEgp-SkQc", "parentEmail": "[email protected]", "note": "" } }
-
Update a student by sending a PUT request to
http://localhost:<port_number>/api/students/{id}
Sample request:
{ "code": "205201", "name": "eGreenBin", "numOfCorrect": 25, "numOfWrong": 19, "imageAvatarUrl": "https://afamilycdn.com/2019/9/25/photo-1-15693889404421259167070.jpg?fbclid=IwAR2YuYMfdc_RazmNjtgWKej14GDwFMn4xnjzu-cWmy5lRN2eLhXEgp-SkQc", "parentEmail": "[email protected]", "Note": "" }
-
Delete a student by sending a DELETE request to
http://localhost:<port_number>/api/students/{id}
If you would like to contribute to this project, please fork the repository and make a pull request.
- Nguyen Huu Hieu