def get(name: str): try: fruit: Fruit = SESSION.query(Fruit).filter(Fruit.name == name).one() return JSONResponse(fruit.dumps(), 200) except Exception as error: SESSION.rollback() logger.error(error) return PlainTextResponse("Bad Request", 400)
def add_user(user_id, user_name, first_name, last_name): users = SESSION.query(Users).get(user_id) if users: return False else: users = Users(user_id, user_name, first_name, last_name) SESSION.add(users) SESSION.commit() return True
def put(payload: Payload): try: fruit: Fruit = SESSION.query(Fruit).filter( Fruit.name == payload.name).one() fruit.count = payload.count SESSION.commit() return PlainTextResponse("OK", 200) except Exception as error: SESSION.rollback() logger.error(error) return PlainTextResponse("Bad Request", 400)
def init_db(): SESSION.merge(Fruit(**{"name": "apple", "count": 1})) SESSION.commit()
def add(self): SESSION.add(self)
def query(cls): return SESSION.query(cls)
def commit(): SESSION.commit()
def clean_db(): SESSION.query(Fruit).delete()
def get_all_users(): users = SESSION.query(Users).all() SESSION.commit() return users