/spring-ddd-demo

Domain Driven Design Spring Boot Demo

Primary LanguageJava

[TOC]

Spring DDD Demo

Env Setup

Setup MySQL database

Order API Tests

Create Order

Request:

http :8080/orders id=11 name="pen" price=2.5

Response:

HTTP 201 Created
{
  "id": 20,
  "orderItems": [
    {
      "id": 31,
      "price": 2.5,
      "productId": 11
    }
  ],
  "price": 2.5,
  "status": "CREATED"
}

Try to create order but missed some fields

Request:

http :8080/orders id=99 name="noprice"

Response:

HTTP 400 Bad Request
{
  "errors": [
    {
      "code": "",
      "description": "Price is mandatory"
    }
  ],
  "message": "Validation failed for arguments",
  "status": 400,
  "timestamp": "2021-09-20 11:03:33"
}

Add product for order

Request:

http :8080/orders/20/products id=22 name="headset" price=128.00

Response:

HTTP 200 OK
{
  "id": 20,
  "orderItems": [
    {
      "id": 31,
      "price": 2.5,
      "productId": 11
    },
    {
      "id": 32,
      "price": 128.0,
      "productId": 22
    }
  ],
  "price": 130.5,
  "status": "CREATED"
}

Get order

Request:

http :8080/orders/20

Response:

HTTP 200 OK
{
  "id": 20,
  "orderItems": [
    {
      "id": 31,
      "price": 2.5,
      "productId": 11
    },
    {
      "id": 32,
      "price": 128.0,
      "productId": 22
    }
  ],
  "price": 130.5,
  "status": "CREATED"
}

Try to get a not found order

Request:

http :8080/orders/99

Response:

HTTP 400 NOT_FOUND
{
  "errors": null,
  "message": "Could not find order 99",
  "status": 404,
  "timestamp": "2021-09-20 11:04:57"
}

Delete product from order

Request:

http DELETE :8080/orders/20/products?productId=22

Response:

HTTP 204 No Content

Complete Order

Request:

http POST :8080/orders/20/complete

Response:

HTTP 200 OK
{
    "id": 20,
    "orderItems": [
        {
            "id": 31,
            "price": 2.5,
            "productId": 11
        }
    ],
    "price": 2.5,
    "status": "COMPLETED"
}

Try to add product in the COMPLETED order

Request:

http :8080/orders/20/products id=33 name="book" price=45.00

Response:

HTTP 500 Internal Error
{
  "errors": null,
  "message": "The order is in completed state.",
  "status": 500,
  "timestamp": "2021-09-20 11:11:11"
}

TODO

  • Map JPA entities to DTO // done
  • Exception handling // done
  • Unify response
  • Use MongoDB for NoSQL example
  • Health check
  • Graceful shutdown // done
  • Log to console/STDOUT // done
  • Multiple application profiles
  • Flyway data migration
  • Unit Tests for domain layer
  • Integration tests

References