Add Movie endpoint
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
from models.movie import Movie
|
||||||
|
from lib.database.database import get_db
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_movies() -> list[Movie]:
|
||||||
|
sql = "SELECT * FROM movies"
|
||||||
|
with get_db() as conn:
|
||||||
|
result = conn.execute(sql).fetchall()
|
||||||
|
movies = []
|
||||||
|
for row in result:
|
||||||
|
movie = Movie(
|
||||||
|
id=str(row[0]),
|
||||||
|
name=row[1],
|
||||||
|
imdb_id=row[2],
|
||||||
|
actors=row[3],
|
||||||
|
release_date=row[4],
|
||||||
|
plot=row[5],
|
||||||
|
showtime=row[6],
|
||||||
|
is_watched=row[7],
|
||||||
|
created_at=row[8],
|
||||||
|
updated_at=row[9],
|
||||||
|
)
|
||||||
|
movies.append(movie)
|
||||||
|
|
||||||
|
return movies
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def get_next_movie() -> Movie:
|
||||||
|
sql = "SELECT * FROM movies WHERE is_watched = FALSE AND showtime > CURRENT_TIMESTAMP ORDER BY showtime ASC"
|
||||||
|
with get_db() as conn:
|
||||||
|
result = conn.execute(sql).fetchone()
|
||||||
|
if result is None:
|
||||||
|
return None
|
||||||
|
movie = Movie(
|
||||||
|
id=str(result[0]),
|
||||||
|
name=result[1],
|
||||||
|
imdb_id=result[2],
|
||||||
|
actors=result[3],
|
||||||
|
release_date=result[4],
|
||||||
|
plot=result[5],
|
||||||
|
showtime=result[6],
|
||||||
|
is_watched=result[7],
|
||||||
|
created_at=result[8],
|
||||||
|
updated_at=result[9],
|
||||||
|
)
|
||||||
|
|
||||||
|
return movie
|
||||||
|
|
||||||
|
return
|
||||||
+35
-10
@@ -4,13 +4,38 @@ from typing import Optional
|
|||||||
|
|
||||||
|
|
||||||
class Movie(BaseModel):
|
class Movie(BaseModel):
|
||||||
id: Optional[str]
|
id: Optional[str] = Field(
|
||||||
name: str
|
default=None, examples=["123e4567-e89b-12d3-a456-426655440000"]
|
||||||
imdb_id: str
|
)
|
||||||
actors: list[str]
|
name: str = Field(
|
||||||
plot: str
|
description="Name of the movie", examples=["A Maple Valley Christmas"]
|
||||||
release_date: datetime
|
)
|
||||||
showtime: datetime
|
imdb_id: str = Field(description="IMDB ID of the movie", examples=["tt1234567"])
|
||||||
is_watched: bool = Field(default=False)
|
actors: list[str] = Field(
|
||||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
description="Actors of the movie",
|
||||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
examples=[["Peyton List", "Andrew W. Walker"]],
|
||||||
|
)
|
||||||
|
plot: str = Field(
|
||||||
|
description="Plot of the movie", examples=["Erica is a rancher..."]
|
||||||
|
)
|
||||||
|
release_date: datetime = Field(
|
||||||
|
description="Release date of the movie", examples=["2022-11-05"]
|
||||||
|
)
|
||||||
|
showtime: datetime = Field(
|
||||||
|
description="Showtime of the movie", examples=["2024-12-01"]
|
||||||
|
)
|
||||||
|
is_watched: bool = Field(
|
||||||
|
default=False,
|
||||||
|
description="Whether the movie is watched or not",
|
||||||
|
examples=[True],
|
||||||
|
)
|
||||||
|
created_at: datetime = Field(
|
||||||
|
default_factory=datetime.utcnow,
|
||||||
|
description="Creation date of the movie",
|
||||||
|
examples=[datetime.utcnow()],
|
||||||
|
)
|
||||||
|
updated_at: datetime = Field(
|
||||||
|
default_factory=datetime.utcnow,
|
||||||
|
description="Modification date of the movie",
|
||||||
|
examples=[datetime.utcnow()],
|
||||||
|
)
|
||||||
|
|||||||
+22
-3
@@ -1,4 +1,8 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Response
|
||||||
|
|
||||||
|
from lib.database.movie import get_all_movies, get_next_movie
|
||||||
|
from models.movie import Movie
|
||||||
|
from models import Message
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
prefix="/movie",
|
prefix="/movie",
|
||||||
@@ -7,6 +11,21 @@ router = APIRouter(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/", response_model=list[Movie])
|
||||||
async def get_movies():
|
async def get_movies():
|
||||||
return {"movies": ["movie1", "movie2", "movie3"]}
|
return get_all_movies()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/next", response_model=Movie)
|
||||||
|
async def get_next_movie_endpoint():
|
||||||
|
"""Get the next movie to watch"""
|
||||||
|
next_movie = get_next_movie()
|
||||||
|
if next_movie is None:
|
||||||
|
return Response(
|
||||||
|
status_code=200,
|
||||||
|
media_type="application/json",
|
||||||
|
content=Message(
|
||||||
|
message="No more movies to watch", message_type="info"
|
||||||
|
).model_dump_json(),
|
||||||
|
)
|
||||||
|
return get_next_movie()
|
||||||
|
|||||||
Reference in New Issue
Block a user