Skip to main content
The ProcessOrderUseCase is responsible for receiving a new order and decomposing it into individual preparation tasks that are distributed across different kitchen stations.

Input Port

The use case implements the ProcessOrderPort interface:
public interface ProcessOrderPort {
    List<Task> execute(Order order);
}

Implementation

ProcessOrderUseCase.java
package com.foodtech.kitchen.application.usecases;

import com.foodtech.kitchen.application.ports.in.ProcessOrderPort;
import com.foodtech.kitchen.application.ports.out.OrderRepository;
import com.foodtech.kitchen.application.ports.out.TaskRepository;
import com.foodtech.kitchen.domain.model.Order;
import com.foodtech.kitchen.domain.model.Task;
import com.foodtech.kitchen.domain.services.TaskDecomposer;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProcessOrderUseCase implements ProcessOrderPort {

    private final OrderRepository orderRepository;
    private final TaskDecomposer taskDecomposer;
    private final TaskRepository taskRepository;

    public ProcessOrderUseCase(
            OrderRepository orderRepository,
            TaskDecomposer taskDecomposer,
            TaskRepository taskRepository
    ) {
        this.orderRepository = orderRepository;
        this.taskDecomposer = taskDecomposer;
        this.taskRepository = taskRepository;
    }

    @Override
    public List<Task> execute(Order order) {
        Order savedOrder = orderRepository.save(order);
        List<Task> tasks = taskDecomposer.decompose(savedOrder);
        taskRepository.saveAll(tasks);

        return tasks;
    }
}

Use Case Flow

1

Persist the Order

The incoming order is saved to the OrderRepository, which assigns it a unique ID.
2

Decompose into Tasks

The TaskDecomposer analyzes the order’s products and groups them by station (BAR, HOT_KITCHEN, COLD_KITCHEN), creating one task per station.
3

Save Tasks

All generated tasks are persisted to the TaskRepository with status PENDING.
4

Return Task List

The complete list of tasks is returned to the caller for tracking purposes.

Example Usage

// Create an order
List<Product> products = List.of(
    new Product("Caesar Salad", ProductType.COLD_DISH),
    new Product("Grilled Steak", ProductType.HOT_DISH),
    new Product("Orange Juice", ProductType.DRINK)
);

Order order = new Order("Table-5", products);

// Process the order
ProcessOrderPort processOrder = new ProcessOrderUseCase(
    orderRepository,
    taskDecomposer,
    taskRepository
);

List<Task> tasks = processOrder.execute(order);
// Returns 3 tasks: one for COLD_KITCHEN, one for HOT_KITCHEN, one for BAR

Dependencies

OrderRepository

Persistence port for storing orders

TaskDecomposer

Domain service that groups products by station

TaskRepository

Persistence port for storing tasks

Build docs developers (and LLMs) love