nonsense part added

This commit is contained in:
grillazz
2021-06-13 12:28:22 +02:00
parent 5fd22a407d
commit 067ba777b3
8 changed files with 160 additions and 3 deletions

View File

@@ -0,0 +1,2 @@
from the_app.models.nonsense import Nonsense
from the_app.models.stuff import Stuff

View File

@@ -16,6 +16,11 @@ class Base:
return cls.__name__.lower()
async def save(self, db_session: AsyncSession):
"""
:param db_session:
:return:
"""
try:
db_session.add(self)
return await db_session.commit()
@@ -23,6 +28,11 @@ class Base:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex))
async def delete(self, db_session: AsyncSession):
"""
:param db_session:
:return:
"""
try:
await db_session.delete(self)
await db_session.commit()
@@ -31,6 +41,12 @@ class Base:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex))
async def update(self, db_session: AsyncSession, **kwargs):
"""
:param db_session:
:param kwargs:
:return:
"""
for k, v in kwargs.items():
setattr(self, k, v)
await self.save(db_session)

View File

@@ -0,0 +1,38 @@
import uuid
from fastapi import HTTPException, status
from sqlalchemy import Column, String, select
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from the_app.models.base import Base
class Nonsense(Base):
__tablename__ = "nonsense"
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
name = Column(String, nullable=False, primary_key=True, unique=True)
description = Column(String, nullable=False)
def __init__(self, name: str, description: str):
self.name = name
self.description = description
@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

View File

@@ -20,6 +20,12 @@ class Stuff(Base):
@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()