Order by for search query

main
furo 2023-04-17 09:36:55 +02:00
parent aee7ab891b
commit a756f7916c
3 changed files with 28 additions and 5 deletions

View File

@ -1,9 +1,22 @@
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::TEXT IS NULL OR record_maps.map ILIKE $1)
AND (COALESCE(ARRAY_LENGTH($2::TEXT[], 1), NULL)::BOOLEAN IS TRUE OR $2::TEXT[] <@ mappers::TEXT[])
AND (ARRAY_LENGTH(ARRAY[]::TEXT[], 1) > 0 IS TRUE OR $2::TEXT[] <@ mappers::TEXT[])
AND ($3::TEXT IS NULL OR category = $3)
AND ($4::SMALLINT[] IS NULL OR stars = ANY($4))
AND ($5::TIMESTAMP IS NULL OR release >= $5)
AND ($6::TIMESTAMP IS NULL OR release <= $6)
AND (COALESCE(ARRAY_LENGTH($7::TEXT[], 1), NULL)::BOOLEAN IS TRUE OR $7::TEXT[] <@ tiles::TEXT[])
AND (ARRAY_LENGTH(ARRAY[]::TEXT[], 1) > 0 IS TRUE OR $7::TEXT[] <@ tiles::TEXT[])
ORDER BY -- what in the flying fuck
(case when lower($8) = 'map' and lower($9) = 'asc' then record_maps.map end) asc,
(case when lower($8) = 'map' and lower($9) = 'desc' then record_maps.map end) desc,
(case when lower($8) = 'category' and lower($9) = 'asc' then category end) asc,
(case when lower($8) = 'category' and lower($9) = 'desc' then category end) desc,
(case when lower($8) = 'points' and lower($9) = 'asc' then points::INTEGER end) asc,
(case when lower($8) = 'points' and lower($9) = 'desc' then points::INTEGER end) desc,
(case when lower($8) = 'stars' and lower($9) = 'asc' then stars::INTEGER end) asc,
(case when lower($8) = 'stars' and lower($9) = 'desc' then stars::INTEGER end) desc,
(case when lower($8) = 'width' and lower($9) = 'asc' then width::INTEGER end) asc,
(case when lower($8) = 'width' and lower($9) = 'desc' then width::INTEGER end) desc,
(case when lower($8) = 'height' and lower($9) = 'asc' then height::INTEGER end) asc,
(case when lower($8) = 'height' and lower($9) = 'desc' then height::INTEGER end) desc

View File

@ -41,7 +41,7 @@ async fn get_maps_by_mapper(
}
}
#[get("/search?<map>&<mappers>&<category>&<stars>&<release_after>&<release_before>&<tiles>")]
#[get("/search?<map>&<mappers>&<category>&<stars>&<release_after>&<release_before>&<tiles>&<orderby>&<sort>")]
async fn search_maps<'a>(
db: &State<DatabaseHandler>,
map: Option<&str>,
@ -51,6 +51,8 @@ async fn search_maps<'a>(
release_after: Option<&str>,
release_before: Option<&str>,
tiles: Option<Vec<String>>,
orderby: Option<&str>,
sort: Option<&str>,
) -> Result<Json<Vec<Map>>, String> {
match Map::search_maps(
db,
@ -61,6 +63,8 @@ async fn search_maps<'a>(
release_after.map(|x| NaiveDateTime::parse_from_str(x, "%Y-%m-%d").unwrap()),
release_before.map(|x| NaiveDateTime::parse_from_str(x, "%Y-%m-%d").unwrap()),
tiles,
orderby,
sort,
)
.await
{

View File

@ -66,10 +66,14 @@ impl Map {
release_after: Option<NaiveDateTime>,
release_before: Option<NaiveDateTime>,
tiles: Option<Vec<String>>,
orderby: Option<&str>,
sort: Option<&str>,
) -> Result<Vec<Map>, sqlx::Error> {
let stars = stars.unwrap_or((0..=5).collect());
let mappers = mappers.unwrap_or(vec!["%".to_string()]);
let tiles = tiles.unwrap_or(vec!["%".to_string()]);
let mappers = mappers.unwrap_or(vec![]);
let tiles = tiles.unwrap_or(vec![]);
println!("{:?}", tiles.as_slice());
sqlx::query_file_as!(
Map,
@ -81,6 +85,8 @@ impl Map {
release_after,
release_before,
tiles.as_slice(),
orderby,
sort,
)
.fetch_all(&db.pool)
.await