Test task: a simplified e-commerce API with Spring boot. The API is implemented with Spring boot, Spring Data JPA, Spring MVC, data is stored in H2 in-memory database. The application is integrated with Swagger/OpenApi 3. A set of modul and integration JUnit tests is added.
To run application checkout repository and run ./mvnw spring-boot:run
.
Alternatively you can build a docker image and run the application in docker container using following commands:
# Create an executable JAR
./mvnw install
# Build an image
docker build -t itykhan/watch-price-calculator .
# Run the application in a container
docker run -p 8080:8080 itykhan/watch-price-calculator
Swagger endpoint: http://localhost:8080/swagger-ui/index.html
OpenAPI 3 endpoint: http://localhost:8080/v3/api-docs
API description
The /checkout
endpoint takes a list of IDs of watches and returns the total cost.
Watch catalogue
Watch ID | Watch Name | Unit price | Discount |
---|---|---|---|
001 | Rolex | 100 | 3 for 200 |
002 | Michael Kors | 80 | 2 for 120 |
003 | Swatch | 50 | |
004 | Casio | 30 |
Endpoint reference
Request
POST http://localhost:8080/checkout
# Headers
Accept: application/json
Content-Type: application/json
# Body
[
"001",
"002",
"001",
"004",
"003"
]
Response
# Headers
Content-Type: application/json
# Body
{ "price": 360 }
Current implementation
The watch catalogue is loaded into H2 database on the application startup. Calculation logic is implemented in CheckoutService
in calculateTotalPrice
method.
It takes the list of IDs of watches, using JPA Repository finds Watch objects by IDs and calculates total price сonsidering the described requirements. If Watch object for any ID is not
found WatchNotFoundException
is thrown. The API HTTP endpoint is implemented in CheckoutController
by calculatePrice
method.
What to improve