Compare commits
4
Commits
21e30b0fb2
...
9ecdc1b040
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ecdc1b040 | ||
|
|
4a480a312e | ||
|
|
f00bff5f64 | ||
|
|
25f528c86d |
@@ -16,4 +16,6 @@ wheels/
|
|||||||
*.log
|
*.log
|
||||||
*.gz
|
*.gz
|
||||||
|
|
||||||
|
data
|
||||||
|
|
||||||
.vscode/*
|
.vscode/*
|
||||||
@@ -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"]
|
||||||
|
|||||||
@@ -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');
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -93,6 +93,13 @@ async def update_review(review: ReviewIn):
|
|||||||
f"AND user_id = {review.user_id}"
|
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
|
# Update the review
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"UPDATE Review SET score_song = ?, score_show = ?, score_costume = ?, text_review = ? WHERE song_id = ? AND user_id = ?",
|
"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,
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user