Skip to content

sphadnis007/go-postgres-as-nosql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This simple project implements "Shopping Cart" used on e-commerce websites. To learn more about how it works, checkout the examples section.

Motive

  1. Learn how to interact with Postgres using Golang.
  2. Handle NoSQL like data in Postgres using jsonb.
  3. Use REST server (gin-gonic) in Golang.

Pre-requisites

  1. Golang should be present on the host.
  2. 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

Installation

Clone the repository and simply run the following commands:

go mod vendor
go run .

API Specifications

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.

Examples

  1. Shopping Carts table is empty at the start.
curl http://localhost:3000/cart | python -m json.tool
[]
  1. 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 }"
  1. 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
    }
]
  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!!! }"
  1. 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
    }
]

Non-standard Modules used

  1. Postgres driver - pgx
  2. REST server - gin-gonic
  3. Fake data generator - gofakeit
  4. UUID - google/uuid

About

Interacting with Postgres NoSQL data using GoLang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages