From 2163bc024f1067248495ed9b5f0af02ad42d28d9 Mon Sep 17 00:00:00 2001 From: dunossauro Date: Thu, 26 Sep 2024 18:15:40 -0300 Subject: [PATCH] =?UTF-8?q?[WIP]=20atualiza=C3=A7=C3=A3o=20do=20c=C3=B3dig?= =?UTF-8?q?o=20apra=20fastapi=200.115?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../07/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/07/fast_zero/schemas.py | 5 ++++ .../08/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/08/fast_zero/schemas.py | 5 ++++ .../09/fast_zero/routers/todos.py | 27 ++++++++++--------- .../09/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/09/fast_zero/schemas.py | 11 ++++++++ .../10/fast_zero/routers/todos.py | 27 ++++++++++--------- .../10/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/10/fast_zero/schemas.py | 11 ++++++++ .../11/fast_zero/routers/todos.py | 27 ++++++++++--------- .../11/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/11/fast_zero/schemas.py | 11 ++++++++ codigo_das_aulas/11/tests/conftest.py | 1 - .../12/fast_zero/routers/todos.py | 27 ++++++++++--------- .../12/fast_zero/routers/users.py | 17 +++++++++--- codigo_das_aulas/12/fast_zero/schemas.py | 11 ++++++++ 17 files changed, 188 insertions(+), 77 deletions(-) diff --git a/codigo_das_aulas/07/fast_zero/routers/users.py b/codigo_das_aulas/07/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/07/fast_zero/routers/users.py +++ b/codigo_das_aulas/07/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/07/fast_zero/schemas.py b/codigo_das_aulas/07/fast_zero/schemas.py index c4279227..af51a11f 100644 --- a/codigo_das_aulas/07/fast_zero/schemas.py +++ b/codigo_das_aulas/07/fast_zero/schemas.py @@ -29,3 +29,8 @@ class Token(BaseModel): class TokenData(BaseModel): username: str | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 diff --git a/codigo_das_aulas/08/fast_zero/routers/users.py b/codigo_das_aulas/08/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/08/fast_zero/routers/users.py +++ b/codigo_das_aulas/08/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/08/fast_zero/schemas.py b/codigo_das_aulas/08/fast_zero/schemas.py index c4279227..af51a11f 100644 --- a/codigo_das_aulas/08/fast_zero/schemas.py +++ b/codigo_das_aulas/08/fast_zero/schemas.py @@ -29,3 +29,8 @@ class Token(BaseModel): class TokenData(BaseModel): username: str | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 diff --git a/codigo_das_aulas/09/fast_zero/routers/todos.py b/codigo_das_aulas/09/fast_zero/routers/todos.py index a1054afa..ac108813 100644 --- a/codigo_das_aulas/09/fast_zero/routers/todos.py +++ b/codigo_das_aulas/09/fast_zero/routers/todos.py @@ -8,6 +8,7 @@ from fast_zero.database import get_session from fast_zero.models import Todo, User from fast_zero.schemas import ( + FilterTodo, Message, TodoList, TodoPublic, @@ -44,27 +45,27 @@ def create_todo( @router.get('/', response_model=TodoList) -def list_todos( # noqa +def list_todos( session: Session, user: CurrentUser, - title: str = Query(None), - description: str = Query(None), - state: str = Query(None), - offset: int = Query(None), - limit: int = Query(None), + todo_filter: Annotated[FilterTodo, Query()], ): query = select(Todo).where(Todo.user_id == user.id) - if title: - query = query.filter(Todo.title.contains(title)) + if todo_filter.title: + query = query.filter(Todo.title.contains(todo_filter.title)) - if description: - query = query.filter(Todo.description.contains(description)) + if todo_filter.description: + query = query.filter( + Todo.description.contains(todo_filter.description) + ) - if state: - query = query.filter(Todo.state == state) + if todo_filter.state: + query = query.filter(Todo.state == todo_filter.state) - todos = session.scalars(query.offset(offset).limit(limit)).all() + todos = session.scalars( + query.offset(todo_filter.offset).limit(todo_filter.limit) + ).all() return {'todos': todos} diff --git a/codigo_das_aulas/09/fast_zero/routers/users.py b/codigo_das_aulas/09/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/09/fast_zero/routers/users.py +++ b/codigo_das_aulas/09/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/09/fast_zero/schemas.py b/codigo_das_aulas/09/fast_zero/schemas.py index 0c8030c3..284c6549 100644 --- a/codigo_das_aulas/09/fast_zero/schemas.py +++ b/codigo_das_aulas/09/fast_zero/schemas.py @@ -51,3 +51,14 @@ class TodoUpdate(BaseModel): title: str | None = None description: str | None = None state: TodoState | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 + + +class FilterTodo(FilterPage): + title: str | None = None + description: str | None = None + state: TodoState | None = None diff --git a/codigo_das_aulas/10/fast_zero/routers/todos.py b/codigo_das_aulas/10/fast_zero/routers/todos.py index a1054afa..ac108813 100644 --- a/codigo_das_aulas/10/fast_zero/routers/todos.py +++ b/codigo_das_aulas/10/fast_zero/routers/todos.py @@ -8,6 +8,7 @@ from fast_zero.database import get_session from fast_zero.models import Todo, User from fast_zero.schemas import ( + FilterTodo, Message, TodoList, TodoPublic, @@ -44,27 +45,27 @@ def create_todo( @router.get('/', response_model=TodoList) -def list_todos( # noqa +def list_todos( session: Session, user: CurrentUser, - title: str = Query(None), - description: str = Query(None), - state: str = Query(None), - offset: int = Query(None), - limit: int = Query(None), + todo_filter: Annotated[FilterTodo, Query()], ): query = select(Todo).where(Todo.user_id == user.id) - if title: - query = query.filter(Todo.title.contains(title)) + if todo_filter.title: + query = query.filter(Todo.title.contains(todo_filter.title)) - if description: - query = query.filter(Todo.description.contains(description)) + if todo_filter.description: + query = query.filter( + Todo.description.contains(todo_filter.description) + ) - if state: - query = query.filter(Todo.state == state) + if todo_filter.state: + query = query.filter(Todo.state == todo_filter.state) - todos = session.scalars(query.offset(offset).limit(limit)).all() + todos = session.scalars( + query.offset(todo_filter.offset).limit(todo_filter.limit) + ).all() return {'todos': todos} diff --git a/codigo_das_aulas/10/fast_zero/routers/users.py b/codigo_das_aulas/10/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/10/fast_zero/routers/users.py +++ b/codigo_das_aulas/10/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/10/fast_zero/schemas.py b/codigo_das_aulas/10/fast_zero/schemas.py index 0c8030c3..284c6549 100644 --- a/codigo_das_aulas/10/fast_zero/schemas.py +++ b/codigo_das_aulas/10/fast_zero/schemas.py @@ -51,3 +51,14 @@ class TodoUpdate(BaseModel): title: str | None = None description: str | None = None state: TodoState | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 + + +class FilterTodo(FilterPage): + title: str | None = None + description: str | None = None + state: TodoState | None = None diff --git a/codigo_das_aulas/11/fast_zero/routers/todos.py b/codigo_das_aulas/11/fast_zero/routers/todos.py index a1054afa..ac108813 100644 --- a/codigo_das_aulas/11/fast_zero/routers/todos.py +++ b/codigo_das_aulas/11/fast_zero/routers/todos.py @@ -8,6 +8,7 @@ from fast_zero.database import get_session from fast_zero.models import Todo, User from fast_zero.schemas import ( + FilterTodo, Message, TodoList, TodoPublic, @@ -44,27 +45,27 @@ def create_todo( @router.get('/', response_model=TodoList) -def list_todos( # noqa +def list_todos( session: Session, user: CurrentUser, - title: str = Query(None), - description: str = Query(None), - state: str = Query(None), - offset: int = Query(None), - limit: int = Query(None), + todo_filter: Annotated[FilterTodo, Query()], ): query = select(Todo).where(Todo.user_id == user.id) - if title: - query = query.filter(Todo.title.contains(title)) + if todo_filter.title: + query = query.filter(Todo.title.contains(todo_filter.title)) - if description: - query = query.filter(Todo.description.contains(description)) + if todo_filter.description: + query = query.filter( + Todo.description.contains(todo_filter.description) + ) - if state: - query = query.filter(Todo.state == state) + if todo_filter.state: + query = query.filter(Todo.state == todo_filter.state) - todos = session.scalars(query.offset(offset).limit(limit)).all() + todos = session.scalars( + query.offset(todo_filter.offset).limit(todo_filter.limit) + ).all() return {'todos': todos} diff --git a/codigo_das_aulas/11/fast_zero/routers/users.py b/codigo_das_aulas/11/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/11/fast_zero/routers/users.py +++ b/codigo_das_aulas/11/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/11/fast_zero/schemas.py b/codigo_das_aulas/11/fast_zero/schemas.py index 0c8030c3..284c6549 100644 --- a/codigo_das_aulas/11/fast_zero/schemas.py +++ b/codigo_das_aulas/11/fast_zero/schemas.py @@ -51,3 +51,14 @@ class TodoUpdate(BaseModel): title: str | None = None description: str | None = None state: TodoState | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 + + +class FilterTodo(FilterPage): + title: str | None = None + description: str | None = None + state: TodoState | None = None diff --git a/codigo_das_aulas/11/tests/conftest.py b/codigo_das_aulas/11/tests/conftest.py index 794abf67..dc48c4a6 100644 --- a/codigo_das_aulas/11/tests/conftest.py +++ b/codigo_das_aulas/11/tests/conftest.py @@ -14,7 +14,6 @@ @pytest.fixture(scope='session') def engine(): with PostgresContainer('postgres:16', driver='psycopg') as postgres: - _engine = create_engine(postgres.get_connection_url()) with _engine.begin(): diff --git a/codigo_das_aulas/12/fast_zero/routers/todos.py b/codigo_das_aulas/12/fast_zero/routers/todos.py index a1054afa..ac108813 100644 --- a/codigo_das_aulas/12/fast_zero/routers/todos.py +++ b/codigo_das_aulas/12/fast_zero/routers/todos.py @@ -8,6 +8,7 @@ from fast_zero.database import get_session from fast_zero.models import Todo, User from fast_zero.schemas import ( + FilterTodo, Message, TodoList, TodoPublic, @@ -44,27 +45,27 @@ def create_todo( @router.get('/', response_model=TodoList) -def list_todos( # noqa +def list_todos( session: Session, user: CurrentUser, - title: str = Query(None), - description: str = Query(None), - state: str = Query(None), - offset: int = Query(None), - limit: int = Query(None), + todo_filter: Annotated[FilterTodo, Query()], ): query = select(Todo).where(Todo.user_id == user.id) - if title: - query = query.filter(Todo.title.contains(title)) + if todo_filter.title: + query = query.filter(Todo.title.contains(todo_filter.title)) - if description: - query = query.filter(Todo.description.contains(description)) + if todo_filter.description: + query = query.filter( + Todo.description.contains(todo_filter.description) + ) - if state: - query = query.filter(Todo.state == state) + if todo_filter.state: + query = query.filter(Todo.state == todo_filter.state) - todos = session.scalars(query.offset(offset).limit(limit)).all() + todos = session.scalars( + query.offset(todo_filter.offset).limit(todo_filter.limit) + ).all() return {'todos': todos} diff --git a/codigo_das_aulas/12/fast_zero/routers/users.py b/codigo_das_aulas/12/fast_zero/routers/users.py index d2cebe6d..8b82a5c3 100644 --- a/codigo_das_aulas/12/fast_zero/routers/users.py +++ b/codigo_das_aulas/12/fast_zero/routers/users.py @@ -1,13 +1,19 @@ from http import HTTPStatus from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select from sqlalchemy.orm import Session from fast_zero.database import get_session from fast_zero.models import User -from fast_zero.schemas import Message, UserList, UserPublic, UserSchema +from fast_zero.schemas import ( + FilterPage, + Message, + UserList, + UserPublic, + UserSchema, +) from fast_zero.security import ( get_current_user, get_password_hash, @@ -54,8 +60,11 @@ def create_user(user: UserSchema, session: Session): @router.get('/', response_model=UserList) -def read_users(session: Session, skip: int = 0, limit: int = 100): - users = session.scalars(select(User).offset(skip).limit(limit)).all() +def read_users(session: Session, filter_users: Annotated[FilterPage, Query()]): + users = session.scalars( + select(User).offset(filter_users.offset).limit(filter_users.limit) + ).all() + return {'users': users} diff --git a/codigo_das_aulas/12/fast_zero/schemas.py b/codigo_das_aulas/12/fast_zero/schemas.py index 0c8030c3..284c6549 100644 --- a/codigo_das_aulas/12/fast_zero/schemas.py +++ b/codigo_das_aulas/12/fast_zero/schemas.py @@ -51,3 +51,14 @@ class TodoUpdate(BaseModel): title: str | None = None description: str | None = None state: TodoState | None = None + + +class FilterPage(BaseModel): + offset: int = 0 + limit: int = 100 + + +class FilterTodo(FilterPage): + title: str | None = None + description: str | None = None + state: TodoState | None = None