This commit is contained in:
2024-03-04 10:15:28 +00:00
parent 4df5770e76
commit 92c52954c8
25 changed files with 80 additions and 114 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,48 +1,9 @@
from contextlib import AbstractContextManager
from typing import Callable
from model.user import User
from sqlalchemy.orm import Session
from api.uow.uow_base import UowBase
class UserRepository:
def __init__(self, session_factory: Callable[..., AbstractContextManager[Session]]) -> None:
self.session_factory = session_factory
def __init__(self, uow: UowBase) -> None:
self.uow = uow
def get_all(self):
with self.session_factory() as session:
return session.query(User).all()
def get_by_id(self, user_id: int) -> User:
with self.session_factory() as session:
user = session.query(User).filter(User.id == user_id).first()
if not user:
raise UserNotFoundError(user_id)
return user
def add(self, email: str, password: str, is_active: bool = True) -> User:
with self.session_factory() as session:
user = User(email=email, hashed_password=password, is_active=is_active)
session.add(user)
session.commit()
session.refresh(user)
return user
def delete_by_id(self, user_id: int) -> None:
with self.session_factory() as session:
entity: User = session.query(User).filter(User.id == user_id).first()
if not entity:
raise UserNotFoundError(user_id)
session.delete(entity)
session.commit()
class NotFoundError(Exception):
entity_name: str
def __init__(self, entity_id):
super().__init__(f"{self.entity_name} not found, id: {entity_id}")
class UserNotFoundError(NotFoundError):
entity_name: str = "User"
async def get_all_users(self):
return await self.uow.get_all_users()