Skip to content

Commit

Permalink
feat: add repositories to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahriyarR committed Oct 23, 2022
1 parent d4b7c03 commit bdc416d
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 106 deletions.
7 changes: 4 additions & 3 deletions src/jobboard/adapters/db/unit_of_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from sqlalchemy.orm.session import Session

from src.jobboard.adapters.db import repository
from src.jobboard.adapters.repositories.users import UserSqlAlchemyRepository
from src.jobboard.adapters.repositories.jobs import JobSqlAlchemyRepository
from src.jobboard.domain.ports.unit_of_work import (
JobUnitOfWorkInterface,
UserUnitOfWorkInterface,
Expand All @@ -15,7 +16,7 @@ def __init__(self, session_factory: Callable[[], Any]):

def __enter__(self):
self.session = self.session_factory() # type: Session
self.users = repository.UserSqlAlchemyRepository(self.session)
self.users = UserSqlAlchemyRepository(self.session)
return super().__enter__()

def __exit__(self, *args):
Expand All @@ -35,7 +36,7 @@ def __init__(self, session_factory: Callable[[], Any]):

def __enter__(self):
self.session = self.session_factory() # type: Session
self.jobs = repository.JobSqlAlchemyRepository(self.session)
self.jobs = JobSqlAlchemyRepository(self.session)
return super().__enter__()

def __exit__(self, *args):
Expand Down
14 changes: 7 additions & 7 deletions src/jobboard/adapters/entrypoints/api/v1/route_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from src.jobboard.configurator.containers import Container
from src.jobboard.domain.model.model import User
from src.jobboard.domain.ports.common.responses import ResponseTypes
from src.jobboard.domain.ports.use_cases.jobs import JobsServiceInterface
from src.jobboard.domain.ports.use_cases.jobs import JobServiceInterface
from src.jobboard.domain.schemas.jobs import JobCreateInputDto

router = APIRouter()
Expand All @@ -25,7 +25,7 @@
def create_job(
job: JobCreateInputDto,
current_user: User = Depends(get_current_user_from_token),
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
response = job_service.create(job=job, owner_id=current_user.id)
data = jsonable_encoder(response.value)
Expand All @@ -40,7 +40,7 @@ def create_job(
@inject
def read_job(
id: int,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
response = job_service.retrieve_job(id_=id)
data = jsonable_encoder(response.value)
Expand All @@ -54,7 +54,7 @@ def read_job(
@router.get("/all")
@inject
def read_jobs(
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
response = job_service.list_jobs()
data = jsonable_encoder(response.value)
Expand All @@ -71,7 +71,7 @@ def update_job(
id: int,
job: JobCreateInputDto,
current_user: User = Depends(get_current_user_from_token),
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
response = job_service.update_job_by_id(id_=id, job=job, owner_id=current_user.id)
data = jsonable_encoder(response.value)
Expand All @@ -87,7 +87,7 @@ def update_job(
def delete_job(
id: int,
current_user: User = Depends(get_current_user_from_token),
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
response = job_service.retrieve_job(id_=id)
if response.type != ResponseTypes.SUCCESS:
Expand All @@ -112,7 +112,7 @@ def delete_job(
@inject
def autocomplete(
term: Optional[str] = None,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
jobs = job_service.search_job(term)
return [job.title for job in jobs]
6 changes: 3 additions & 3 deletions src/jobboard/adapters/entrypoints/api/v1/route_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from src.jobboard.configurator.config import settings
from src.jobboard.configurator.containers import Container
from src.jobboard.configurator.security import create_access_token
from src.jobboard.domain.ports.use_cases.users import UsersServiceInterface
from src.jobboard.domain.ports.use_cases.users import UserServiceInterface
from src.jobboard.domain.schemas.tokens import Token
from src.jobboard.domain.schemas.users import UserLoginInputDto, UserOutputDto

Expand All @@ -21,7 +21,7 @@
def login_for_access_token(
response: Response,
form_data: OAuth2PasswordRequestForm = Depends(),
user_service: UsersServiceInterface = Depends(Provide[Container.user_service]),
user_service: UserServiceInterface = Depends(Provide[Container.user_service]),
):
user = UserLoginInputDto(email=form_data.username, password=form_data.password)
user = user_service.authenticate_user(user)
Expand All @@ -46,7 +46,7 @@ def login_for_access_token(
@inject
def get_current_user_from_token(
token: str = Depends(oauth2_scheme),
user_service: UsersServiceInterface = Depends(Provide[Container.user_service]),
user_service: UserServiceInterface = Depends(Provide[Container.user_service]),
):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
4 changes: 2 additions & 2 deletions src/jobboard/adapters/entrypoints/api/v1/route_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from src.jobboard.adapters.entrypoints import STATUS_CODES
from src.jobboard.configurator.containers import Container
from src.jobboard.domain.ports.use_cases.users import UsersServiceInterface
from src.jobboard.domain.ports.use_cases.users import UserServiceInterface
from src.jobboard.domain.schemas.users import UserCreateInputDto

router = APIRouter()
Expand All @@ -16,7 +16,7 @@
@inject
def create_user(
user: UserCreateInputDto,
user_service: UsersServiceInterface = Depends(Provide[Container.user_service]),
user_service: UserServiceInterface = Depends(Provide[Container.user_service]),
):
response = user_service.create(user=user)
data = jsonable_encoder(response.value)
Expand Down
12 changes: 6 additions & 6 deletions src/jobboard/adapters/entrypoints/webapps/jobs/route_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from src.jobboard.adapters.entrypoints.webapps.jobs.forms import JobCreateForm
from src.jobboard.configurator.containers import Container
from src.jobboard.domain.model.model import User
from src.jobboard.domain.ports.use_cases.jobs import JobsServiceInterface
from src.jobboard.domain.ports.use_cases.jobs import JobServiceInterface
from src.jobboard.domain.schemas.jobs import JobCreateInputDto

templates = Jinja2Templates(directory="src/jobboard/adapters/entrypoints/templates")
Expand All @@ -22,7 +22,7 @@
@inject
async def home(
request: Request,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
msg: Optional[str] = None,
):
jobs = job_service.list_jobs()
Expand All @@ -36,7 +36,7 @@ async def home(
def job_detail(
id: int,
request: Request,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
job = job_service.retrieve_job(id_=id)
return templates.TemplateResponse(
Expand All @@ -53,7 +53,7 @@ def create_job(request: Request):
@inject
async def create_job(
request: Request,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
form = JobCreateForm(request)
await form.load_data()
Expand Down Expand Up @@ -82,7 +82,7 @@ async def create_job(
@inject
def show_jobs_to_delete(
request: Request,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
):
jobs = job_service.list_jobs()
return templates.TemplateResponse(
Expand All @@ -94,7 +94,7 @@ def show_jobs_to_delete(
@inject
def search(
request: Request,
job_service: JobsServiceInterface = Depends(Provide[Container.job_service]),
job_service: JobServiceInterface = Depends(Provide[Container.job_service]),
query: Optional[str] = None,
):
jobs = job_service.search_job(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from src.jobboard.adapters.entrypoints.webapps.users.forms import UserCreateForm
from src.jobboard.configurator.containers import Container
from src.jobboard.domain.ports.use_cases.users import UsersServiceInterface
from src.jobboard.domain.ports.use_cases.users import UserServiceInterface
from src.jobboard.domain.schemas.users import UserCreateInputDto

templates = Jinja2Templates(directory="src/jobboard/adapters/entrypoints/templates")
Expand All @@ -21,7 +21,7 @@ def register(request: Request):
@inject
async def register(
request: Request,
user_service: UsersServiceInterface = Depends(Provide[Container.user_service]),
user_service: UserServiceInterface = Depends(Provide[Container.user_service]),
):
form = UserCreateForm(request)
await form.load_data()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
from src.jobboard.domain.model import model
from src.jobboard.domain.ports.repository import (
from src.jobboard.domain.ports.repositories.jobs import (
JobRepositoryInterface,
UserRepositoryInterface,
)


class UserSqlAlchemyRepository(UserRepositoryInterface):
def __init__(self, session):
super().__init__()
self.session = session

def _add(self, user):
self.session.add(user)

def _get(self, user_name: str):
return self.session.query(model.User).filter_by(user_name=user_name).first()

def _get_by_email(self, email: str) -> model.User:
return self.session.query(model.User).filter_by(email=email).first()

def _get_by_id(self, id: int) -> model.User:
return self.session.query(model.User).filter_by(id=id).first()


class JobSqlAlchemyRepository(JobRepositoryInterface):
def __init__(self, session):
super().__init__()
Expand All @@ -44,4 +25,4 @@ def _get_all(self) -> list[model.Job]:
return self.session.query(model.Job).all()

def _search(self, query: str) -> list[model.Job]:
return self.session.query(model.Job).filter(model.Job.title.contains(query))
return self.session.query(model.Job).filter(model.Job.title.contains(query))
22 changes: 22 additions & 0 deletions src/jobboard/adapters/repositories/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from src.jobboard.domain.model import model
from src.jobboard.domain.ports.repositories.users import (
UserRepositoryInterface,
)


class UserSqlAlchemyRepository(UserRepositoryInterface):
def __init__(self, session):
super().__init__()
self.session = session

def _add(self, user):
self.session.add(user)

def _get(self, user_name: str):
return self.session.query(model.User).filter_by(user_name=user_name).first()

def _get_by_email(self, email: str) -> model.User:
return self.session.query(model.User).filter_by(email=email).first()

def _get_by_id(self, id: int) -> model.User:
return self.session.query(model.User).filter_by(id=id).first()
4 changes: 2 additions & 2 deletions src/jobboard/adapters/use_cases/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ResponseTypes,
)
from src.jobboard.domain.ports.unit_of_work import JobUnitOfWorkInterface
from src.jobboard.domain.ports.use_cases.jobs import JobsServiceInterface
from src.jobboard.domain.ports.use_cases.jobs import JobServiceInterface
from src.jobboard.domain.schemas.jobs import JobCreateInputDto, JobOutputDto


Expand All @@ -27,7 +27,7 @@ def _handle_response_failure(
)


class JobsService(JobsServiceInterface):
class JobService(JobServiceInterface):
def __init__(self, uow: JobUnitOfWorkInterface):
self.uow = uow

Expand Down
4 changes: 2 additions & 2 deletions src/jobboard/adapters/use_cases/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
ResponseTypes,
)
from src.jobboard.domain.ports.unit_of_work import UserUnitOfWorkInterface
from src.jobboard.domain.ports.use_cases.users import UsersServiceInterface
from src.jobboard.domain.ports.use_cases.users import UserServiceInterface
from src.jobboard.domain.schemas.users import (
UserCreateInputDto,
UserLoginInputDto,
UserOutputDto,
)


class UsersService(UsersServiceInterface):
class UserService(UserServiceInterface):
def __init__(self, uow: UserUnitOfWorkInterface):
self.uow = uow

Expand Down
8 changes: 4 additions & 4 deletions src/jobboard/configurator/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
JobSqlAlchemyUnitOfWork,
UserSqlAlchemyUnitOfWork,
)
from src.jobboard.adapters.use_cases.jobs import JobsService
from src.jobboard.adapters.use_cases.users import UsersService
from src.jobboard.adapters.use_cases.jobs import JobService
from src.jobboard.adapters.use_cases.users import UserService
from src.jobboard.configurator import config


Expand Down Expand Up @@ -35,8 +35,8 @@ class Container(containers.DeclarativeContainer):
)

user_service = providers.Factory(
UsersService,
UserService,
uow=user_uow,
)

job_service = providers.Factory(JobsService, uow=job_uow)
job_service = providers.Factory(JobService, uow=job_uow)
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,6 @@
from src.jobboard.domain.model import model


class UserRepositoryInterface(abc.ABC):
def __init__(self):
self.seen = set() # type: Set[model.User]

def add(self, user: model.User):
self._add(user)
self.seen.add(user)

def get(self, user_name: str) -> model.User:
user = self._get(user_name)
if user:
self.seen.add(user)
return user

def get_by_email(self, email: str) -> model.User:
user = self._get_by_email(email)
if user:
self.seen.add(user)
return user

def get_by_id(self, id: int) -> model.User:
user = self._get_by_id(id)
if user:
self.seen.add(user)
return user

@abc.abstractmethod
def _add(self, user: model.User):
raise NotImplementedError

@abc.abstractmethod
def _get(self, user_name: str) -> model.User:
raise NotImplementedError

@abc.abstractmethod
def _get_by_email(self, email: str) -> model.User:
raise NotImplementedError

@abc.abstractmethod
def _get_by_id(self, id: int) -> model.User:
raise NotImplementedError


class JobRepositoryInterface(abc.ABC):
def __init__(self):
self.seen = set() # type: Set[model.Job]
Expand Down
Loading

0 comments on commit bdc416d

Please sign in to comment.