Documentation
Learn
Repository

Haunted Houses Repository

In this section, we'll delve into the implementation details of the HauntedHousesRepository.

This repository acts as a mock storage system for haunted houses, providing basic methods for retrieving, creating, updating, and deleting haunted houses.

This is not exactly pest-specific, but we will use it in this getting-started guide to simulate a database or any other storage system.

We will use an in-memory list to simulate a database and to provide CRUD (Create, Read, Update, Delete) operations for haunted house entities.

~/haunted-houses
 mkdir haunted_houses/houses
 touch haunted_houses/houses/repository.py

The model

Let's start by defining the model for our haunted houses. We'll use pydantic for this:

haunted_houses/houses/repository.py
from pydantic import BaseModel
 
class HauntedHouse(BaseModel):
    """Model for a spooky house"""
    id: int
    name: str
    ghost_count: int

The repository

Now, let's move on to creating the repository. Our approach is simple: we'll use a basic list to store information about our haunted houses.

haunted_houses/houses/repository.py
from pydantic import BaseModel
 
class HauntedHouse(BaseModel):
    """A spooky house"""
    id: int
    name: str
    ghost_count: int
 
class HauntedHousesRepository:
    """Mock repository for haunted houses"""
 
    def __init__(self):
        self.houses: list[HauntedHouse] = [
            HauntedHouse(id=1, name="Spooky Manor", ghost_count=5),
            HauntedHouse(id=2, name="Eerie Estate", ghost_count=3),
        ]

The __init__ method initializes the repository with a predefined list of haunted houses, where each haunted house is represented by an instance of the HauntedHouse class.

CRUD operations

With our haunted houses repository in place, it's time to implement the standard CRUD (Create, Read, Update, Delete) operations. Let's dive into the code and bring our repository to life.

"Talk is cheap. Show me the code." - Linus Torvalds

haunted_houses/houses/repository.py
from pydantic import BaseModel
 
class HauntedHouse(BaseModel):
    """A spooky house"""
    id: int
    name: str
    ghost_count: int
 
class HauntedHousesRepository:
    """Mock repository for haunted houses"""
 
    def __init__(self):
        self.houses: list[HauntedHouse] = [
            HauntedHouse(id=1, name="Spooky Manor", ghost_count=5),
            HauntedHouse(id=2, name="Eerie Estate", ghost_count=3),
        ]
 
    def get_all_houses(self) -> list[HauntedHouse]:
        return self.houses
 
    def get_house_by_id(self, house_id: int) -> HauntedHouse | None:
        return next((house for house in self.houses if house.id == house_id), None)
 
    def create_house(self, house: HauntedHouse) -> HauntedHouse:
        new_id = max(house.id for house in self.houses) + 1
        new_house = HauntedHouse(id=new_id, name=house.name, ghost_count=house.ghost_count)
        self.houses.append(new_house)
        return new_house
 
    def update_house(self, house_id: int, updated_house: HauntedHouse) -> HauntedHouse | None:
        house_index = next(
            (index for index, house in enumerate(self.houses) if house.id == house_id),
            None
        )
        if house_index is not None:
            self.houses[house_index] = updated_house
            return updated_house
 
    def delete_house(self, house_id: int) -> None:
        self.houses = [house for house in self.houses if house.id != house_id]
 
  • get_all_houses: Returns a list containing all the haunted houses currently stored in the repository.

  • get_house_by_id: Retrieves a specific haunted house from the repository based on its unique identifier.

  • create_house: Adds a new haunted house to the repository.

  • update_house: Modifies the details of an existing haunted house in the repository.

  • delete_house: Removes a haunted house from the repository.