Searching implemented.
This commit is contained in:
parent
4f8b21bc36
commit
aee7ab891b
9
sql/maps/search_maps.sql
Normal file
9
sql/maps/search_maps.sql
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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 ($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[])
|
|
@ -1,3 +1,4 @@
|
||||||
|
use chrono::NaiveDateTime;
|
||||||
use rocket::{serde::json::Json, Route, State};
|
use rocket::{serde::json::Json, Route, State};
|
||||||
|
|
||||||
use crate::database::{models::map::Map, DatabaseHandler};
|
use crate::database::{models::map::Map, DatabaseHandler};
|
||||||
|
@ -40,11 +41,40 @@ async fn get_maps_by_mapper(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/search?<map>&<mappers>&<category>&<stars>&<release_after>&<release_before>&<tiles>")]
|
||||||
|
async fn search_maps<'a>(
|
||||||
|
db: &State<DatabaseHandler>,
|
||||||
|
map: Option<&str>,
|
||||||
|
mappers: Option<Vec<String>>,
|
||||||
|
category: Option<&str>,
|
||||||
|
stars: Option<Vec<i16>>,
|
||||||
|
release_after: Option<&str>,
|
||||||
|
release_before: Option<&str>,
|
||||||
|
tiles: Option<Vec<String>>,
|
||||||
|
) -> Result<Json<Vec<Map>>, String> {
|
||||||
|
match Map::search_maps(
|
||||||
|
db,
|
||||||
|
map,
|
||||||
|
mappers,
|
||||||
|
category,
|
||||||
|
stars,
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(maps) => Ok(Json(maps)),
|
||||||
|
Err(err) => Err(format!("Error: {}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_all_routes() -> Vec<Route> {
|
pub fn get_all_routes() -> Vec<Route> {
|
||||||
routes![
|
routes![
|
||||||
get_all_maps,
|
get_all_maps,
|
||||||
get_map_by_name,
|
get_map_by_name,
|
||||||
get_maps_by_category,
|
get_maps_by_category,
|
||||||
get_maps_by_mapper
|
get_maps_by_mapper,
|
||||||
|
search_maps
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,36 @@ impl Map {
|
||||||
.fetch_all(&db.pool)
|
.fetch_all(&db.pool)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Experimental function to search for maps.
|
||||||
|
pub async fn search_maps(
|
||||||
|
db: &DatabaseHandler,
|
||||||
|
map: Option<&str>,
|
||||||
|
mappers: Option<Vec<String>>,
|
||||||
|
category: Option<&str>,
|
||||||
|
stars: Option<Vec<i16>>,
|
||||||
|
release_after: Option<NaiveDateTime>,
|
||||||
|
release_before: Option<NaiveDateTime>,
|
||||||
|
tiles: Option<Vec<String>>,
|
||||||
|
) -> 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()]);
|
||||||
|
|
||||||
|
sqlx::query_file_as!(
|
||||||
|
Map,
|
||||||
|
"sql/maps/search_maps.sql",
|
||||||
|
map.map(|map| format!("%{}%", map)),
|
||||||
|
mappers.as_slice(),
|
||||||
|
category,
|
||||||
|
stars.as_slice(),
|
||||||
|
release_after,
|
||||||
|
release_before,
|
||||||
|
tiles.as_slice(),
|
||||||
|
)
|
||||||
|
.fetch_all(&db.pool)
|
||||||
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user