diff --git a/sql/maps/get_all_maps.sql b/sql/maps/get_all_maps.sql new file mode 100644 index 0000000..6a3dd48 --- /dev/null +++ b/sql/maps/get_all_maps.sql @@ -0,0 +1,3 @@ +SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles + FROM record_maps + JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map \ No newline at end of file diff --git a/sql/maps/get_map_by_name.sql b/sql/maps/get_map_by_name.sql new file mode 100644 index 0000000..34a167e --- /dev/null +++ b/sql/maps/get_map_by_name.sql @@ -0,0 +1,4 @@ +SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles + FROM record_maps + JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map + WHERE record_maps.map = $1 \ No newline at end of file diff --git a/sql/maps/get_maps_by_category.sql b/sql/maps/get_maps_by_category.sql new file mode 100644 index 0000000..db3deda --- /dev/null +++ b/sql/maps/get_maps_by_category.sql @@ -0,0 +1,4 @@ +SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles + FROM record_maps + JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map + WHERE category = $1 \ No newline at end of file diff --git a/sql/maps/get_maps_by_mapper.sql b/sql/maps/get_maps_by_mapper.sql new file mode 100644 index 0000000..41bb273 --- /dev/null +++ b/sql/maps/get_maps_by_mapper.sql @@ -0,0 +1,4 @@ +SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles + FROM record_maps + JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map + WHERE $1 = ANY(mappers) \ No newline at end of file diff --git a/sql/races/get_races_by_map.sql b/sql/races/get_races_by_map.sql new file mode 100644 index 0000000..37554df --- /dev/null +++ b/sql/races/get_races_by_map.sql @@ -0,0 +1,18 @@ +SELECT + name, + map, + time, + timestamp, + server, + ARRAY[cp1, cp2, cp3, cp4, cp5, + cp6, cp7, cp8, cp9, cp10, + cp11, cp12, cp13, cp14, cp15, + cp16, cp17, cp18, cp19, cp20, + cp21, cp22, cp23, cp24, cp25 + ] AS checkpoints, + gameid, + ddnet7 +FROM record_race WHERE map = $1 +ORDER BY name, time +OFFSET (($2 - 1) * $3) +FETCH NEXT $3 ROWS ONLY diff --git a/sql/races/get_races_by_player.sql b/sql/races/get_races_by_player.sql new file mode 100644 index 0000000..8b5b624 --- /dev/null +++ b/sql/races/get_races_by_player.sql @@ -0,0 +1,18 @@ +SELECT + name, + map, + time, + timestamp, + server, + ARRAY[cp1, cp2, cp3, cp4, cp5, + cp6, cp7, cp8, cp9, cp10, + cp11, cp12, cp13, cp14, cp15, + cp16, cp17, cp18, cp19, cp20, + cp21, cp22, cp23, cp24, cp25 + ] AS checkpoints, + gameid, + ddnet7 +FROM record_race WHERE name = $1 +ORDER BY map, time +OFFSET (($2 - 1) * $3) +FETCH NEXT $3 ROWS ONLY \ No newline at end of file diff --git a/sql/teamraces/get_teamraces_by_map.sql b/sql/teamraces/get_teamraces_by_map.sql new file mode 100644 index 0000000..306f42d --- /dev/null +++ b/sql/teamraces/get_teamraces_by_map.sql @@ -0,0 +1,5 @@ +SELECT players, map, time, timestamp + FROM record_teamrace_array + WHERE map = $1 + OFFSET (($2 - 1) * $3) + FETCH NEXT $3 ROWS ONLY diff --git a/sql/teamraces/get_teamraces_by_player.sql b/sql/teamraces/get_teamraces_by_player.sql new file mode 100644 index 0000000..1a26f99 --- /dev/null +++ b/sql/teamraces/get_teamraces_by_player.sql @@ -0,0 +1,5 @@ +SELECT players, map, time, timestamp + FROM record_teamrace_array + WHERE $1 = ANY(players) + OFFSET (($2 - 1) * $3) + FETCH NEXT $3 ROWS ONLY diff --git a/src/api/teamraces.rs b/src/api/teamraces.rs index 449aff0..bad9e8c 100644 --- a/src/api/teamraces.rs +++ b/src/api/teamraces.rs @@ -15,7 +15,7 @@ async fn get_teamrace_by_player( page: Option<&str>, ) -> Result>, String> { let page_num = get_page_number_as_i32_from_str(page); - match Teamrace::get_teamrace_by_player(db, player, page_num).await { + match Teamrace::get_teamraces_by_player(db, player, page_num).await { Ok(maps) => Ok(Json(maps)), Err(err) => Err(format!("Error: {}", err)), } @@ -28,7 +28,7 @@ async fn get_teamrace_by_map( page: Option<&str>, ) -> Result>, String> { let page_num = get_page_number_as_i32_from_str(page); - match Teamrace::get_teamrace_by_map(db, map, page_num).await { + match Teamrace::get_teamraces_by_map(db, map, page_num).await { Ok(maps) => Ok(Json(maps)), Err(err) => Err(format!("Error: {}", err)), } diff --git a/src/database/models/map.rs b/src/database/models/map.rs index 9eb6df7..8bcb738 100644 --- a/src/database/models/map.rs +++ b/src/database/models/map.rs @@ -1,5 +1,4 @@ use chrono::NaiveDateTime; -use rocket::futures::StreamExt; use serde::{Deserialize, Serialize}; use crate::database::DatabaseHandler; @@ -19,67 +18,33 @@ pub struct Map { impl Map { pub async fn get_all_maps(db: &DatabaseHandler) -> Result, sqlx::Error> { - sqlx::query_as!( - Map, - " - SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles - FROM record_maps - JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map; - " - ) - .fetch_all(&db.pool) - .await + sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql") + .fetch_all(&db.pool) + .await } pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result { - sqlx::query_as!( - Map, - " - SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles - FROM record_maps - JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map - WHERE record_maps.map = $1; - ", - map - ) - .fetch_one(&db.pool) - .await + sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map) + .fetch_one(&db.pool) + .await } pub async fn get_maps_by_category( db: &DatabaseHandler, category: &str, ) -> Result, sqlx::Error> { - sqlx::query_as!( - Map, - " - SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles - FROM record_maps - JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map - WHERE category = $1; - ", - category - ) - .fetch_all(&db.pool) - .await + sqlx::query_file_as!(Map, "sql/maps/get_maps_by_category.sql", category) + .fetch_all(&db.pool) + .await } pub async fn get_maps_by_mapper( db: &DatabaseHandler, mapper: &str, ) -> Result, sqlx::Error> { - sqlx::query_as!( - Map, - " - SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles - FROM record_maps - JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map - WHERE $1 = ANY(mappers) - ", - mapper - ) - .fetch_all(&db.pool) - .await + sqlx::query_file_as!(Map, "sql/maps/get_maps_by_mapper.sql", mapper) + .fetch_all(&db.pool) + .await } } diff --git a/src/database/models/race.rs b/src/database/models/race.rs index 04c485f..b651d27 100644 --- a/src/database/models/race.rs +++ b/src/database/models/race.rs @@ -23,27 +23,9 @@ impl Race { player: &str, page: i32, ) -> Result, sqlx::Error> { - let results = sqlx::query_as!( + let results = sqlx::query_file_as!( Race, - " - SELECT name, - map, - time, - timestamp, - server, - ARRAY[cp1, cp2, cp3, cp4, cp5, - cp6, cp7, cp8, cp9, cp10, - cp11, cp12, cp13, cp14, cp15, - cp16, cp17, cp18, cp19, cp20, - cp21, cp22, cp23, cp24, cp25 - ] AS checkpoints, - gameid, - ddnet7 - FROM record_race WHERE name = $1 - ORDER BY map, time - OFFSET (($2 - 1) * $3) - FETCH NEXT $3 ROWS ONLY - ", + "sql/races/get_races_by_player.sql", player, page, ROWS_PER_PAGE @@ -73,28 +55,9 @@ impl Race { map: &str, page: i32, ) -> Result, sqlx::Error> { - let results = sqlx::query_as!( + let results = sqlx::query_file_as!( Race, - " - SELECT - name, - map, - time, - timestamp, - server, - ARRAY[cp1, cp2, cp3, cp4, cp5, - cp6, cp7, cp8, cp9, cp10, - cp11, cp12, cp13, cp14, cp15, - cp16, cp17, cp18, cp19, cp20, - cp21, cp22, cp23, cp24, cp25 - ] AS checkpoints, - gameid, - ddnet7 - FROM record_race WHERE map = $1 - ORDER BY name, time - OFFSET (($2 - 1) * $3) - FETCH NEXT $3 ROWS ONLY - ", + "sql/races/get_races_by_map.sql", map, page, ROWS_PER_PAGE diff --git a/src/database/models/teamrace.rs b/src/database/models/teamrace.rs index 49d31f9..a6b79c3 100644 --- a/src/database/models/teamrace.rs +++ b/src/database/models/teamrace.rs @@ -14,20 +14,14 @@ pub struct Teamrace { } impl Teamrace { - pub async fn get_teamrace_by_player( + pub async fn get_teamraces_by_player( db: &DatabaseHandler, player: &str, page: i32, ) -> Result, sqlx::Error> { - let results = sqlx::query_as!( + let results = sqlx::query_file_as!( Teamrace, - " - SELECT players, map, time, timestamp - FROM record_teamrace_array - WHERE $1 = ANY(players) - OFFSET (($2 - 1) * $3) - FETCH NEXT $3 ROWS ONLY - ", + "sql/teamraces/get_teamraces_by_player.sql", player, page, ROWS_PER_PAGE @@ -52,20 +46,14 @@ impl Teamrace { }) } - pub async fn get_teamrace_by_map( + pub async fn get_teamraces_by_map( db: &DatabaseHandler, map: &str, page: i32, ) -> Result, sqlx::Error> { - let results = sqlx::query_as!( + let results = sqlx::query_file_as!( Teamrace, - " - SELECT players, map, time, timestamp - FROM record_teamrace_array - WHERE map = $1 - OFFSET (($2 - 1) * $3) - FETCH NEXT $3 ROWS ONLY - ", + "sql/teamraces/get_teamraces_by_map.sql", map, page, ROWS_PER_PAGE