Skip to content

luisbilecki/cart-redis-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cart management with Redis in Node.JS

This repository contains an approach to store the customers' shopping cart in Redis. This approach was based in RedisLabs usage example (see here) and another example (Cart Management Service).


Requirements

This app is not docker based, so you should install Node.js, in order to run the API server.

Node.js >= 10.x
Redis

How to run

  1. Run yarn install to install required dependencies for this project;

  2. Create .env file using the provided template (.env.example);

  3. Run API using:

    yarn start

    or

    node src/server.js
  1. Run tests using the following command:
    yarn test
  1. Linting can be checked using:
    yarn lint

Rest API endpoints

The REST API endpoints is described below.

Get Cart

Returns json data with cart items for user specified with session_id parameter.

  • URL

    /carts/:session

  • Method:

    GET

  • URL Params

    Required:

    session=[string]

  • Data Params

    None

  • Success Response:

    • Code: 200
      Content: { success: true, items: { <Item ID>: <Quantity>, ...} }
  • Sample Call:

      curl http://localhost:3000/carts/3713ae4a-b7de-42f6-934d-9a74ac233cd5

Add Item to Cart

Add new item to the cart.

  • URL

    /carts/:session/items

  • Method:

    POST

  • URL Params

    Required:

    session=[string]

  • Data Params

      {
        "itemId": "integer",
        "quantity": "integer",
      }
  • Success Response:

    • Code: 200
      Content: { success: true }
  • Error Response:

    None

  • Sample Call:

      curl -H "Content-Type: application/json" -X POST -d '{"itemId":900,"quantity":1}' http://localhost:3000/carts/3713ae4a-b7de-42f6-934d-9a74ac233cd5/items

Update Cart Item

Update the quantity of an existent item on customer cart.

  • URL

    /carts/:session/items/:itemId

  • Method:

    PUT

  • URL Params

    Required:

    session=[string] itemId=[integer]

  • Data Params

      {
        "quantity": "integer",
      }
  • Success Response:

    • Code: 200
      Content: { success: true }
  • Error Response:

    • Code: 404 NOT FOUND
      Content:
        {
          "status": 404,
          "error": "Not Found",
          "message": "Item not found",
          "path": "/3713ae4a-b7de-42f6-934d-9a74ac233cd5/items/929",
          "timestamp": 1578089747041
        }
      
  • Sample Call:

      curl -H "Content-Type: application/json" -X PUT -d '{"quantity":3}' http://localhost:3000/carts/3713ae4a-b7de-42f6-934d-9a74ac233cd5/items/929

Delete Item from Cart

Delete one item from customer cart.

  • URL

    /carts/:session/items/:itemId

  • Method:

    DELETE

  • URL Params

    Required:

    session=[string] itemId=[integer]

  • Data Params

    None

  • Success Response:

    • Code: 200
      Content: { success: true }
  • Sample Call:

      curl -X DELETE http://localhost:3000/carts/3713ae4a-b7de-42f6-934d-9a74ac233cd5/items/5455

Clear All Cart Items

Delete all items from customer cart.

  • URL

    /carts/:session

  • Method:

    DELETE

  • URL Params

    Required:

    session=[string]

  • Data Params

    None

  • Success Response:

    • Code: 200
      Content: { success: true }
  • Sample Call:

      curl -X DELETE http://localhost:3000/carts/3713ae4a-b7de-42f6-934d-9a74ac233cd5

Notes

About

Shopping cart with Redis in Node.JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published