service_man/api/repositories/user.py

34 lines
938 B
Python
Raw Normal View History

2024-03-11 21:07:01 +03:00
from sqlalchemy import insert, select
from sqlalchemy.ext.asyncio.session import AsyncSession
from api.models import UserModel
2024-03-12 01:06:43 +03:00
from api.schemas.user_schema import UserReadDTO, UserWriteDTO
2024-03-11 21:07:01 +03:00
2024-03-12 01:06:43 +03:00
class UserRepository:
2024-03-11 21:07:01 +03:00
def __init__(self, session: AsyncSession):
self.session = session
2024-03-12 01:06:43 +03:00
async def add_one(
self,
data: UserWriteDTO,
):
stmt = insert(UserModel).values(**data.model_dump())
2024-03-11 21:07:01 +03:00
res = await self.session.execute(stmt)
2024-03-12 01:06:43 +03:00
return UserReadDTO.model_validate(res.scalar_one())
2024-03-11 21:07:01 +03:00
async def find_all(self):
stmt = select(UserModel)
res = await self.session.execute(stmt)
2024-03-12 01:06:43 +03:00
res = [UserReadDTO.model_validate(row) for row in res.scalars().all()]
2024-03-11 21:07:01 +03:00
return res
async def find_one(self, filter: dict):
return
async def update_one(self, filter: dict, data: dict):
return
async def delete_one(self, filter: dict):
return