Skip to content

Commit 9fcd066

Browse files
committed
feat: add problem_index and name to Problem
1 parent 0de882b commit 9fcd066

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

atcoder-problems-backend/sql-client/src/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ impl Contest {
2323
pub struct Problem {
2424
pub id: String,
2525
pub contest_id: String,
26+
pub problem_index: String,
27+
pub name: String,
2628
pub title: String,
2729
}
2830

atcoder-problems-backend/sql-client/src/simple_client.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,36 @@ impl SimpleClient for PgPool {
6868
}
6969

7070
async fn insert_problems(&self, values: &[Problem]) -> Result<usize> {
71-
let (ids, contest_ids, titles) = values.iter().fold(
72-
(vec![], vec![], vec![]),
73-
|(mut ids, mut contest_ids, mut titles), cur| {
71+
let (ids, contest_ids, problem_indexes, names, titles) = values.iter().fold(
72+
(vec![], vec![], vec![], vec![], vec![]),
73+
|(mut ids, mut contest_ids, mut problem_indexes, mut names, mut titles), cur| {
7474
ids.push(cur.id.as_str());
7575
contest_ids.push(cur.contest_id.as_str());
76+
problem_indexes.push(cur.problem_index.as_str());
77+
names.push(cur.name.as_str());
7678
titles.push(cur.title.as_str());
77-
(ids, contest_ids, titles)
79+
(ids, contest_ids, problem_indexes, names, titles)
7880
},
7981
);
8082

8183
let result = sqlx::query(
8284
r"
8385
INSERT INTO problems
84-
(id, contest_id, title)
86+
(id, contest_id, problem_index, name, title)
8587
VALUES (
8688
UNNEST($1::VARCHAR(255)[]),
8789
UNNEST($2::VARCHAR(255)[]),
88-
UNNEST($3::VARCHAR(255)[])
90+
UNNEST($3::VARCHAR(255)[]),
91+
UNNEST($4::VARCHAR(255)[]),
92+
UNNEST($5::VARCHAR(255)[])
8993
)
9094
ON CONFLICT DO NOTHING
9195
",
9296
)
9397
.bind(ids)
9498
.bind(contest_ids)
99+
.bind(problem_indexes)
100+
.bind(names)
95101
.bind(titles)
96102
.execute(self)
97103
.await?;
@@ -100,19 +106,24 @@ impl SimpleClient for PgPool {
100106
}
101107

102108
async fn load_problems(&self) -> Result<Vec<Problem>> {
103-
let problems = sqlx::query("SELECT id, contest_id, title FROM problems")
104-
.try_map(|row: PgRow| {
105-
let id: String = row.try_get("id")?;
106-
let contest_id: String = row.try_get("contest_id")?;
107-
let title: String = row.try_get("title")?;
108-
Ok(Problem {
109-
id,
110-
contest_id,
111-
title,
109+
let problems =
110+
sqlx::query("SELECT id, contest_id, problem_index, name, title FROM problems")
111+
.try_map(|row: PgRow| {
112+
let id: String = row.try_get("id")?;
113+
let contest_id: String = row.try_get("contest_id")?;
114+
let problem_index: String = row.try_get("problem_index")?;
115+
let name: String = row.try_get("name")?;
116+
let title: String = row.try_get("title")?;
117+
Ok(Problem {
118+
id,
119+
contest_id,
120+
problem_index,
121+
name,
122+
title,
123+
})
112124
})
113-
})
114-
.fetch_all(self)
115-
.await?;
125+
.fetch_all(self)
126+
.await?;
116127
Ok(problems)
117128
}
118129

atcoder-problems-backend/sql-client/tests/test_simple_client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ async fn test_insert_problems() {
3838
pool.insert_problems(&[Problem {
3939
id: "problem1".to_string(),
4040
contest_id: "".to_string(),
41+
problem_index: "".to_string(),
42+
name: "".to_string(),
4143
title: "".to_string(),
4244
}])
4345
.await
@@ -49,6 +51,8 @@ async fn test_insert_problems() {
4951
pool.insert_problems(&[Problem {
5052
id: "problem1".to_string(),
5153
contest_id: "".to_string(),
54+
problem_index: "".to_string(),
55+
name: "".to_string(),
5256
title: "".to_string(),
5357
}])
5458
.await

atcoder-problems-backend/src/bin/dump_json.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ async fn main() -> Result<()> {
124124
SELECT
125125
problems.id AS merged_problem_id,
126126
problems.contest_id AS merged_contest_id,
127+
problems.problem_index AS merged_problem_index,
128+
problems.name AS merged_problem_name,
127129
problems.title AS merged_problem_title,
128130
129131
shortest.submission_id AS shortest_submission_id,
@@ -158,6 +160,8 @@ async fn main() -> Result<()> {
158160
.map(|row: PgRow| {
159161
let id: String = row.get("merged_problem_id");
160162
let contest_id: String = row.get("merged_contest_id");
163+
let problem_index: String = row.get("merged_problem_index");
164+
let name: String = row.get("merged_problem_name");
161165
let title: String = row.get("merged_problem_title");
162166

163167
let shortest_submission_id: Option<i64> = row.get("shortest_submission_id");
@@ -180,6 +184,9 @@ async fn main() -> Result<()> {
180184
MergedProblem {
181185
id,
182186
contest_id,
187+
problem_index,
188+
name,
189+
183190
title,
184191
shortest_submission_id,
185192
shortest_contest_id,
@@ -236,6 +243,8 @@ struct UserStreak {
236243
struct MergedProblem {
237244
id: String,
238245
contest_id: String,
246+
problem_index: String,
247+
name: String,
239248
title: String,
240249
shortest_submission_id: Option<i64>,
241250
shortest_contest_id: Option<String>,

atcoder-problems-backend/src/crawler/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ fn convert_problem(p: AtCoderProblem) -> Problem {
126126
Problem {
127127
id: p.id,
128128
contest_id: p.contest_id,
129+
problem_index: p.position.to_string(),
129130
title: p.position + ". " + p.title.as_str(),
131+
name: p.title.to_string(),
130132
}
131133
}
132134

config/database-definition.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ DROP TABLE IF EXISTS problems;
2626
CREATE TABLE problems (
2727
id VARCHAR(255) NOT NULL,
2828
contest_id VARCHAR(255) NOT NULL,
29+
problem_index VARCHAR(255) NOT NULL,
30+
name VARCHAR(255) NOT NULL,
2931
title VARCHAR(255) NOT NULL,
3032
PRIMARY KEY (id)
3133
);

0 commit comments

Comments
 (0)