From cfe4d429c9dd75651a9dd2a731c7d64a6f4e303b Mon Sep 17 00:00:00 2001 From: Dylan Starink Date: Fri, 17 Nov 2023 13:23:13 -0800 Subject: [PATCH] AD Campaigns + Song History --- schema.sql | 19 +++++++++++++++++++ src/api/ad.py | 29 +++++++++++++++++++++++++++++ src/api/server.py | 3 ++- src/api/songs.py | 8 ++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/api/ad.py diff --git a/schema.sql b/schema.sql index 41cdec5..1b1c353 100644 --- a/schema.sql +++ b/schema.sql @@ -57,6 +57,25 @@ create table ) tablespace pg_default; +create table + public.ad_campaigns ( + id bigint generated by default as identity, + created_at timestamp with time zone not null default now(), + link text not null, + target_mood text not null, + constraint ad_campaign_pkey primary key (id) + ) tablespace pg_default; + +create table + public.song_history ( + id bigint generated by default as identity, + created_at timestamp with time zone not null default now(), + user_id bigint not null, + song_id bigint not null, + constraint song_history_pkey primary key (id), + constraint song_history_song_id_fkey foreign key (song_id) references songs (id) on update cascade on delete cascade, + constraint song_history_user_id_fkey foreign key (user_id) references users (id) on update cascade on delete cascade + ) tablespace pg_default; INSERT INTO songs (song_name, artist, album) VALUES ('Mr. Brightside', 'The Killers', 'Hot Fuss'); diff --git a/src/api/ad.py b/src/api/ad.py new file mode 100644 index 0000000..6f38460 --- /dev/null +++ b/src/api/ad.py @@ -0,0 +1,29 @@ +from fastapi import APIRouter +from enum import Enum +import sqlalchemy +import src.database as db + +router = APIRouter( + prefix="/ad", + tags=["ad"], +) + +class MoodEnum(str, Enum): + sad = 'SAD' + happy = 'HAPPY' + angry = 'ANGRY' + + +@router.post("/create") +def create_playlist(link: str, mood: MoodEnum): + """ """ + with db.engine.begin() as connection: + connection.execute(sqlalchemy.text(""" + INSERT INTO ad_campaigns (link, target_mood) VALUES (:link, :mood) + """), + [{ + "link": link, + "mood": mood + }]) + + return "Success" \ No newline at end of file diff --git a/src/api/server.py b/src/api/server.py index 807a070..597661b 100644 --- a/src/api/server.py +++ b/src/api/server.py @@ -1,7 +1,7 @@ from fastapi import FastAPI, exceptions from fastapi.responses import JSONResponse from pydantic import ValidationError -from src.api import users, songs, playlists +from src.api import users, songs, playlists, ad import json import logging @@ -23,6 +23,7 @@ app.include_router(users.router) app.include_router(songs.router) app.include_router(playlists.router) +app.include_router(ad.router) @app.exception_handler(exceptions.RequestValidationError) @app.exception_handler(ValidationError) diff --git a/src/api/songs.py b/src/api/songs.py index ce66c1a..7116e62 100644 --- a/src/api/songs.py +++ b/src/api/songs.py @@ -168,6 +168,14 @@ def play_song(song_id: int, user_id: str = Header(None)) -> SongPlayLink: return "Song not available on user's platform" + conn.execute(sqlalchemy.text(""" + INSERT INTO song_history (user_id, song_id) VALUES (:user_id, :song_id) + """), + [{ + "song_id": song_id, + "user_id": user_id + }]) + return query.song_url