Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RaminMammadzada/aad851ba4b14de18f4bc47b83c39e083 to your computer and use it in GitHub Desktop.
Save RaminMammadzada/aad851ba4b14de18f4bc47b83c39e083 to your computer and use it in GitHub Desktop.
Sample Project: E-commerce Order Processing System

Project: E-commerce Order Processing System

Objective: Implement a RESTful API for an E-commerce Order Processing System using Java, Spring Boot, and Apache Kafka. The system should allow users to place orders and process them asynchronously through a message queue.

Requirements:

  1. Initialize a new Spring Boot project using Spring Initializr. Choose the following dependencies: Web, JPA, H2, Cache, Spring for Apache Kafka, and Lombok.

  2. Structure your project with the following packages:

    • model: Contains domain model classes (Product, Order)
    • repository: Contains repository interfaces extending JpaRepository
    • service: Contains service classes to encapsulate business logic
    • controller: Contains REST API controllers
    • exception: Contains custom exception classes
    • dto: Contains data transfer objects (DTOs) used in the API
  3. Implement the domain model:

    • Product class: Fields should include id, name, description, price, and stockQuantity
    • Order class: Fields should include id, product, quantity, status, and totalPrice. The status should be an enum with values PLACED, PROCESSING, COMPLETED, and FAILED.
  4. Implement the repositories:

    • ProductRepository: Extends JpaRepository<Product, Long>
    • OrderRepository: Extends JpaRepository<Order, Long>
  5. Implement the services:

    • ProductService: Contains methods for creating, updating, deleting, and retrieving products. Use Spring's Dependency Injection to inject the ProductRepository into the service.
    • OrderService: Contains methods for placing and updating orders. It should also contain methods for sending orders to a Kafka topic and consuming orders from the topic. Use Dependency Injection to inject the OrderRepository and a KafkaTemplate into the service.
  6. Implement the REST API controllers:

    • ProductController: Handles HTTP requests for CRUD operations on products. Use Dependency Injection to inject the ProductService into the controller.
    • OrderController: Handles HTTP requests for placing orders. Use Dependency Injection to inject the OrderService into the controller.
  7. Implement DTOs for the API:

    • ProductDTO: Contains fields for id, name, description, price, and stockQuantity. Use Java's built-in serialization mechanism to ensure that this object can be easily converted to JSON for use in the API.
    • OrderDTO: Contains fields for id, product, quantity, status, and totalPrice. Again, use Java's serialization mechanism for this.
  8. Implement exception handling:

    • Create custom exceptions (ResourceNotFoundException, BadRequestException, OutOfStockException)
    • Implement a global exception handler with @ControllerAdvice to handle exceptions and return appropriate HTTP response status codes
  9. Configure an H2 in-memory database for the application. Use Spring Data JPA for Object-Relational Mapping.

  10. Configure Spring's caching mechanism to cache products. You can use Spring's @Cacheable annotation to do this.

  11. Set up a Kafka producer and consumer in your OrderService. The producer should send orders to a Kafka topic when they are placed, and the consumer should process these orders asynchronously.

  12. Write unit tests for the service classes using JUnit and Mockito.

  13. (Optional) Implement API documentation using Swagger or OpenAPI.

Deliverables:

  • A fully functional Spring Boot project, including source

code and tests

  • A README file with instructions on how to build and run the project, as well as an explanation of any design decisions you made

When you submit your project, please include all necessary files in a single archive (e.g., ZIP or TAR) and provide clear instructions for building and running your application. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment