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 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)
|
||||
async def update_stuff(
|
||||
stuff: StuffSchema,
|
||||
payload: StuffSchema,
|
||||
name: str,
|
||||
db_session: AsyncSession = Depends(get_db),
|
||||
):
|
||||
stuff_instance = await Stuff.find(db_session, name)
|
||||
stuff_instance = await stuff_instance.update(db_session, stuff)
|
||||
return stuff_instance
|
||||
stuff = await Stuff.find(db_session, name)
|
||||
await stuff.update(db_session, **payload.dict())
|
||||
return stuff
|
||||
|
@ -1,7 +1,6 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from icecream import ic
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||
@ -30,3 +29,8 @@ class Base:
|
||||
return True
|
||||
except SQLAlchemyError as 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
|
||||
|
||||
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
|
||||
from the_app.schemas.stuff import StuffSchema
|
||||
|
||||
|
||||
class Stuff(Base):
|
||||
@ -18,14 +18,14 @@ class Stuff(Base):
|
||||
self.name = name
|
||||
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
|
||||
async def find(cls, db_session: AsyncSession, name: str):
|
||||
stmt = select(cls).where(cls.name == name)
|
||||
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