update object in database with fastapi

Viewed 4118

i am new to fastapi, and i want to be able to update my bank account information when a deposit is made. i am not sure how to do this as i am not sure how patch works. the logic used in deposit is that i used in create a bank account (obviously they should not work the same). this is my model:

class Account(Base):
   __tablename__ = "accounts"

   id = Column(Integer, primary_key=True, index=True)
   name = Column(String)
   deposits_made = Column(Integer)
   total = Column(Integer)

class PaymentRequest(BaseModel):
   bank_id: str
   amount: int

def get_db():
   try:
       db = SessionLocal()
       yield db
   finally:
       db.close()

@app.patch("/deposit")
def deposit(request: PaymentRequest, db: Session = Depends(get_db)):

   account = db.query(Account)
   account = accounts[request.id]
   account.total += request.amount
   account.deposits_made += 1
   db.add(account)
   db.commit() 
   return{
       "code":"success",
       "message":"donation made"
   }
2 Answers

Your table needs to be specific


class Account(Base):
   __tablename__ = "accounts"

   id = Column(Integer, primary_key=True, index=True)
   name = Column(String)
   deposits_made = Column(Integer)
   total = Column(Integer)

Your schema should be a good reflection of your table but if you like to go with what you have:

class PaymentRequest(BaseModel):
   bank_id: str
   amount: int

then some calculations have to be made to be able to have a successful table update because what must go to the table is:

{'name':name, 'deposits_made':deposits_made, 'total ':total}

remember, your incoming data is just:

bank_id: str
amount: int

and your table does not have provision made for such, that being said,

if bank_id is the same as the name on the Account table you should have given it a better name at least depositor_name, unique, ...

when you have decided to make changes to your table, the code below will help:


@app.put("/deposit/{id}")
def deposit(id, request:schemas.PaymentRequest, db:Session=Depends(get_db)):
    old_account = db.query(models.Account).filter(models.Account.id == id)
    if not old_account.first():
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f'old_account with the id {id} is not available')
    old_account.update({'name':name, 'deposits_made':deposits_made, 'total ':total})
    db.commit()
    return {"code":"success","message":"donation made"}

The idea of a PUT request is to update the data of a certain object.

First things first, for REST APIs, it is typical to reference the object in the url, e.. via an id. Your url would become something like this domain.com/path/to/resource/id

https://www.edureka.co/blog/what-is-rest-api/

This being said, your code will now look as follows

@app.patch("/deposit/{id}")
def deposit(request: PaymentRequest, db: Session = Depends(get_db)):

   # Not sure what kind of orm you are using, but you get the idea
   account = db.query(Account.id=id)
   account = accounts[request.id]
   account.total += request.amount
   account.deposits_made += 1
   db.add(account)
   db.commit() 
   return{
       "code":"success",
       "message":"donation made"
   }
Related