""" In-memory data store using Pydantic models. This module is NOT thread-safe and is intended for demos and scaffolds only. This module provides a minimal, process-local data store for the model-based CRUD example application. It stores and returns domain objects defined using Pydantic models and is intended solely for demonstration and scaffolding purposes. The implementation intentionally avoids: - persistence - concurrency guarantees - transactional semantics - validation beyond what Pydantic provides It is not part of the ``openapi_first`` library API surface. """ from typing import Dict from models import Item, ItemCreate # In-memory storage keyed by item ID. _items: Dict[int, Item] = { 1: Item(id=1, name="Apple", price=0.5), 2: Item(id=2, name="Banana", price=0.3), } # Auto-incrementing identifier. _next_id = 3 def list_items() -> list[Item]: """ Return all items in the data store. Returns ------- list[Item] A list of item domain objects. """ return list(_items.values()) def get_item(item_id: int) -> Item: """ Retrieve a single item by ID. Parameters ---------- item_id : int Identifier of the item to retrieve. Returns ------- Item The requested item. Raises ------ KeyError If the item does not exist. """ return _items[item_id] def create_item(payload: ItemCreate) -> Item: """ Create a new item in the data store. A new identifier is assigned automatically. No additional validation is performed beyond Pydantic model validation. Parameters ---------- payload : ItemCreate Data required to create a new item. Returns ------- Item The newly created item. """ global _next_id item = Item(id=_next_id, **payload.model_dump()) _items[_next_id] = item _next_id += 1 return item def update_item(item_id: int, payload: ItemCreate) -> Item: """ Replace an existing item in the data store. This function performs a full replacement of the stored item. Partial updates are not supported. Parameters ---------- item_id : int Identifier of the item to update. payload : ItemCreate New item data. Returns ------- Item The updated item. Raises ------ KeyError If the item does not exist. """ if item_id not in _items: raise KeyError(item_id) item = Item(id=item_id, **payload.model_dump()) _items[item_id] = item return item def delete_item(item_id: int) -> None: """ Remove an item from the data store. Parameters ---------- item_id : int Identifier of the item to delete. Raises ------ KeyError If the item does not exist. """ del _items[item_id]