Refactor models and endpoints, add logging, and improve error handling.

This commit is contained in:
Esa Kataja
2024-12-01 22:29:00 +02:00
parent cecd9a4458
commit b5e693ab9f
8 changed files with 174 additions and 77 deletions
+12 -15
View File
@@ -1,25 +1,22 @@
from models.movie import Movie
from lib.database.database import get_db
from lib.logger import logger
@logger.catch
def get_all_movies() -> list[Movie]:
logger.info("Getting all movies")
sql = "SELECT * FROM movies"
with get_db() as conn:
result = conn.execute(sql).fetchall()
result = conn.execute(sql).df()
result_dict = result.to_dict(orient="records")
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],
)
for row in result_dict:
row["id"] = str(row["id"])
logger.debug(f"Found movie: {row}")
print("Type of name:", type(row["name"]))
movie = Movie(**row)
movies.append(movie)
return movies
@@ -28,7 +25,7 @@ def get_all_movies() -> list[Movie]:
def get_next_movie() -> Movie | None:
sql = "SELECT * FROM movies WHERE is_watched = FALSE AND showtime > CURRENT_TIMESTAMP ORDER BY showtime ASC"
sql = "SELECT * FROM movies WHERE is_watched = FALSE AND showtime = CURRENT_DATE ORDER BY showtime ASC"
with get_db() as conn:
result = conn.execute(sql).fetchdf()
if result.empty: