async def create(con: AsyncIOConnection, *, obj_in: ItemCreate, owner_id: UUID) -> Item: data_in = obj_in.dict(exclude_unset=True) shape_expr = utils.get_shape(data_in) try: result = await con.query_one_json( f"""SELECT ( INSERT Item {{ {shape_expr}, owner := ( SELECT User FILTER .id = <uuid>$owner_id ) }} ) {{ id, title, description, owner: {{ id, email, full_name }} }}""", **data_in, owner_id=owner_id, ) except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") item = Item.parse_raw(result) return item
async def create_with_owner( con: AsyncIOConnection, *, obj_in: ItemCreate, owner_id: UUID ) -> Item: try: result = await con.fetchone_json( """SELECT ( INSERT Item { title := <str>$title, description := <str>$description, owner := ( SELECT User FILTER .id = <uuid>$owner_id ) } ) { id, title, description, owner: { id, full_name, email } }""", title=obj_in.title, description=obj_in.description, owner_id=owner_id, ) except Exception as e: print(f"EXCEPTION: {e}") item = Item.parse_raw(result) return item
async def update( con: AsyncIOConnection, *, id: UUID, obj_in: ItemUpdate ) -> Optional[Item]: data_in = obj_in.dict(exclude_unset=True) if not data_in: item = await get(con, id=id) return item shape_expr = utils.get_shape(data_in) try: result = await con.query_one_json( f"""SELECT ( UPDATE Item FILTER .id = <uuid>$id SET {{ {shape_expr} }} ) {{ id, title, description, owner: {{ id, email, full_name }} }}""", id=id, **data_in, ) except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") item = Item.parse_raw(result) return item
async def get(con: AsyncIOConnection, *, id: UUID) -> Optional[Item]: try: result = await con.fetchone_json( """SELECT Item { id, title, description, owner: { id, full_name, email } } FILTER .id = <uuid>$id""", id=id, ) except NoDataError: return None except Exception as e: print(f"EXCEPTION: {e}") item = Item.parse_raw(result) return item
async def remove(con: AsyncIOConnection, *, id: UUID) -> Item: try: result = await con.fetchone_json( """SELECT ( DELETE Item FILTER .id = <uuid>$id ) { id, title, description, owner: { id, full_name, email } }""", id=id, ) except Exception as e: print(f"EXCEPTION: {e}") item = Item.parse_raw(result) return item
async def update(con: AsyncIOConnection, *, db_obj: Item, obj_in: ItemUpdate) -> Item: update_data = obj_in.dict(exclude_unset=True) shape = ", ".join([k + db.type_cast(update_data[k]) + k for k in update_data]) try: result = await con.fetchone_json( f"""SELECT ( UPDATE Item FILTER .id = <uuid>$id SET {{ {shape} }} ) {{ id, title, description, owner: {{ id, full_name, email }} }}""", id=db_obj.id, **update_data, ) except Exception as e: print(f"EXCEPTION: {e}") item = Item.parse_raw(result) return item
async def get(con: AsyncIOConnection, *, id: UUID) -> Optional[Item]: try: result = await con.query_one_json( """SELECT Item { id, title, description, owner: { id, email, full_name } } FILTER .id = <uuid>$id""", id=id, ) except NoDataError: return None except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") item = Item.parse_raw(result) return item
async def remove(con: AsyncIOConnection, *, id: UUID) -> Item: try: result = await con.query_one_json( """SELECT ( DELETE Item FILTER .id = <uuid>$id ) { id, title, description, owner: { id, email, full_name } }""", id=id, ) except Exception as e: raise HTTPException(status_code=400, detail=f"{e}") item = Item.parse_raw(result) return item