/order-up

Primary LanguageGoMIT LicenseMIT

Order Up

The order-up service handles all order-specific calls including creating orders, checking the status on orders, etc. This service is part of a larger microservice backend for a online marketplace.

Project Setup

  1. Download Docker.
  2. install Go
  3. Pull repository
  4. Run go mod tidy in repository folder
  5. cp .env.example .env - Fill with appropriate values for mongo database
  6. Start mongodb, docker run --rm -it -p 27017:27017 mongo
  7. Run project - go run main.go
  8. Open browser and check localhost:8888

Using the project

  • Run curl/postman against localhost:8888 the following:

POST localhost:8888/orders

{
    "customerEmail": "example@example.com",
    "lineItems": [
        {
            "description": "A sponge.",
            "priceCents": 500,
            "quantity": 50
        },
        {
            "description": "ACME baking kit! For all of your roadrunner needs!",
            "priceCents": 1234,
            "quantity": 3
        }
    ]
}

(Record the id from the response)

GET localhost:8888/orders/{order_id}

Using the charge and fulfillment services

  • These are external services. You will need to set them up separately.
  • (Insert hypothetical instructions on how to set up external service here. I didn't make time for this, but you could do it locally via a mock server or similar.)

API documentation

GET /orders - retrieves a list of orders and their statuses

Status codes: 200,

# Example Response - 200
{
    "orders": [
        {
            "id": "order-1",
            "customerEmail": "email@example.com",
            "lineItems": [
                {
                    "description": "A really awesome laptop",
                    "priceCents": 123000,
                    "quantity": 1
                }
            ],
            "status": 0
        },
        {
            "id": "order-2",
            "customerEmail": "email2@example.com",
            "lineItems": [
                {
                    "description": "A sponge.",
                    "priceCents": 500,
                    "quantity": 50
                },
                {
                    "description": "ACME baking kit! For all of your roadrunner needs!",
                    "priceCents": 1234,
                    "quantity": 3
                }
            ],
            "status": 1
        },
    ]
}

# Example response: 400
{
    "error": "unknown value for status: invalid"
}

POST /orders - inputs a list of line items in database as an order.

Status codes: 200,

# Example Request
{
    "customerEmail": "example@example.com",
    "lineItems": [
        {
            "description": "A sponge.",
            "priceCents": 500,
            "quantity": 50
        },
        {
            "description": "ACME baking kit! For all of your roadrunner needs!",
            "priceCents": 1234,
            "quantity": 3
        }
    ]
}
# Example Response
{
    "id": "order-abc",
    "customerEmail": "example@example.com",
    "lineItems": [
        {
            "description": "A sponge.",
            "priceCents": 500,
            "quantity": 50
        },
        {
            "description": "ACME baking kit! For all of your roadrunner needs!",
            "priceCents": 1234,
            "quantity": 3
        }
    ],
    "status": 0
}

# Example Response - 400
{
    "error": "invalid customerEmail"
}

{ 
    "error": "an order must contain at least one line item"
}

{
    "error": "an order's total cannot be less than 0"
}

# Example Response - 409
{
    "error": "order already exists"
}

GET /orders/:id - gets an order by id Status codes: 200,

# Example Response - 200
{
    "id": "order-abc",
    "customerEmail": "example@example.com",
    "lineItems": [
        {
            "description": "A sponge.",
            "priceCents": 500,
            "quantity": 50
        },
        {
            "description": "ACME baking kit! For all of your roadrunner needs!",
            "priceCents": 1234,
            "quantity": 3
        }
    ],
    "status": 0
}

# Example Response - 404
{
    "error": "not found"
}

POST /orders/:id/charge - charges a given order. Returns the charge in cents (USD). Status codes: 200,

# Example Request
{
    "cardToken": "amex"
}

# Example Response
{
    "chargedCents": 5300
}

# Example Response - 409
{
    "error": "order ineligible for charging"
}

POST /orders/:id/cancel - cancels a given order and issues a refund. Status codes: 201,

# Example Request
{
    "cardToken": "amex"
}

# Example Response - 201
{
    "orderStatus": "cancelled",
    "chargedCents": -4500 # Negative if a refund has been issued.
}

# Example Response - 409
{
    "error": "order has already been fulfilled"
}

PUT /orders/:id/fulfill - fulfils a given order by fulfilling all of the relevant line items. Returns the final status of the order after fulfill attempt.

# Example request
{}

# Example Response - 200
{
    "fulfilled": "true"
}

# Example Response - 400
{
    "error": "order cannot be fulfilled, order has not been charged."
}