Skip to content

Commit 63014e1

Browse files
committed
refactor base model crud meths
1 parent 8a87f15 commit 63014e1

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

app/models/base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi import HTTPException, status
55
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
66
from sqlalchemy.ext.asyncio import AsyncSession
7-
from sqlalchemy.ext.declarative import as_declarative, declared_attr
7+
from sqlalchemy.orm import as_declarative, declared_attr
88

99

1010
@as_declarative()
@@ -53,16 +53,19 @@ async def delete(self, db_session: AsyncSession):
5353
except SQLAlchemyError as ex:
5454
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
5555

56-
async def update(self, db_session: AsyncSession, **kwargs):
56+
async def update(self, db: AsyncSession, **kwargs):
5757
"""
5858
59-
:param db_session:
60-
:param kwargs:
59+
:param db:
60+
:param kwargs
6161
:return:
6262
"""
63-
for k, v in kwargs.items():
64-
setattr(self, k, v)
65-
await self.save(db_session)
63+
try:
64+
for k, v in kwargs.items():
65+
setattr(self, k, v)
66+
return await db.commit()
67+
except SQLAlchemyError as ex:
68+
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
6669

6770
async def save_or_update(self, db: AsyncSession):
6871
try:

0 commit comments

Comments
 (0)