Skip to content
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
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

FROM python:3.9-slim-buster

WORKDIR /app


COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt


EXPOSE 8000

CMD ["uvicorn", "filmes:app", "--host", "0.0.0.0", "--port", "8000"]
27 changes: 27 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# API de Filmes - Backend

Este projeto contém uma API simples para gerenciar filmes.

## Como Rodar a API (com Docker)

1. **Pré-requisitos:** Tenha o [Docker Desktop](https://www.docker.com/products/docker-desktop/) instalado e rodando.
2. **No Terminal:** Abra o terminal na pasta raiz do projeto (`backend`).
3. **Construa a imagem:**
```bash
docker compose build
```
4. **Inicie a API:**
```bash
docker compose up
```
(Para rodar em segundo plano, use `docker compose up -d`)

## Acessando a API

Após iniciar a API, abra seu navegador e vá para [http://localhost:8000/docs](http://localhost:8000/docs) para ver e testar a documentação.

## Endpoints da API

* **`GET /filmes`**: Lista todos os filmes.
* **`GET /filmes/{id}`**: Pega detalhes de um filme pelo ID.
* **`POST /filmes`**: Adiciona um novo filme. (Precisa de um JSON no corpo da requisição com `titulo`, `ano`, `genero`).
Binary file added __pycache__/filmes.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/filmes.cpython-39.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.8'

services:
backend-api:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
38 changes: 38 additions & 0 deletions filmes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"id": 1,
"titulo": "Berserk: A Era de Ouro - Ato 1",
"ano": 2012,
"genero": "fantasia"
},
{
"id": 2,
"titulo": "Midsommar - O Mal Não Espera a Noite",
"ano": 2019,
"genero": "terror e suspense"
},
{
"id": 3,
"titulo": "A Bruxa de Blair",
"ano": 1999,
"genero": "terror"
},
{
"id": 4,
"titulo": "Tusk",
"ano": 2014,
"genero": "terror/comédia"
},
{
"id": 5,
"titulo": "Eu, Christiane F. - 13 Anos, Drogada e Prostituída",
"ano": 1981,
"genero": "Drama/independente"
},
{
"id": 6,
"titulo": "A Bruxa",
"ano": 2015,
"genero": "terror/misterio"
}
]
45 changes: 45 additions & 0 deletions filmes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import json, os

app = FastAPI()
FILMES = "filmes.json"

class Filme(BaseModel):
titulo: str
ano: int
genero: str

def carregar_filmes():
if os.path.exists(FILMES):
with open(FILMES, "r", encoding="utf-8") as f:
try:
return json.load(f)
except:
return []
return []

def salvar_filmes(filmes):
with open(FILMES, "w", encoding="utf-8") as f:
json.dump(filmes, f, indent=4, ensure_ascii=False)

@app.get("/filmes")
def listar_filmes():
return carregar_filmes()

@app.post("/filmes")
def adicionar_filme(filme: Filme):
filmes = carregar_filmes()
novo_id = max([f.get("id", 0) for f in filmes], default=0) + 1
novo_filme = {"id": novo_id, **filme.dict()}
filmes.append(novo_filme)
salvar_filmes(filmes)
return novo_filme

@app.get("/filmes/{id}")
def obter_filme(id: int):
filmes = carregar_filmes()
for filme in filmes:
if filme["id"] == id:
return filme
raise HTTPException(status_code=404, detail="Filme não encontrado")
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi==0.111.0
uvicorn==0.30.1
pydantic==2.7.4