/go-gin-e-market

Toy project for implementing REST server in Go

Primary LanguageGo

E-Market

Sample REST API server built with Go, Gin and MongoDB

Usage

  1. 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
  2. Install dependencies.

    go get .
  3. Start a mongodb server at local using Docker.

    docker run --name mongodb -d -p 27017:27017 mongodb/mongodb-community-server
  4. 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 for Product

    use josh_assignment
    
    db.createCollection("order")
    db.createCollection("product")
    

    4.3 Verify the collections.

    db.getCollectionNames()
    

    Exit mongosh.

  5. Run the server.

    go run .

Sample requests (cURL)

  1. Health check

    curl --location --request GET 'http://localhost:8080/api/healthcheck'
  2. 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"
        }'
  3. Get all products

    curl --location --request GET 'http://localhost:8080/api/products/'
  4. Get product by ID. (Replace <PRODUCT_ID> with a sample ID)

    curl --location --request GET "http://localhost:8080/api/products/<PRODUCT_ID>"
  5. (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,
        }'
  6. Delete product by ID. (Replace <PRODUCT_ID> with a sample ID)

    curl --location --request DELETE "http://localhost:8080/api/products/<PRODUCT_ID>"
  7. 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.

  8. Get all orders

    curl --location --request GET "http://localhost:8080/api/orders/"
  9. Get order by ID

    curl --location --request GET "http://localhost:8080/api/orders/<ORDER_ID>"
  10. 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.

  11. Delete order by ID

    curl --location --request DELETE "http://localhost:8080/api/orders/<ORDER_ID>"