26 lines
1.0 KiB
Python
26 lines
1.0 KiB
Python
from sqlalchemy import text
|
|
from sqlalchemy.orm import Session
|
|
|
|
from flask_demo_api.protocols.models import Gender, GenderResponse, PeopleRequest
|
|
from flask_demo_api.protocols.repository import RepositoryGateway
|
|
|
|
|
|
class SqlalchemyRepositoryGateway(RepositoryGateway):
|
|
def __init__(self, session: Session) -> None:
|
|
self.session = session
|
|
|
|
def get_gender(self, people: PeopleRequest) -> GenderResponse | None:
|
|
stmt = text("""SELECT * FROM names_man WHERE name = :name""")
|
|
man_result = self.session.execute(stmt, {"name": people.name})
|
|
man_result = man_result.mappings().one_or_none()
|
|
if man_result:
|
|
return GenderResponse(id=people.id, gender=Gender.MALE)
|
|
|
|
stmt = text("""SELECT * FROM names_woman WHERE name = :name""")
|
|
man_result = self.session.execute(stmt, {"name": people.name})
|
|
man_result = man_result.mappings().one_or_none()
|
|
if man_result:
|
|
return GenderResponse(id=people.id, gender=Gender.FEMALE)
|
|
|
|
return None
|