4 Commits
5 changed files with 68 additions and 12 deletions
+2
View File
@@ -16,4 +16,6 @@ wheels/
*.log *.log
*.gz *.gz
data
.vscode/* .vscode/*
+1
View File
@@ -12,4 +12,5 @@ FROM base
COPY --from=builder /app /app COPY --from=builder /app /app
ENV PATH="/app/.venv/bin:$PATH" ENV PATH="/app/.venv/bin:$PATH"
WORKDIR /app WORKDIR /app
RUN mkdir -p /app/data
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"] CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
+29
View File
@@ -10,6 +10,8 @@ DROP TABLE IF EXISTS Song;
DROP TABLE IF EXISTS "User"; -- Quoted because USER is a reserved keyword DROP TABLE IF EXISTS "User"; -- Quoted because USER is a reserved keyword
DROP TABLE IF EXISTS Team; DROP TABLE IF EXISTS Team;
DROP TABLE IF EXISTS CountryCodes; DROP TABLE IF EXISTS CountryCodes;
DROP VIEW IF EXISTS ReviewSummary;
DROP VIEW IF EXISTS ReviewSummaryByTeam;
DROP SEQUENCE IF EXISTS group_id_seq; DROP SEQUENCE IF EXISTS group_id_seq;
DROP SEQUENCE IF EXISTS user_id_seq; DROP SEQUENCE IF EXISTS user_id_seq;
@@ -42,6 +44,8 @@ CREATE TABLE "Team" (
CREATE TABLE "User" ( CREATE TABLE "User" (
id INTEGER PRIMARY KEY DEFAULT nextval('user_id_seq'), -- Use sequence for auto-increment id INTEGER PRIMARY KEY DEFAULT nextval('user_id_seq'), -- Use sequence for auto-increment
username VARCHAR UNIQUE NOT NULL, -- The user's login name username VARCHAR UNIQUE NOT NULL, -- The user's login name
first_name VARCHAR DEFAULT '',
last_name VARCHAR DEFAULT '',
hashed_password VARCHAR NOT NULL, -- The securely hashed password hashed_password VARCHAR NOT NULL, -- The securely hashed password
email VARCHAR UNIQUE, -- User's email address (nullable) email VARCHAR UNIQUE, -- User's email address (nullable)
team_id INTEGER NOT NULL, -- Foreign Key -> Group.id team_id INTEGER NOT NULL, -- Foreign Key -> Group.id
@@ -115,6 +119,31 @@ CREATE TABLE CountryCodes (
name_sv VARCHAR NOT NULL name_sv VARCHAR NOT NULL
); );
CREATE VIEW ReviewSummaryGlobal AS
SELECT
song_id,
COUNT(*) AS total_reviews,
AVG(score_song) AS avg_score_song,
AVG(score_show) AS avg_score_show,
AVG(score_costume) AS avg_score_costume,
AVG((score_song + score_show + score_costume)/3) AS avg_total_score
FROM Review
GROUP BY song_id;
CREATE VIEW ReviewSummaryByTeam AS
SELECT
r.song_id,
u.team_id,
AVG(r.score_song) AS avg_score_song,
AVG(r.score_show) AS avg_score_show,
AVG(r.score_costume) AS avg_score_costume,
AVG((r.score_song + r.score_show + r.score_costume)/3) AS avg_total_score
FROM Review r
JOIN "User" u ON r.user_id = u.id
JOIN Song s ON r.song_id = s.id
GROUP BY r.song_id, u.team_id
ORDER BY r.song_id, u.team_id;
-- Insert country codes data -- Insert country codes data
INSERT INTO CountryCodes (code, name_en, name_fi, name_sv) VALUES ('ALB', 'Albania', 'Albania', 'Albanien'); INSERT INTO CountryCodes (code, name_en, name_fi, name_sv) VALUES ('ALB', 'Albania', 'Albania', 'Albanien');
INSERT INTO CountryCodes (code, name_en, name_fi, name_sv) VALUES ('ARM', 'Armenia', 'Armenia', 'Armenien'); INSERT INTO CountryCodes (code, name_en, name_fi, name_sv) VALUES ('ARM', 'Armenia', 'Armenia', 'Armenien');
+4
View File
@@ -79,8 +79,12 @@ def seed_db() -> None:
def init_db(): def init_db():
logger.info("Initializing database...")
if Path(DB_PATH).exists(): if Path(DB_PATH).exists():
return return
logger.info("Database does not exist, creating...")
if not Path(DB_PATH).parent.exists():
Path(DB_PATH).parent.mkdir(parents=True, exist_ok=True)
with open(SQL_PATH, "r") as f: with open(SQL_PATH, "r") as f:
sql = f.read() sql = f.read()
with get_connection() as conn: with get_connection() as conn:
+32 -12
View File
@@ -93,18 +93,38 @@ async def update_review(review: ReviewIn):
f"AND user_id = {review.user_id}" f"AND user_id = {review.user_id}"
) )
# Update the review # Check if the review exists
conn.execute( review_df = conn.execute(
"UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?", "SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
( (review.song_id, review.user_id),
review.score_song, ).fetchdf()
review.score_show,
review.score_costume, if not review_df.empty:
review.text_review, # Update the review
review.song_id, conn.execute(
review.user_id, "UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
), (
) review.score_song,
review.score_show,
review.score_costume,
review.text_review,
review.song_id,
review.user_id,
),
)
else:
# Insert the review
conn.execute(
"INSERT INTO Review (user_id, song_id, score_song, score_show, score_costume, text_review) VALUES (?, ?, ?, ?, ?, ?)",
(
review.user_id,
review.song_id,
review.score_song,
review.score_show,
review.score_costume,
review.text_review,
),
)
# Fetch the updated review # Fetch the updated review
logger.debug( logger.debug(