# NIKAME GENERATED — DO NOT EDIT DIRECTLY
import grpc
from concurrent import futures
import {{ project_name }}_pb2
import {{ project_name }}_pb2_grpc
from sqlalchemy import select
from app.db.session import SessionLocal
from app.models import {% for model_name in models %}{{ model_name | lower }}{% if not loop.last %}, {% endif %}{% endfor %}

class {{ model_name }}Servicer({{ project_name }}_pb2_grpc.{{ model_name }}ServiceServicer):
    async def Get{{ model_name }}s(self, request, context):
        async with SessionLocal() as session:
            result = await session.execute(select(models.{{ model_name }}))
            items = result.scalars().all()
            return {{ project_name }}_pb2.Get{{ model_name }}sResponse(
                items=[{{ project_name }}_pb2.{{ model_name }}(**item.to_dict()) for item in items]
            )

async def serve():
    server = grpc.aio.server()
    {{ project_name }}_pb2_grpc.add_{{ model_name }}ServiceServicer_to_server({{ model_name }}Servicer(), server)
    server.add_insecure_port('[::]:50051')
    await server.start()
    await server.wait_for_termination()
