This simple project implements "Shopping Cart" used on e-commerce websites. To learn more about how it works, checkout the examples section.
- Learn how to interact with Postgres using Golang.
- Handle NoSQL like data in Postgres using jsonb.
- Use REST server (gin-gonic) in Golang.
- Golang should be present on the host.
- Postgres server must be present/running on the host. It should have configurations as per configs.go file (or configs.go should be modified accordingly).
If Docker is present on the host then Postgres container with the required configurations can be started by running the following command:
docker run -p 5432:5432 -e POSTGRES_PASSWORD=shubham -e POSTGRES_DB=test-db -d postgres
Clone the repository and simply run the following commands:
go mod vendor
go run .
List all the carts
GET /cart
List products present in a cart with "cartID"
GET /cart/:cartID
Creates a new cart and adds "numOfObjects" products in it. It generates fake products data and returns cartID to the user. We don't have to provide any data in this call.
POST /cart/add/:numOfObjects
Deletes a product from the cart
DELETE /cart/:cartID/:productID
- Note: All the IDs used in this project are UUIDs.
- Shopping Carts table is empty at the start.
curl http://localhost:3000/cart | python -m json.tool
[]
- Added 3 carts in the DB with fake products data.
curl -X POST http://localhost:3000/cart/add/3 | python -m json.tool
"{ cart_id: a79cee22-44a4-447a-87b8-8e5c07214dba }"
curl -X POST http://localhost:3000/cart/add/1 | python -m json.tool
"{ cart_id: c3bfa6d3-79be-445f-9e18-bafae2f7b60e }"
curl -X POST http://localhost:3000/cart/add/2 | python -m json.tool
"{ cart_id: b4b43f97-cdb6-4b34-a173-891356ef6854 }"
- For - curl -X POST http://localhost:3000/cart/add/2 - call, we had received cart id as b4b43f97-cdb6-4b34-a173-891356ef6854. We can check products present in this cart.
curl http://localhost:3000/cart/b4b43f97-cdb6-4b34-a173-891356ef6854 | python -m json.tool
[
{
"category": "Objects",
"created_at": "1900-11-17T05:05:07.501868099Z",
"id": "7525ee13-92e3-4eda-8e50-550f9bc159d4",
"name": "Eggplant",
"price": 7699,
"quantity": 1
},
{
"category": "People & Body",
"created_at": "2003-05-16T00:52:15.591212582Z",
"id": "11531d65-0759-460a-9941-07407d223d9b",
"name": "Carrot",
"price": 1469,
"quantity": 1
}
]
- Deleting "Carrot" from the above cart
curl -X DELETE http://localhost:3000/cart/b4b43f97-cdb6-4b34-a173-891356ef6854/11531d65-0759-460a-9941-07407d223d9b | python -m json.tool
"{ Product Removed from the Cart!!! }"
- Rechecking if "Carrot" got removed or not.
curl http://localhost:3000/cart/b4b43f97-cdb6-4b34-a173-891356ef6854 | python -m json.tool
[
{
"category": "Objects",
"created_at": "1900-11-17T05:05:07.501868099Z",
"id": "7525ee13-92e3-4eda-8e50-550f9bc159d4",
"name": "Eggplant",
"price": 7699,
"quantity": 1
}
]
- Postgres driver - pgx
- REST server - gin-gonic
- Fake data generator - gofakeit
- UUID - google/uuid