Sample REST API server built with Go, Gin and MongoDB
-
Clone the repository and change to the project directory.
git clone https://github.com/jenishayadav/go-gin-e-market.git cd go-gin-e-market
-
Install dependencies.
go get .
-
Start a mongodb server at local using Docker.
docker run --name mongodb -d -p 27017:27017 mongodb/mongodb-community-server
-
Mongo initial steps.
4.1 Start mongo shell.
docker exec -it mongodb mongosh
4.2 Create a database and two mongo collections, one for
Order
and another forProduct
use josh_assignment
db.createCollection("order") db.createCollection("product")
4.3 Verify the collections.
db.getCollectionNames()
Exit
mongosh
. -
Run the server.
go run .
-
Health check
curl --location --request GET 'http://localhost:8080/api/healthcheck'
-
Create product
curl --location --request POST 'http://localhost:8080/api/products/' \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "DermaCO Vitamin-C Serum", "availableQty": 6, "price": 1000.0, "category": "Premium" }'
-
Get all products
curl --location --request GET 'http://localhost:8080/api/products/'
-
Get product by ID. (Replace
<PRODUCT_ID>
with a sample ID)curl --location --request GET "http://localhost:8080/api/products/<PRODUCT_ID>"
-
(Partial) Update product by ID. (Replace
<PRODUCT_ID>
with a sample ID)curl --location --request PATCH "http://localhost:8080/api/products/<PRODUCT_ID>" \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "DermaCo Vitamin-C Serum", "price": 1100.0, }'
-
Delete product by ID. (Replace
<PRODUCT_ID>
with a sample ID)curl --location --request DELETE "http://localhost:8080/api/products/<PRODUCT_ID>"
-
Create order. (Replace product ids placeholders)
curl --location --request POST 'http://localhost:8080/api/orders/' \ --header 'Content-Type: application/json' \ --data-raw '{ "orderItems": [ { "productId":"<PRODUCT_ID_1>", "quantity": 3 }, { "productId":"<PRODUCT_ID_2>", "quantity": 5 } ] }'
NOTE: This takes care of maximum order quantity and also checks if ordered quantity is less than or equal to available quantity of order item.
NOTE: You will see change in available quantity of ordered products as well. Try the request for fetching all products and validate.
-
Get all orders
curl --location --request GET "http://localhost:8080/api/orders/"
-
Get order by ID
curl --location --request GET "http://localhost:8080/api/orders/<ORDER_ID>"
-
Update order by ID
curl --location --request PATCH "http://localhost:8080/api/orders/<ORDER_ID>" \ --header 'Content-Type: application/json' \ --data-raw '{ "orderStatus": "Dispatched" }'
NOTE: You will see the update in dispatch date as well.
-
Delete order by ID
curl --location --request DELETE "http://localhost:8080/api/orders/<ORDER_ID>"