mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
move update t0 base class
This commit is contained in:
parent
5b4d83a4aa
commit
3d3fa629b3
@ -1,4 +1,4 @@
|
|||||||
from fastapi import APIRouter, Depends, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from the_app.database import get_db
|
from the_app.database import get_db
|
||||||
@ -32,10 +32,10 @@ async def delete_stuff(name: str, db_session: AsyncSession = Depends(get_db)):
|
|||||||
|
|
||||||
@router.patch("/", response_model=StuffResponse)
|
@router.patch("/", response_model=StuffResponse)
|
||||||
async def update_stuff(
|
async def update_stuff(
|
||||||
stuff: StuffSchema,
|
payload: StuffSchema,
|
||||||
name: str,
|
name: str,
|
||||||
db_session: AsyncSession = Depends(get_db),
|
db_session: AsyncSession = Depends(get_db),
|
||||||
):
|
):
|
||||||
stuff_instance = await Stuff.find(db_session, name)
|
stuff = await Stuff.find(db_session, name)
|
||||||
stuff_instance = await stuff_instance.update(db_session, stuff)
|
await stuff.update(db_session, **payload.dict())
|
||||||
return stuff_instance
|
return stuff
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from icecream import ic
|
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||||
@ -30,3 +29,8 @@ class Base:
|
|||||||
return True
|
return True
|
||||||
except SQLAlchemyError as ex:
|
except SQLAlchemyError as ex:
|
||||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex))
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex))
|
||||||
|
|
||||||
|
async def update(self, db_session: AsyncSession, **kwargs):
|
||||||
|
for k, v in kwargs.items():
|
||||||
|
setattr(self, k, v)
|
||||||
|
await self.save(db_session)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy import Column, String, select
|
from sqlalchemy import Column, String, select
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from the_app.models.base import Base
|
from the_app.models.base import Base
|
||||||
from the_app.schemas.stuff import StuffSchema
|
|
||||||
|
|
||||||
|
|
||||||
class Stuff(Base):
|
class Stuff(Base):
|
||||||
@ -18,14 +18,14 @@ class Stuff(Base):
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
|
|
||||||
async def update(self, db_session: AsyncSession, schema: StuffSchema):
|
|
||||||
self.name = schema.name
|
|
||||||
self.description = schema.description
|
|
||||||
await self.save(db_session)
|
|
||||||
return self
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
stmt = select(cls).where(cls.name == name)
|
stmt = select(cls).where(cls.name == name)
|
||||||
result = await db_session.execute(stmt)
|
result = await db_session.execute(stmt)
|
||||||
return result.scalars().first()
|
if instance := result.scalars().first() 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user