mirror of
https://github.com/supleed2/cch23-8bit.git
synced 2024-12-22 05:55:50 +00:00
Day 18 sqlx files + fixed query
This commit is contained in:
parent
f33fcacece
commit
d6adad1c53
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "select name as \"region!\", sum(quantity) as \"total!\"\n from orders join regions on orders.region_id = regions.id\n group by name order by name",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "region!",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "total!",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": [
|
||||
true,
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "1c9e1cc1e5326aca618ac1093c97eb9d50290537c93baa76a70285559b96e351"
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "insert into regions values ($1, $2)",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int4",
|
||||
"Varchar"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "2a21722026139ab89b4f8131d12d47b1da5471d984bd015ece9051dfaccd0eaa"
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "drop table if exists regions",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "37b00daca64bf079e94b5d091990c4ad3e3eef03e850641d4d4de956b1f076ae"
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "create table orders (\n id INT PRIMARY KEY,\n region_id INT,\n gift_name VARCHAR(50),\n quantity INT\n )",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "a85986b4dd4eb8c6e86b01ad38d0b9558d5519d218ee25a6a3bb64d0c6f49c61"
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "create table regions (\n id INT PRIMARY KEY,\n name VARCHAR(50)\n )",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "bdd47285c5aec94ebd89c91f2ba2b82c1f2f8f78665f4fce65daa4ad7d2b1e0e"
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "select r.name as \"region!\", (array_remove(array_agg(o.g), null))[1:$1] as \"top_gifts!\" from regions as r left join lateral (select gift_name as g, region_id as r from orders group by r, g order by sum(quantity) desc, g) as o on r.id = o.r group by r.name order by r.name",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "region!",
|
||||
"type_info": "Varchar"
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "top_gifts!",
|
||||
"type_info": "VarcharArray"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int4"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
true,
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "fa0b25c2c2e00f619c9bbdeed70e090e1a6e3aee1bd438d2cda81332af564a9d"
|
||||
}
|
|
@ -125,43 +125,32 @@ async fn total(State(state): State<Day18State>) -> Result<impl IntoResponse, (St
|
|||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct TopGifts {
|
||||
struct TopList {
|
||||
region: String,
|
||||
top_gifts: Vec<String>,
|
||||
}
|
||||
|
||||
async fn top_list(
|
||||
Path(limit): Path<i64>,
|
||||
Path(limit): Path<i32>,
|
||||
State(state): State<Day18State>,
|
||||
) -> Result<Json<Vec<TopGifts>>, (StatusCode, String)> {
|
||||
let mut top_list = vec![];
|
||||
|
||||
let regions = sqlx::query_as!(
|
||||
Region,
|
||||
"select id, name as \"name!\" from regions order by name"
|
||||
) -> Result<Json<Vec<TopList>>, (StatusCode, String)> {
|
||||
let top_lists = sqlx::query_as!(
|
||||
TopList,
|
||||
"select r.name as \"region!\", (array_remove(array_agg(o.g), null))[1:$1] as \"top_gifts!\" \
|
||||
from regions as r \
|
||||
left join lateral (\
|
||||
select gift_name as g, region_id as r \
|
||||
from orders \
|
||||
group by r, g \
|
||||
order by sum(quantity) desc, g) as o \
|
||||
on r.id = o.r \
|
||||
group by r.name \
|
||||
order by r.name",
|
||||
limit
|
||||
)
|
||||
.fetch_all(&state.pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
for region in regions {
|
||||
let top_gifts = sqlx::query!(
|
||||
"select gift_name as \"gift_name!\" from orders where region_id = $1 group by gift_name order by sum(quantity) desc limit $2",
|
||||
region.id,
|
||||
limit
|
||||
)
|
||||
.fetch_all(&state.pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
.into_iter()
|
||||
.map(|r| r.gift_name)
|
||||
.collect();
|
||||
|
||||
top_list.push(TopGifts {
|
||||
region: region.name,
|
||||
top_gifts,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Json(top_list))
|
||||
Ok(Json(top_lists))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue