|
|
from fastapi import FastAPI, Request, Query
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
from fastapi.responses import FileResponse
|
|
|
from pathlib import Path
|
|
|
from .models import Movie
|
|
|
from .data import movie_data
|
|
|
|
|
|
app = FastAPI(title="Binge")
|
|
|
|
|
|
|
|
|
static_dir = Path("static")
|
|
|
static_dir.mkdir(exist_ok=True)
|
|
|
(static_dir / "css").mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
|
|
|
|
|
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
@app.get("/")
|
|
|
async def home(request: Request, search: str | None = Query(None)):
|
|
|
if search:
|
|
|
filtered_movies = [
|
|
|
movie for movie in movie_data
|
|
|
if search.lower() in movie.title.lower() or
|
|
|
(movie.description and search.lower() in movie.description.lower())
|
|
|
]
|
|
|
else:
|
|
|
filtered_movies = movie_data
|
|
|
|
|
|
return templates.TemplateResponse(
|
|
|
"index.html",
|
|
|
{"request": request, "movies": filtered_movies, "search": search}
|
|
|
)
|
|
|
|
|
|
@app.get("/movie/{movie_id}")
|
|
|
async def movie_detail(request: Request, movie_id: int):
|
|
|
if 0 <= movie_id < len(movie_data):
|
|
|
return templates.TemplateResponse(
|
|
|
"movie.html",
|
|
|
{"request": request, "movie": movie_data[movie_id]}
|
|
|
)
|
|
|
return {"error": "Movie not found"} |