-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.rs
156 lines (144 loc) · 3.94 KB
/
day18.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
};
#[derive(Clone)]
struct Day18State {
pool: sqlx::PgPool,
}
pub(crate) fn router(pool: sqlx::PgPool) -> Router {
Router::new()
.route("/18/reset", post(reset))
.route("/18/orders", post(orders))
.route("/18/regions", post(regions))
.route("/18/regions/total", get(total))
.route("/18/regions/top_list/:count", get(top_list))
.with_state(Day18State { pool })
}
async fn reset(State(state): State<Day18State>) -> Result<StatusCode, StatusCode> {
sqlx::query!("drop table if exists regions")
.execute(&state.pool)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
sqlx::query!("drop table if exists orders")
.execute(&state.pool)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
sqlx::query!(
"create table regions (
id INT PRIMARY KEY,
name VARCHAR(50)
)"
)
.execute(&state.pool)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
sqlx::query!(
"create table orders (
id INT PRIMARY KEY,
region_id INT,
gift_name VARCHAR(50),
quantity INT
)"
)
.execute(&state.pool)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::OK)
}
#[derive(serde::Deserialize)]
struct Order {
id: i32,
region_id: i32,
gift_name: String,
quantity: i32,
}
async fn orders(
State(state): State<Day18State>,
Json(orders): Json<Vec<Order>>,
) -> Result<StatusCode, (StatusCode, String)> {
for Order {
id,
region_id,
gift_name,
quantity,
} in orders
{
sqlx::query!(
"insert into orders values ($1, $2, $3, $4)",
id,
region_id,
gift_name,
quantity
)
.execute(&state.pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
}
Ok(StatusCode::OK)
}
#[derive(serde::Deserialize)]
struct Region {
id: i32,
name: String,
}
async fn regions(
State(state): State<Day18State>,
Json(regions): Json<Vec<Region>>,
) -> Result<StatusCode, (StatusCode, String)> {
for Region { id, name } in regions {
sqlx::query!("insert into regions values ($1, $2)", id, name)
.execute(&state.pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
}
Ok(StatusCode::OK)
}
#[derive(serde::Serialize)]
struct Total {
region: String,
total: i64,
}
async fn total(State(state): State<Day18State>) -> Result<impl IntoResponse, (StatusCode, String)> {
let totals = sqlx::query_as!(
Total,
"select name as \"region!\", sum(quantity) as \"total!\"
from orders join regions on orders.region_id = regions.id
group by name order by name"
)
.fetch_all(&state.pool)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(Json(totals))
}
#[derive(serde::Serialize)]
struct TopList {
region: String,
top_gifts: Vec<String>,
}
async fn top_list(
Path(limit): Path<i32>,
State(state): State<Day18State>,
) -> 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()))?;
Ok(Json(top_lists))
}