Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/emanoel #58

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
.git
.gitignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.11

WORKDIR /app

COPY ./requirements.txt .

RUN pip install -r requirements.txt

COPY . /app

CMD [ "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000" ]
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Descrição de como executar o código:

### 1. Para executar diretamente o arquivo main.py com o Python é necessário instalar os pacotes listados em requirements.txt
- pip install -r requirements.txt

### 2. Para executar pelo Docker, foi utilizado o Python na versão 3.11, e configurado o docker-compose.yml
- Obs. está utilizando a porta 8000
- docker-compose up

### 3. Após executar basta abrir o navegador em localhost:8000/docs para ter acesso aos get e post para listar e cadastrar filmes


![WATTIO](http://wattio.com.br/web/image/1204-212f47c3/Logo%20Wattio.png)

#### Descrição
Expand Down
Binary file added app/db/__pycache__/database.cpython-311.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions app/db/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Configurando a conexão com o banco de dados SQLite
SQLALCHEMY_DATABASE_URL = "sqlite:///./filmes.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
Binary file not shown.
18 changes: 18 additions & 0 deletions app/model/filmes_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pydantic import BaseModel
from sqlalchemy import Column, Integer, String
from app.db.database import *


# Definindo o modelo de dados do filme
class Filme(Base):
__tablename__ = "filmes"

id = Column(Integer, primary_key=True, index=True)
titulo = Column(String)
lancamento = Column(Integer)


# Criando classe de modelo para receber dados de entrada no POST
class FilmeCreate(BaseModel):
titulo: str
lancamento: int
Binary file not shown.
34 changes: 34 additions & 0 deletions app/routers/filmes_routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from fastapi import APIRouter, HTTPException
from app.db.database import *
from app.model.filmes_model import *

# Configurando a rota filmes
router = APIRouter(prefix="/filmes")

# Rota para listar todos os filmes
@router.get("")
def listar_filmes():
db = SessionLocal()
filmes = db.query(Filme).all()
return filmes


# Rota para listar filme por id
@router.get("/{filme_id}")
def listar_filme_por_id(filme_id: int):
db = SessionLocal()
filme = db.query(Filme).filter(Filme.id == filme_id).first()
if filme is None:
raise HTTPException(status_code=404, detail="Filme não encontrado!")
return filme


# Rota para cadastrar um novo filme
@router.post("")
def cadastrar_filme(filme: FilmeCreate):
db = SessionLocal()
db_filme = Filme(**filme.model_dump())
db.add(db_filme)
db.commit()
db.refresh(db_filme)
return db_filme
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3.8'

services:
fastapi-app:
build: .
ports:
- "8000:8000"
Binary file added filmes.db
Binary file not shown.
21 changes: 21 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import FastAPI
import uvicorn
from app.routers import filmes_routers
from app.db.database import *


# Criando as tabelas no banco de dados, se não existirem
Base.metadata.create_all(bind=engine)

app = FastAPI()

@app.get("/")
def pagina_inicial():
return {"Olá FastAPI": "Olá FastAPI"}


# Configurando as rotas com o modulo filmes_routers
app.include_router(filmes_routers.router)

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Binary file added requirements.txt
Binary file not shown.