-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathnonsense.py
41 lines (35 loc) · 1.26 KB
/
nonsense.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import uuid
from fastapi import HTTPException, status
from sqlalchemy import String, select
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class Nonsense(Base):
__tablename__ = "nonsense"
__table_args__ = ({"schema": "happy_hog"},)
id: Mapped[uuid:UUID] = mapped_column(
UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True
)
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
description: Mapped[str | None]
# TODO: apply relation to other tables
@classmethod
async def find(cls, db_session: AsyncSession, name: str):
"""
:param db_session:
:param name:
:return:
"""
stmt = select(cls).where(cls.name == name)
result = await db_session.execute(stmt)
instance = result.scalars().first()
if instance is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"Record not found": f"There is no record for requested name value : {name}"
},
)
else:
return instance