This is an example golang backend application using MongoDB database with clean architecture.
- Go Web Framework (gin-gonic)
- Swagger (swaggo)
- CRUD operations
- Mock: golang/mock
- Database: MongoDB
- Test Assertions: stretchr/testify
# download the project
git clone https://github.com/aabdullahgungor/product-api.git
cd product-api
go run main.go
http://localhost:8000/api/v1
- GET localhost:8000/api/v1/products
- GET localhost:8000/api/v1/products/:id
- POST localhost:8000/api/v1/products
- PUT localhost:8000/api/v1/products
- DELETE localhost:8000/api/v1/products/:id
localhost:8000/api/v1/products
request body: // We did not write the id number because we generate it automatically.
{
"name": "Refrigerator 1",
"price": 1000,
"quantity": 3,
"status": true,
"date": "2016-10-20T00:00:00Z",
"category_id": "5a30de130867edfa45711668",
"brand": {
"id": "641db6ba47413b86ecfae49a",
"name": "Vestel"
},
"colors": [
"red",
"green",
"blue"
]
}
response body:
{
"message": "Product has been created"
}
localhost:8000/api/v1/products/641db6ba47413b86ecfae49b
response body:
{
"id": "641db6ba47413b86ecfae49b",
"name": "Mobile 1",
"price": 45,
"quantity": 4,
"status": true,
"date": "2016-10-20T00:00:00Z",
"category_id": "5a30de130867edfa45711668",
"brand": {
"id": "641db6ba47413b86ecfae49a",
"name": "Vestel"
},
"colors": [
"red",
"green",
"blue"
]
}
localhost:8000/api/v1/products
request body: //we changed price and quantity
{
"id": "641db6ba47413b86ecfae49b",
"name": "Mobile 1",
"price": 5000,
"quantity": 20,
"status": true,
"date": "2016-10-20T00:00:00Z",
"category_id": "5a30de130867edfa45711668",
"brand": {
"id": "641db6ba47413b86ecfae49a",
"name": "Vestel"
},
"colors": [
"red",
"green",
"blue"
]
}
response body:
{
"message": "Product has been edited",
"product_id": "641db6ba47413b86ecfae49b"
}
localhost:8000/api/v1/products/64903edda330532bbb2614b2
// You can use the id number of the new product you added before. You must use GET products method to learn the id of new product
{
"message": "Product has been deleted",
"product_id": "64903edda330532bbb2614b2"
}
http://localhost:8000/api/v1/swagger/index.html
# Run tests
go test ./controller -v
go test ./service -v