Skip to content

Commit

Permalink
AD Campaigns + Song History
Browse files Browse the repository at this point in the history
  • Loading branch information
StarDylan committed Nov 17, 2023
1 parent c9e2fef commit cfe4d42
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
19 changes: 19 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
29 changes: 29 additions & 0 deletions src/api/ad.py
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion src/api/server.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/api/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit cfe4d42

Please sign in to comment.