Skip to content

jolienai/go-products-api

Repository files navigation

go-products-api

A simple example of how to implement a products API in Golang using:

Endpoints

  1. POST /products/bulk Using to upload the csv file to be processed, so the stock for each product will be updated accordingly.

the .csv file should contain the following structure:

country,sku,name,stock_change
"gh","e920c573f128","Ramirez-Molina Granite Pizza","100"
  1. GET /products/:sku Returns a list of products for the specified sku, passed in the url

  2. PATCH /products Consume products if the is enough quantity in stock. It returns bad requests if the quantity is too high or product was not found in teh database.

Request body example:

{
  "sku": "cbf87a9be799",
  "country": "dz",
  "quantity": 1
}

Run products-api locally

  1. Set environment variable
export POSTGRES_CONNECTION_STRING="host=localhost port=5432 user=testuser dbname=productsdb password=123456 sslmode=disable"
make postgres-up
cd cmd/app
go run main.go

Testing

got to http://localhost:8080/v1/products/bulk to upload the csv file, after that the job will update the product accordingly. got to http://localhost:8080/v1/products/{put_here_product_sku} to find a list of products for the specified sku.

See an example below:

curl GET http://localhost:8080/v1/products/e920c573f128
{
  "products": [
    {
      "sku": "e920c573f128",
      "country": "gh",
      "name": "Ramirez-Molina Granite Pizza",
      "stock_change": 51
    },
    {
      "sku": "e920c573f128",
      "country": "ma",
      "name": "Ramirez-Molina Granite Pizza",
      "stock_change": 58
    },
    {
      "sku": "e920c573f128",
      "country": "ug",
      "name": "Ramirez-Molina Granite Pizza",
      "stock_change": 63
    }
  ]
}

Note: You also can use the Postman collection products-api-postman_colq.json where you can find also an example of the PATCH method to consume a product and update its stock if possible.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published