Git Product home page Git Product logo

shopping-platform-hexagonal-kotlin's Introduction

Shopping Platform Hexagonal Kotlin

A simple example of a shopping platform implemented in Kotlin with hexagonal architecture.

Features

  • REST API Endpoints:
    • Retrieve Product Information: Fetch details of a product using its UUID.
    • Calculate Total Price: Compute the total price for a given product and quantity, applying applicable discounts.
  • Configurable Discount Policies.
  • Hexagonal Architecture.
  • In-Memory Database: For simplicity, the service uses an in-memory database to store product information.

Architecture Overview

The architecture of the project is illustrated in the following diagram:

Architecture Diagram

Assumptions

  • If multiple discounts are applicable to a product, the service will apply the discount that results in the lowest total price for the customer.
  • Percentage-based discounts are only applicable to specific products. There is a predefined list of product IDs for which each percentage-based discount can be applied. Products not on this list will not receive the percentage-based discount.
  • The product data is fixed and hardcoded in the in-memory repository implementation.
  • Discount policies are defined and configurable through the application.yaml file.
  • The maximum number of products that can be processed in a single price calculation request is limited to 100

Building the Application

Step 1: Clean and Build the Application

Run the following command to clean and build the project:

./gradlew clean build

Step 2: Package the Application into a Fat JAR

./gradlew bootJar

Running the Application in Docker

Step 1: Build the Docker Image

Use the following command to build the Docker image:

docker build -t shopping-platform .

Step 2: Run the Docker Container

docker run -p 8080:8080 --name shopping-platform-container shopping-platform

Stop and remove the Docker Container

docker stop shopping-platform-container
docker rm shopping-platform-container

Predefined Products

The following products are preloaded into the in-memory database:

ID Price (PLN)
aac7d817-93f0-4f6f-92c4-6752c95d23b0 599.99
aac7d817-93f0-4f6f-92c4-6752c95d23b1 11999.99
aac7d817-93f0-4f6f-92c4-6752c95d23b2 5299.99
aac7d817-93f0-4f6f-92c4-6752c95d23b3 1299.00
aac7d817-93f0-4f6f-92c4-6752c95d23b4 7699.99

Configuration of Discounts

The discounts applied to the products are configured in the application.yaml file, located in the src/main/resources.

Here’s an example configuration:

discounts:
  amount-based:
    - amount: 50
      threshold: 10
    - amount: 30
      threshold: 5
    - amount: 10
      threshold: 2
  percentage-based:
    - productIds:
        - "aac7d817-93f0-4f6f-92c4-6752c95d23b0"
        - "aac7d817-93f0-4f6f-92c4-6752c95d23b1"
      percentage: 10
    - productIds:
        - "aac7d817-93f0-4f6f-92c4-6752c95d23b2"
      percentage: 15
    - productIds:
        - "aac7d817-93f0-4f6f-92c4-6752c95d23b3"
      percentage: 20

Calling the API Endpoints

1. Retrieve Product Information

To retrieve product information by UUID, you can use the following curl command.

curl -X GET "http://localhost:8080/products/aac7d817-93f0-4f6f-92c4-6752c95d23b0" -H "Accept: application/json"

2. Calling the Price Calculator API

To calculate total price of the product, you can use the following curl command.

curl -X POST "http://localhost:8080/products/aac7d817-93f0-4f6f-92c4-6752c95d23b0/calculate-price" \
-H "Content-Type: application/json" \
-d '{"quantity": 2}'

shopping-platform-hexagonal-kotlin's People

Contributors

sledzmateusz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.