From 073149464269b5fcc90e282536c1a946ad474085 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Sun, 19 Jan 2025 14:17:25 +0100 Subject: add docs + song api routes --- Cargo.toml | 1 + N | 6 ----- README.md | 29 ++++++++++++++++++++++ docs/entry1.pdf | Bin 0 -> 82039 bytes docs/entry2.pdf | Bin 0 -> 59738 bytes docs/entry3.txt | 36 +++++++++++++++++++++++++++ docs/entry4.txt | 25 +++++++++++++++++++ src/routes.rs | 5 ++++ src/routes/song.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++------ 9 files changed, 159 insertions(+), 13 deletions(-) delete mode 100644 N create mode 100644 README.md create mode 100644 docs/entry1.pdf create mode 100644 docs/entry2.pdf create mode 100644 docs/entry3.txt create mode 100644 docs/entry4.txt diff --git a/Cargo.toml b/Cargo.toml index 0e6a598..672a1d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ serde_json = "1.0.128" db_create = "bash ./scripts/create_db.sh" db_populate = "bash ./scripts/populate.sh" db_start = "sudo systemctl start mariadb" +db_stop = "sudo systemctl stop mariadb" # db_start = "sudo docker start sqlx" # db_start = "sudo docker stop sqlx" # db_remove = "sudo docker rm sqlx" diff --git a/N b/N deleted file mode 100644 index 207ae9c..0000000 --- a/N +++ /dev/null @@ -1,6 +0,0 @@ -Per preparar la base de dades: -cargo install cargo-run-script -cargo-run-script db-create - -Sobre les rutes: -# searchResults no s'ha implementat per falta de temps. diff --git a/README.md b/README.md new file mode 100644 index 0000000..164f0b3 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Balalaika +School project of a lyrics game site. + +## Setup database +To manage the database through the program's scripts use +`cargo run-script` + +You can install the app with `cargo install cargo-run-script` + +Available scripts: +``` +db_create +db_populate +db_start +db_stop +``` + +The stop/start scripts assume you're using systemd. +If not, just start/stop the mariadb service manually. + +## Changes +### Single parameters ONLY +You can only search a parameter at a time. +This is because Rust needs to process the sql queries ahead of time +in order to make them safe to injection attacks. + +### SearchResults changed its name +Since searchResults was highly unintuitive, +it has been renamed to just "name". diff --git a/docs/entry1.pdf b/docs/entry1.pdf new file mode 100644 index 0000000..2735c72 Binary files /dev/null and b/docs/entry1.pdf differ diff --git a/docs/entry2.pdf b/docs/entry2.pdf new file mode 100644 index 0000000..75073b9 Binary files /dev/null and b/docs/entry2.pdf differ diff --git a/docs/entry3.txt b/docs/entry3.txt new file mode 100644 index 0000000..f92c3df --- /dev/null +++ b/docs/entry3.txt @@ -0,0 +1,36 @@ +-------------------------------------------------------------------------------- +ACTIVITAT AVALUABLE AC3 +-------------------------------------------------------------------------------- +Mòdul: MP12- Projectes +-------------------------------------------------------------------------------- +UF: UF1 +-------------------------------------------------------------------------------- +Professor: Albert Guardiola, Marc Callejón +-------------------------------------------------------------------------------- +Data límit d'entrega: 16/10/2024 23:59 +-------------------------------------------------------------------------------- +Mètode d'entrega: Per mitjà del Clickedu de l'assignatura. Les activitats entregades més enllà de la data límit només podran obtenir una nota de 5. +-------------------------------------------------------------------------------- +Instruccions: S'ha d'entregar un únic fitxer comprimit amb el nom: +-------------------------------------------------------------------------------- + MP12-PROJ1-ENTR3.zip/rar +-------------------------------------------------------------------------------- +Es valorará la presentació. + +Tasca 1. Desenvolupar el codi de l'API especificada, en llenguatge Python. El codi de l'API ha de complir els següents requisits: +Requisits funcionals: +-Adequar-se coompletament a l'especificació elaborada a l' ENTREGA 1. Qualsevol desviació respecte de l'especificació ha de documentar-se a la Tasca 2. +Requisits tecnològics: +-El codi de l'API ha d'estar modularitzat, com a mínim, de la següent manera. + -Mòdul principal (app.py): instanciació i configuració (incloses les rutes) del servidor Flask. + -Mòdul de base de dades (bd.py): crides a la base de dades. + -Mòdul d'autenticació (auth.py): gestió de l'autenticació d'usuaris. +-Les crides a la base de dades han d'estar sotmeses a control d'excepcions. +-Les claus d'autenticació de l'API han d guardar-se encriptades a la base de dades. + +Tasca 2. Lliurar un checklist dels requisits funcionals especificats (ENTERGA 1) que marqui clarament si s'han implementat o no en el codi lliurat. Els requisits no implementats han de venir acompanyats d'una justificació de per què no s'han implementat. + +Tasca 3. Allotjar l'API al servidor de hosting https://www.pythonanywhere.com/. + (Es proporciona a continuació un tutorial per a allotjar una aplicació python amb una base de dades MySQL) + https://blog.pythonanywhere.com/121/ + diff --git a/docs/entry4.txt b/docs/entry4.txt new file mode 100644 index 0000000..6b2c4dd --- /dev/null +++ b/docs/entry4.txt @@ -0,0 +1,25 @@ +-------------------------------------------------------------------------------- +ACTIVITAT AVALUABLE AC4 +-------------------------------------------------------------------------------- +Mòdul: MP12- Projectes +-------------------------------------------------------------------------------- +UF: UF1 +-------------------------------------------------------------------------------- +Professor: Albert Guardiola, Marc Callejón +-------------------------------------------------------------------------------- + Data d'entrega: 30/10/24 +-------------------------------------------------------------------------------- +Mètode d'entrega: Per mitjà del Clickedu de l'assignatura. Les activitats entregades més enllà de la data límit només podran obtenir una nota de 5. +-------------------------------------------------------------------------------- +Instruccions: S'ha d'entregar un únic fitxer comprimit amb el nom: +-------------------------------------------------------------------------------- + MP12-PROJ1-ENTR4.zip/rar +-------------------------------------------------------------------------------- +Es valorará la presentació. + +Tasca 1. Testejar l'API desenvolupada, de manera exhaustiva (tots els endpoints, tots els paràmetres, tots els mètodes disponibles), amb un TalendTester o un forntend similar. + +Tasca 2. Generar la documentació de l'API en format HTML mitjançant la llibreria flask-apispec o similar: +https://flask-apispec.readthedocs.io/en/latest/ + + diff --git a/src/routes.rs b/src/routes.rs index 6195f32..a219da1 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -4,6 +4,11 @@ pub mod song; #[derive(Serialize)] pub struct Song { + id: Option, name: Option, lyrics: Option, + album_name: Option, + album_id: Option, + artist_name: Option, + artist_id: Option, } diff --git a/src/routes/song.rs b/src/routes/song.rs index f0630e9..8b28b8d 100644 --- a/src/routes/song.rs +++ b/src/routes/song.rs @@ -7,9 +7,11 @@ use serde::Deserialize; struct SongQueryOptions { id: Option, name: Option, + artist: Option, + album: Option, } -#[get("/song")] +#[get("/api/song")] pub async fn song( app_state: web::Data, get_args: web::Query, @@ -20,25 +22,79 @@ pub async fn song( _ if check_if_exists(&get_args.id) => { search_attempt = sqlx::query_as!( Song, - "SELECT name, lyrics FROM song WHERE id=?", + "SELECT song.name, song.lyrics, song.id, + album.name as album_name, album.id as album_id, + artist.name as artist_name, artist.id as artist_id + FROM song + INNER JOIN album ON song.album_id = album.id + INNER JOIN artist ON album.artist_id = artist.id + WHERE song.id=?", &get_args.id, ) .fetch_all(&app_state.pool) .await; } _ if check_if_exists(&get_args.name) => { + let new_name : String = format!("{}{}{}", "%", &get_args.name.clone().unwrap(), "%"); search_attempt = sqlx::query_as!( Song, - "SELECT name, lyrics FROM song WHERE LOWER(name) LIKE '%' + LOWER(?) + '%'", - &get_args.name, + "SELECT song.name, song.lyrics, song.id, + album.name as album_name, album.id as album_id, + artist.name as artist_name, artist.id as artist_id + FROM song + INNER JOIN album ON song.album_id = album.id + INNER JOIN artist ON album.artist_id = artist.id + WHERE LOWER(song.name) LIKE LOWER(?)", + new_name, + ) + .fetch_all(&app_state.pool) + .await; + } + _ if check_if_exists(&get_args.album) => { + search_attempt = sqlx::query_as!( + Song, + "SELECT song.name, song.lyrics, song.id, + album.name as album_name, album.id as album_id, + artist.name as artist_name, artist.id as artist_id + FROM song + INNER JOIN album ON song.album_id = album.id + INNER JOIN artist ON album.artist_id = artist.id + WHERE song.album_id=? + ", + &get_args.album, + ) + .fetch_all(&app_state.pool) + .await; + } + _ if check_if_exists(&get_args.artist) => { + search_attempt = sqlx::query_as!( + Song, + "SELECT song.name, song.lyrics, song.id, + album.name as album_name, album.id as album_id, + artist.name as artist_name, artist.id as artist_id + FROM song + INNER JOIN album ON song.album_id = album.id + INNER JOIN artist ON album.artist_id = artist.id + WHERE album.artist_id=? + ", + &get_args.artist, ) .fetch_all(&app_state.pool) .await; } _ => { - search_attempt = sqlx::query_as!(Song, "SELECT name, lyrics FROM song",) - .fetch_all(&app_state.pool) - .await; + search_attempt = sqlx::query_as!( + Song, + "SELECT song.name, song.lyrics, song.id, + album.name as album_name, album.id as album_id, + artist.name as artist_name, artist.id as artist_id + FROM song + INNER JOIN album ON song.album_id = album.id + INNER JOIN artist ON album.artist_id = artist.id + ", + ) + .fetch_all(&app_state.pool) + .await; } }; -- cgit v1.2.3