Source code for flask_boilerplate.domain.entities.entity_example

"""Module defining an example entity in the domain layer.

Entities are domain objects that have a unique identity and lifecycle. They are defined
by their attributes and behavior, and they encapsulate business logic related to their
identity and state.

This module provides an example entity `ExampleEntity` that demonstrates how to define
entities in the domain layer. Entities typically have a unique identifier and attributes
that represent their state.
"""

from dataclasses import dataclass, field
from uuid import UUID, uuid4

from flask_boilerplate.domain.errors.entities_error import EntitiesError
from flask_boilerplate.domain.primitives.entity import Entity  # Import de la classe abstraite Entity


[docs] @dataclass class EntityExample(Entity): """An example entity in the domain layer. This entity represents a domain object with a unique identity and attributes. It encapsulates business logic related to its identity and state. Attributes: name (str): The name of the entity. description (str): A description of the entity. id (UUID): The unique identifier of the entity. """ name: str description: str id: UUID = field(default_factory=uuid4) # Génère un UUID par défaut
[docs] def __post_init__(self) -> None: """ Post-initialization hook to ensure the entity is properly initialized. If no ID is provided, it will be generated by the parent class `Entity`. """ super().__init__(id=self.id) # Assure que l'ID est initialisé par Entity
[docs] def __eq__(self, other: object) -> bool: """ Compare two entities based on their unique identifier. Args: other (object): The other entity to compare with. Returns: bool: True if the entities have the same identifier, False otherwise. """ if not isinstance(other, EntityExample): return NotImplemented return self.id == other.id # L'égalité se base aussi sur l'UUID
[docs] def __hash__(self) -> int: """ Generate a hash value for the entity based on its unique identifier. Returns: int: The hash value of the entity. """ return hash(self.id)
[docs] def __str__(self) -> str: """ Return a string representation of the entity. The string is formatted as: EntityExample(id=<UUID>, name=<name>, description=<description>) This format ensures consistency and readability when displaying the entity. """ return f"EntityExample(id={self.id}, name={self.name}, description={self.description})"
[docs] def update_name(self, new_name: str) -> None: """Update the name of the entity. Args: new_name (str): The new name for the entity. """ self.name = new_name
[docs] def update_description(self, new_description: str) -> None: """Update the description of the entity. Args: new_description (str): The new description for the entity. """ self.description = new_description
[docs] def to_dict(self) -> dict[str, str]: """ Convert the entity to a dictionary representation. Returns: dict: A dictionary containing the entity's attributes. """ return { "id": str(self.id), "name": self.name, "description": self.description, }
[docs] def validate(self) -> None: """ Validate the entity's data. Raises: EntitiesError: If the entity's data is invalid. """ if not self.name or len(self.name.strip()) == 0: raise EntitiesError("Name cannot be empty.") if not self.description or len(self.description.strip()) == 0: raise EntitiesError("Description cannot be empty.")
# Add the class to __all__ for re-export in the parent module. __all__ = ["EntityExample"]