async def delete_shopping_list_item( list_id: int, item_id: int, user: UserModel = Depends(get_current_active_user)): shopping_list = await ShoppingListModel.query.where( db.and_( ShoppingListModel.user_id == user.id, ShoppingListModel.id == list_id, )).gino.first() if not shopping_list: raise HTTPException(status_code=404, detail="Shopping list not found") shopping_list_item = await ShoppingItemModel.query.where( db.and_( ShoppingItemModel.user_id == user.id, ShoppingItemModel.shopping_list_id == list_id, )).gino.first() if not shopping_list_item: raise HTTPException(status_code=404, detail="Shopping list item not found") await shopping_list_item.delete() return {}, 204
async def get_shopping_list( user: UserModel = Depends(get_current_active_user)): shopping_lists = await ShoppingListModel.query.where( db.and_(ShoppingListModel.user_id == user.id, )).limit(200).gino.all() if not shopping_lists: raise HTTPException(status_code=404, detail="Shopping list not found") return ShoppingListsSchema.parse_obj( {'objects': [x.to_dict() for x in shopping_lists]})
async def get_shopping_list( list_id: int, user: UserModel = Depends(get_current_active_user)): shopping_list = await ShoppingListModel.query.where( db.and_( ShoppingListModel.user_id == user.id, ShoppingListModel.id == list_id, )).gino.first() if not shopping_list: raise HTTPException(status_code=404, detail="Shopping list not found") return ShoppingListSchema.parse_obj(shopping_list.to_dict())
async def get_todo(todo_id: int, user: UserModel = Depends(get_current_active_user)): todo = await TodoModel.query.where( db.and_( TodoModel.user_id == user.id, TodoModel.id == todo_id, )).gino.first() if not todo: raise HTTPException(status_code=404, detail="Todos are not found") return TodoSchema.parse_obj(todo.to_dict())
async def get_shopping_list_items( list_id: int, user: UserModel = Depends(get_current_active_user)): shopping_list = await ShoppingListModel.query.where( db.and_( ShoppingListModel.user_id == user.id, ShoppingListModel.id == list_id, )).gino.first() if not shopping_list: raise HTTPException(status_code=404, detail="Shopping list not found") shopping_list_items = await ShoppingItemModel.query.where( db.and_( ShoppingItemModel.user_id == user.id, ShoppingItemModel.shopping_list_id == list_id, )).limit(200).gino.all() return ShoppingItemsSchema.parse_obj({ 'shopping_list': shopping_list.to_dict(), 'objects': [x.to_dict() for x in shopping_list_items], })
async def edit_todo(todo_id: int, todo_schema: TodoSchema, user: UserModel = Depends(get_current_active_user)): todo = await TodoModel.query.where( db.and_( TodoModel.user_id == user.id, TodoModel.id == todo_id, )).gino.first() if not todo: raise HTTPException(status_code=404, detail="Todos are not found") await todo.update( text=todo_schema.text, completed=todo_schema.completed, ).apply() return TodoSchema.parse_obj(todo.to_dict())
async def get_todos(user: UserModel = Depends(get_current_active_user)): todos = await TodoModel.query.where(db.and_(TodoModel.user_id == user.id, ) ).order_by(TodoModel.id.desc() ).limit(200).gino.all() return TodosSchema.parse_obj({'objects': [x.to_dict() for x in todos]})