Skip to content

Test task: a simplified e-commerce API with Spring boot

Notifications You must be signed in to change notification settings

itykhan/watch-price-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

watch-price-calculator

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 IDWatch NameUnit priceDiscount
001Rolex1003 for 200
002Michael Kors802 for 120
003Swatch50
004Casio30
  • The first two products have a possible discount. As an example, if the user attempts to check out three or six Rolex watches then they will receive the discount price once or twice, respectively.
  • There is no limit to the number of items or combinations of watches a user can checkout.
  • There is no limit to the number of times a discount can be used.
  • Similarly, a user can checkout a single item if they wish.

  • 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

  • Split the application into two modules: "watch" and "checkout." The first module will encompass the "watch" entity, repository, data loading into the database, while the second module will be focused on price calculation logic and checkout endpoint.
  • Simplify data loading into the database, for example, from a CSV file.
  • Check and improve code test coverage.
  • In case it's feasible, identify areas of change and modify the design to make the process of making changes simpler.
  • Improve documentation quality.
  • About

    Test task: a simplified e-commerce API with Spring boot

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published