-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database interface and in-memory database infrastructure
- Loading branch information
Showing
9 changed files
with
78 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Generic, Protocol, TypeVar, Optional | ||
from uuid import UUID | ||
|
||
|
||
class HasID(Protocol): | ||
id: UUID # Enforce that any entity passed to the repository must have an `id` attribute | ||
|
||
|
||
T = TypeVar("T", bound=HasID) | ||
|
||
|
||
class Database(ABC, Generic[T]): | ||
@abstractmethod | ||
async def add(self, entity: T) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
async def get(self, id: UUID) -> Optional[T]: | ||
pass | ||
|
||
@abstractmethod | ||
async def update(self, entity: T) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
async def delete(self, id: UUID) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
async def list_all(self) -> list[T]: | ||
pass |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from application.interfaces.database import T, Database | ||
|
||
|
||
class InMemoryDatabase(Database[T]): | ||
def __init__(self) -> None: | ||
self._data: dict[UUID, T] = {} | ||
|
||
async def add(self, entity: T) -> None: | ||
self._data[entity.id] = entity | ||
|
||
async def get(self, id: UUID) -> Optional[T]: | ||
return self._data.get(id) | ||
|
||
async def update(self, entity: T) -> None: | ||
if entity.id in self._data: | ||
self._data[entity.id] = entity | ||
|
||
async def delete(self, id: UUID) -> None: | ||
if id in self._data: | ||
del self._data[id] | ||
|
||
async def list_all(self) -> list[T]: | ||
return list(self._data.values()) |
Empty file.
Empty file.