service_man/api/infrastructure/persistence/repositories/company_repository.py

56 lines
1.6 KiB
Python
Raw Normal View History

2024-04-10 00:33:31 +03:00
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
2024-04-23 12:08:56 +03:00
from api.domain.company.model import (
Company,
CompanyAddress,
CompanyEmail,
CompanyId,
CompanyName,
)
2024-04-10 00:33:31 +03:00
from api.domain.company.repository import CompanyRepository
2024-04-21 23:46:17 +03:00
from api.domain.user.model import UserId
2024-04-10 00:33:31 +03:00
class SqlAlchemyCompanyRepository(CompanyRepository):
def __init__(self, session: AsyncSession) -> None:
self.session = session
2024-04-21 23:46:17 +03:00
async def create_company(self, company: Company) -> None:
stmt = text(
"""INSERT INTO company (id, name, email, address, owner_id)
VALUES(:id, :name, :email, :address, :owner_id)
"""
)
await self.session.execute(
stmt,
{
"id": str(company.id.value),
"name": company.name.value,
"email": company.email.value,
"address": company.address.value,
"owner_id": str(company.owner_id.value),
},
)
2024-04-10 00:33:31 +03:00
async def get_companies_by_owner_email(self, filter: dict) -> list[Company]:
stmt = text("""SELECT * FROM companies WHERE email = :val""")
result = await self.session.execute(stmt, {"val": filter["email"]})
result = result.mappings().all()
return [
Company(
id=CompanyId(c.id),
name=CompanyName(c.name),
email=CompanyEmail(c.email),
2024-04-21 23:46:17 +03:00
address=CompanyAddress(c.address),
owner_id=UserId(c.owner_id),
2024-04-10 00:33:31 +03:00
)
for c in result
]
2024-04-08 23:55:30 +03:00
# async def get_users(self) -> list[User]:
# return []