Program/mooc/crud/crud_account.py
烟雨如花 4f34f5b8be init
2024-12-31 22:27:04 +08:00

34 lines
1.1 KiB
Python

from sqlalchemy.orm import Session
from typing import Optional
from mooc.models.account import AccountWechats
from mooc.schemas.account import AccountWechatsCreate, AccountWechatsUpdate
class CRUDAccountWechats:
def create(self, db: Session, obj_in: AccountWechatsCreate) -> AccountWechats:
db_obj = AccountWechats(**obj_in.dict())
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def get(self, db: Session, acid: int) -> Optional[AccountWechats]:
return db.query(AccountWechats).filter(AccountWechats.acid == acid).first()
def update(
self, db: Session, *, db_obj: AccountWechats, obj_in: AccountWechatsUpdate
) -> AccountWechats:
for field, value in obj_in.dict(exclude_unset=True).items():
setattr(db_obj, field, value)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def delete(self, db: Session, acid: int) -> None:
obj = db.query(AccountWechats).filter(AccountWechats.acid == acid).first()
if obj:
db.delete(obj)
db.commit()
account_wechats = CRUDAccountWechats()