/ecommerce

Java Ecommerce made with GraphQl and PubSub

Primary LanguageJava

ecommerce

Simple Ecommerce endpoints using GraphQL and PubSub.

Summary

  1. Basic features
  2. Run locally
  3. API Usage
  4. Front End
  5. Unit Tests
  6. Hot it Works
  7. Future Improvements

Basic features:

  • Receive orders from third parties origins.
  • Visualize all orders.

Run locally:

  1. Install Docker and Docker Compose.

  2. Clone the repository and cd into it:

git clone https://github.com/SirVoRaK/ecommerce
cd ecommerce
  1. Build the docker images:
docker build -t ecommerce-publisher ecommerce-publisher
docker build -t ecommerce-consumer ecommerce-consumer
docker build -t ecommerce-frontend ecommerce-frontend
  1. (IMPORTANT) Create the data folder:
mkdir data
mkdir data/mongodb
mkdir data/pubsub
  1. Run the containers:

    1. Keep attached:
    docker compose up
    1. Run in background:
    docker compose up -d
  2. Stop the container:

docker compose down

API Usage:

Publish an order:

Example Request:

POST http://localhost:8001/graphql

mutation {
    publishPlacedOrderMessage(order: {
        order: "MY-ORDER-NUMBER-1",
        origin: "moon",
        items: [
            {
                name: "Boot",
                image: "image-path.png",
                qty: 1,
                cost: 50.25,
                currency: "BRL"
            },
            {
                name: "Hammer",
                image: "image-path.png",
                qty: 2,
                cost: 10.5,
                currency: "BRL"
            },
            {
                name: "Fork",
                image: "image-path.png",
                qty: 12,
                cost: 2.25,
                currency: "BRL"
            }
        ]
    }) {
        success,
        message
    }
}
Example Response:
{
    "data": {
        "publishPlacedOrderMessage": {
            "success": true,
            "message": "Order created successfully"
        }
    }
}

List all orders:

Example Request:

POST http://localhost:8002/graphql

query {
    placedOrders {
        id,
        order,
        origin,
        total,
        createdAt,
        items {
            name,
            image,
            qty,
            cost,
            currency
        }
    }
}
Example Response:
{
    "data": {
        "placedOrders": [
            {
                "id": "64cc0ba8dd694012b48b31ee",
                "order": "MY-ORDER-NUMBER-1",
                "origin": "moon",
                "total": 98.25,
                "createdAt": "2023-08-03T17:18:48.051946324",
                "items": [
                    {
                        "name": "Boot",
                        "image": "image-path.png",
                        "qty": 1,
                        "cost": 50.25,
                        "currency": "BRL"
                    },
                    {
                        "name": "Hammer",
                        "image": "image-path.png",
                        "qty": 2,
                        "cost": 10.5,
                        "currency": "BRL"
                    },
                    {
                        "name": "Fork",
                        "image": "image-path.png",
                        "qty": 12,
                        "cost": 2.25,
                        "currency": "BRL"
                    }
                ]
            }
        ]
    }
}

Get Order By Id:

Example Request:

POST http://localhost:8002/graphql

query {
    placedOrder (id: "64cc0ba8dd694012b48b31ee") {
        id,
        order,
        origin,
        total,
        createdAt,
        items {
            name,
            image,
            qty,
            cost,
            currency
        }
    }
}
Example Response:
{
    "data": {
        "placedOrder": {
            "id": "64cc0ba8dd694012b48b31ee",
            "order": "MY-ORDER-NUMBER-1",
            "origin": "moon",
            "total": 98.25,
            "createdAt": "2023-08-03T17:18:48.051946324",
            "items": [
                {
                    "name": "Boot",
                    "image": "image-path.png",
                    "qty": 1,
                    "cost": 50.25,
                    "currency": "BRL"
                },
                {
                    "name": "Hammer",
                    "image": "image-path.png",
                    "qty": 2,
                    "cost": 10.5,
                    "currency": "BRL"
                },
                {
                    "name": "Fork",
                    "image": "image-path.png",
                    "qty": 12,
                    "cost": 2.25,
                    "currency": "BRL"
                }
            ]
        }
    }
}

Front End

Accessing http://localhost:8000/orders (or just http://localhost:8000) in your browser you will see: orders list

Accessing http://localhost:8000/orders/ID (or by clicking on an order) you will see: order details

Unit Tests

Both microservices are tested using JUnit and Mockito:
Publisher:
unit tests publisher
Consumer:
unit tests consumer

How it Works

The ecommerce-publisher project is responsible for receiving an order through a GraphQL endpoint (exposed to the third parties), parsing it to JSON and publishing it to a PubSub topic/queue.
After that the ecommerce-consumer listens to that topic, parses the JSON into an Order instance and persist it to a Mongo database.
The consumer is also responsible for exposing a GraphQL endpoint to read the orders from the database, by making this the third parties won't have access to all orders.
The front end is made with Angular. It makes a request to the consumer to get the orders and display it to the user, clicking on an order you'll see it's details.

Future Improvements

  • Adding order pagination.
  • Adding logs and save them to ElasticSearch.
  • Adding filters.