mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
refactor base model crud meths
This commit is contained in:
parent
8a87f15ff5
commit
63014e11db
@ -4,7 +4,7 @@ from asyncpg import UniqueViolationError
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||
from sqlalchemy.orm import as_declarative, declared_attr
|
||||
|
||||
|
||||
@as_declarative()
|
||||
@ -53,16 +53,19 @@ class Base:
|
||||
except SQLAlchemyError as ex:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
|
||||
async def update(self, db_session: AsyncSession, **kwargs):
|
||||
async def update(self, db: AsyncSession, **kwargs):
|
||||
"""
|
||||
|
||||
:param db_session:
|
||||
:param kwargs:
|
||||
:param db:
|
||||
:param kwargs
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
for k, v in kwargs.items():
|
||||
setattr(self, k, v)
|
||||
await self.save(db_session)
|
||||
return await db.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
|
||||
async def save_or_update(self, db: AsyncSession):
|
||||
try:
|
||||
|
Loading…
x
Reference in New Issue
Block a user