Compare commits
4
Commits
21e30b0fb2
...
9ecdc1b040
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ecdc1b040 | ||
|
|
4a480a312e | ||
|
|
f00bff5f64 | ||
|
|
25f528c86d |
@@ -16,4 +16,6 @@ wheels/
|
||||
*.log
|
||||
*.gz
|
||||
|
||||
data
|
||||
|
||||
.vscode/*
|
||||
@@ -12,4 +12,5 @@ FROM base
|
||||
COPY --from=builder /app /app
|
||||
ENV PATH="/app/.venv/bin:$PATH"
|
||||
WORKDIR /app
|
||||
RUN mkdir -p /app/data
|
||||
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
|
||||
@@ -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 Team;
|
||||
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 user_id_seq;
|
||||
@@ -42,6 +44,8 @@ CREATE TABLE "Team" (
|
||||
CREATE TABLE "User" (
|
||||
id INTEGER PRIMARY KEY DEFAULT nextval('user_id_seq'), -- Use sequence for auto-increment
|
||||
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
|
||||
email VARCHAR UNIQUE, -- User's email address (nullable)
|
||||
team_id INTEGER NOT NULL, -- Foreign Key -> Group.id
|
||||
@@ -115,6 +119,31 @@ CREATE TABLE CountryCodes (
|
||||
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 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');
|
||||
|
||||
@@ -79,8 +79,12 @@ def seed_db() -> None:
|
||||
|
||||
|
||||
def init_db():
|
||||
logger.info("Initializing database...")
|
||||
if Path(DB_PATH).exists():
|
||||
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:
|
||||
sql = f.read()
|
||||
with get_connection() as conn:
|
||||
|
||||
@@ -93,6 +93,13 @@ async def update_review(review: ReviewIn):
|
||||
f"AND user_id = {review.user_id}"
|
||||
)
|
||||
|
||||
# Check if the review exists
|
||||
review_df = conn.execute(
|
||||
"SELECT * FROM Review WHERE song_id = ? AND user_id = ?",
|
||||
(review.song_id, review.user_id),
|
||||
).fetchdf()
|
||||
|
||||
if not review_df.empty:
|
||||
# Update the review
|
||||
conn.execute(
|
||||
"UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
|
||||
@@ -105,6 +112,19 @@ async def update_review(review: ReviewIn):
|
||||
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
|
||||
logger.debug(
|
||||
|
||||
Reference in New Issue
Block a user