Skip to content

Commit

Permalink
[WIP] atualização do código apra fastapi 0.115
Browse files Browse the repository at this point in the history
  • Loading branch information
dunossauro committed Sep 26, 2024
1 parent 9e9d89e commit 2163bc0
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 77 deletions.
17 changes: 13 additions & 4 deletions codigo_das_aulas/07/fast_zero/routers/users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}


Expand Down
5 changes: 5 additions & 0 deletions codigo_das_aulas/07/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ class Token(BaseModel):

class TokenData(BaseModel):
username: str | None = None


class FilterPage(BaseModel):
offset: int = 0
limit: int = 100
17 changes: 13 additions & 4 deletions codigo_das_aulas/08/fast_zero/routers/users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}


Expand Down
5 changes: 5 additions & 0 deletions codigo_das_aulas/08/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ class Token(BaseModel):

class TokenData(BaseModel):
username: str | None = None


class FilterPage(BaseModel):
offset: int = 0
limit: int = 100
27 changes: 14 additions & 13 deletions codigo_das_aulas/09/fast_zero/routers/todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}

Expand Down
17 changes: 13 additions & 4 deletions codigo_das_aulas/09/fast_zero/routers/users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}


Expand Down
11 changes: 11 additions & 0 deletions codigo_das_aulas/09/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 14 additions & 13 deletions codigo_das_aulas/10/fast_zero/routers/todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}

Expand Down
17 changes: 13 additions & 4 deletions codigo_das_aulas/10/fast_zero/routers/users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}


Expand Down
11 changes: 11 additions & 0 deletions codigo_das_aulas/10/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 14 additions & 13 deletions codigo_das_aulas/11/fast_zero/routers/todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}

Expand Down
17 changes: 13 additions & 4 deletions codigo_das_aulas/11/fast_zero/routers/users.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}


Expand Down
11 changes: 11 additions & 0 deletions codigo_das_aulas/11/fast_zero/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion codigo_das_aulas/11/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading

0 comments on commit 2163bc0

Please sign in to comment.